Java Reference
In-Depth Information
Display 17.19
A Simple Calculator (part 1 of 4)
1 import javax.swing.JFrame;
2 import javax.swing.JTextField;
3 import javax.swing.JPanel;
4 import javax.swing.JLabel;
5 import javax.swing.JButton;
6 import java.awt.BorderLayout;
7 import java.awt.FlowLayout;
8 import java.awt.Color;
9 import java.awt.event.ActionListener;
10 import java.awt.event.ActionEvent;
11 /**
12 A simplified calculator.
13 The only operations are addition and subtraction.
14 */
15 public class Calculator extends JFrame
16
implements ActionListener
17 {
18
public static final int WIDTH = 400;
19
public static final int HEIGHT = 200;
20
public static final int NUMBER_OF_DIGITS = 30;
21
private JTextField ioField;
22
private double result = 0.0;
23 public static void main(String[] args)
24 {
25 Calculator aCalculator = new Calculator();
26 aCalculator.setVisible( true );
27 }
28 public Calculator()
29 {
30 setTitle("Simplified Calculator");
31 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
32 setSize(WIDTH, HEIGHT);
33 setLayout( new BorderLayout());
34 JPanel textPanel = new JPanel();
35 textPanel.setLayout( new FlowLayout());
36 ioField =
37 new JTextField("Enter numbers here.",NUMBER_OF_DIGITS);
38 ioField.setBackground(Color.WHITE);
39 textPanel.add(ioField);
40 add(textPanel, BorderLayout.NORTH);
41 JPanel buttonPanel = new JPanel();
42 buttonPanel.setBackground(Color.BLUE);
43 buttonPanel.setLayout( new FlowLayout());
Search WWH ::




Custom Search