Java Reference
In-Depth Information
window, the application program is created by extending the definition of the
class JFrame . Thus, we need the following GUI components:
private JLabel headingMainL;
//label for the first line
private JLabel selectionL;
//label for the second line
private JButton exitB, candyB, chipsB, gumB, cookiesB;
The following statements create and instantiate these labels and button objects:
headingMainL = new JLabel("WELCOME TO SHELLY'S CANDY SHOP",
SwingConstants.CENTER);
selectionL = new JLabel("To Make a Selection, "
+ "Click on the Product Button",
SwingConstants.CENTER);
candyB = new JButton("Candy");
chipsB = new JButton("Chips");
gumB = new JButton("Gum");
8
cookiesB = new JButton("Cookies");
exitB = new JButton("Exit");
These components are to be placed in the content pane of the window. The seven
components—labels and buttons—are arranged in seven rows. Therefore, the content
pane layout will be a grid of 7 rows and 1 column. The following statements get the
content pane and add these components to the content pane:
Container pane = getContentPane();
setSize(300, 300);
pane.setLayout( new GridLayout(7,1));
pane.add(headingMainL);
pane.add(selectionL);
pane.add(candyB);
pane.add(chipsB);
pane.add(gumB);
pane.add(cookiesB);
pane.add(exitB);
When the user clicks on a product button, it generates an action event. There are five
buttons, each generating an action event. To handle these action events, we use the
same process that we used in Chapter 6. That is:
EVENT
HANDLING
Search WWH ::




Custom Search