How to create a Button in Java

This is one of the simplest components. The component labeled ''Add to Cart," in Figure 1, is an example of a button. To create a button, first declare the button object to be of type JButton. For example,

JButton cartbutton;


Then invoke the javax.swing.JButton constructor, as follows:

cartbutton = new JButton(''Add to Cart");

This will create a button labeled with the supplied text. It is possible to create an unlabeled button using the constructor JButton(). All the layout examples in Section 8.5 also use buttons.

No comments:

Post a Comment