Java Reference
In-Depth Information
Constructors you can use for buttons include the following:
JButton( String ) —A button labeled with the specified text
n
JButton( Icon ) —A button that displays the specified graphical icon
n
JButton( String , Icon ) —A button with the specified text and graphical icon
n
The following statements create three buttons with text labels:
JButton play = new JButton(“Play”);
JButton stop = new JButton(“Stop”);
JButton rewind = new JButton(“Rewind”);
Graphical buttons are covered later today.
Adding Components to a Container
Before you can display a user interface component such as a button in a Java program,
you must add it to a container and display that container.
To add a component to a container, call the container's add( Component ) method with the
component as the argument (all user interface components in Swing inherit from
java.awt.Component ).
The simplest Swing container is a panel (the JPanel class). The following example cre-
ates a button and adds it to a panel:
JButton quit = new JButton(“Quit”);
JPanel panel = new JPanel();
panel.add(quit);
Use the same technique to add components to frames and windows.
In previous versions of Java, components could not be added
directly to frames. Instead, they were placed in the container's
content pane. Though this technique is no longer necessary,
you're likely to encounter it in code.
Complex containers are divided into panes, a kind of container
within a container, and components are added to the container's
content pane .
You can use a panel to represent a frame's content pane, adding
components to it with the panel's add( Component ) method. After the
panel has been filled, call the frame's setContentPane( Container )
method with the panel as the argument. This makes it the frame's
content pane, which also can be done with windows.
NOTE
Search WWH ::




Custom Search