CASE STUDY^419
// Instantiate button objects
add = newJButton("+");
subtract = newJButton("-");
clear = newJButton("Clear");
// Register the button listeners
add.addActionListener(operation);
subtract.addActionListener(operation);
clear.addActionListener(clearOperation);
// Add interface elements to calcFrame
calcFrame = newJFrame(); // Give the frame a value
calcFrame.setSize( 300 , 200 ); // Specify size of frame
// Set close operation to exit
calcFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
calcPane = calcFrame.getContentPane(); // Get the content pane
calcPane.setLayout(new GridLayout( 4 , 2 )); // Set the layout manager
calcPane.add(resultLabel);
calcPane.add(register);
calcPane.add(entryLabel);
calcPane.add(inputField);
calcPane.add(add);
calcPane.add(subtract);
calcPane.add(clear);
calcFrame.setVisible(true); // Show the frame
}
}
When we run the application, it displays a window like the one here: