// package SwingDemos; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * CalculatorPanel.java * * * Created: Tue Mar 18 11:22:33 2003 * * @author <a href="mailto:shapiro@cse.buffalo.edu">Stuart C. Shapiro</a> * @version */ public class CalculatorPanel extends JPanel implements ActionListener { JTextField op1, op2; JLabel results; JButton quitButton; public CalculatorPanel (){ setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); JPanel fieldPanel = new JPanel(); op1 = new JTextField("0",5); op2 = new JTextField("0",5); results = new JLabel("0"); quitButton = new JButton("Quit"); op1.addActionListener(this); op2.addActionListener(this); quitButton.addActionListener(this); fieldPanel.add(op1); fieldPanel.add(new JLabel(" x ")); fieldPanel.add(op2); fieldPanel.add(new JLabel(" = ")); fieldPanel.add(results); add(fieldPanel); add(quitButton); } public void actionPerformed(ActionEvent evt) { if (evt.getSource() == quitButton) { System.exit(0); } else { try {results.setText("" + Integer.parseInt(op1.getText()) * Integer.parseInt(op2.getText())); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(this, "The entry must be an integer.", "error", JOptionPane.ERROR_MESSAGE); } // end of try-catch } // end of if-else } // actionPerformed }// CalculatorPanel