Java Reference
In-Depth Information
Adding a Menu Bar to a Frame
There are two different ways to add a menu bar to a
JFrame
. You can use the method
add
to
add the menu bar to a
JFrame
(or to any other container). Another common way of adding a
menu bar to a
JFrame
is to use the method
setJMenuBar
as follows:
SYNTAX
setJMenuBar(
JMenu_Bar_Name
);
EXAMPLE
setJMenuBar(bar);
Display 17.15
Some Methods in the Class
AbstractButton
(part 1 of 2)
The abstract class
AbstractButton
is in the
javax.swing
package.
All of these methods are inherited by both of the classes
JButton
and
JMenuItem
.
public void
setBackground(Color theColor)
Sets the background color of this component.
public void
addActionListener(ActionListener listener)
Adds an
ActionListener
.
public void
removeActionListener(ActionListener listener)
Removes an
ActionListener
.
public void
setActionCommand(String actionCommand)
Sets the action command.
public
String getActionCommand()
Returns the action command for this component.
public
void
setText(String text)
Makes text the only text on this component.
public
String getText()
Returns the text written on the component, such as the text on a button or the string for a menu item.
public
void
setPreferredSize(Dimension preferredSize)
Sets the preferred size of the button or label. Note that this is only a suggestion to the layout man-
ager. The layout manager is not required to use the preferred size. The following special case will
work for most simple situations. The
int
values give the width and height in pixels.
public
void
setPreferredSize(
new
Dimension(
int
width,
int
height))