Yes. This means that a JButton
can contain
other components.
This is sometimes used to put a picture on a button.
Ordinary AWT buttons (class Button
) can't do this.
Here is an example program that adds a button to a frame.
import java.awt.*; import java.awt.event.*; import javax.swing.*; class ButtonFrame extends JFrame { JButton bChange ; // reference to the button object // constructor for ButtonFrame ButtonFrame() { // construct a Button bChange = new JButton("Click Me!"); // add the button to the JFrame getContentPane().add( bChange ); setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE ); } } public class ButtonDemo { public static void main ( String[] args ) { ButtonFrame frm = new ButtonFrame(); frm.setSize( 200, 150 ); frm.setVisible( true ); } }
To construct a JButton
object, use new
.
Then add the button to the frame's
content pane.
The content pane is a container that
represents the main rectangle of the frame.
To get a reference to the content pane, use
the getContentPane()
method of the frame.
The add
method of a content pane puts a
JButton
(or other component) into the frame.
What do you think JButton
constructor parameter "Click Me!" does?