Professional Documents
Culture Documents
java
1 package demoCalculator;
2
3 import java.awt.EventQueue;
12
13 public class DemoCalculator {
14
15 private JFrame frame;
16 private JTextField txtDisplay;
17
18 double firstnum;
19 double secondnum;
20 double result;
21 String operations;
22 String answer;
23
24 /**
25 * Launch the application.
26 */
27 public static void main(String[] args) {
28 EventQueue.invokeLater(new Runnable() {
29 public void run() {
30 try {
31 DemoCalculator window = new DemoCalculator();
32 window.frame.setVisible(true);
33 } catch (Exception e) {
34 e.printStackTrace();
35 }
36 }
37 });
38 }
39
40 /**
41 * Create the application.
42 */
43 public DemoCalculator() {
44 initialize();
45 }
46
47 /**
48 * Initialize the contents of the frame.
49 */
50 private void initialize() {
51 frame = new JFrame();
52 frame.setBounds(100, 100, 467, 666);
53 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
54 frame.getContentPane().setLayout(null);
55
56 txtDisplay = new JTextField();
57 txtDisplay.setHorizontalAlignment(SwingConstants.RIGHT);
58 txtDisplay.setFont(new Font("Tahoma", Font.BOLD, 60));
59 txtDisplay.setBounds(10, 10, 430, 60);
60 frame.getContentPane().add(txtDisplay);
61 txtDisplay.setColumns(10);
62
63 //===================ROW 1=============================
64
65 JButton btnbackspace = new JButton("\uF0E7");
Page 1
DemoCalculator.java
66 btnbackspace.addActionListener(new ActionListener() {
67 public void actionPerformed(ActionEvent arg0) {
68
69 if(txtDisplay.getText().length()>0)
70 {
71 String backspace = null;
72
73 StringBuilder strB = new StringBuilder(txtDisplay.getText());
74 strB.deleteCharAt(txtDisplay.getText().length()-1);
75 backspace = strB.toString();
76 txtDisplay.setText(backspace);
77 }
78
79 }
80 });
81 btnbackspace.setFont(new Font("Dialog", Font.BOLD, 50));
82 btnbackspace.setBounds(10, 80, 100, 100);
83 frame.getContentPane().add(btnbackspace);
84
85 JButton btnclear = new JButton("C");
86 btnclear.addActionListener(new ActionListener() {
87 public void actionPerformed(ActionEvent arg0) {
88
89 txtDisplay.setText(null);
90
91 }
92 });
93 btnclear.setFont(new Font("Tahoma", Font.BOLD, 70));
94 btnclear.setBounds(120, 80, 100, 100);
95 frame.getContentPane().add(btnclear);
96
97 JButton btnModulus = new JButton("%");
98 btnModulus.addActionListener(new ActionListener() {
99 public void actionPerformed(ActionEvent arg0) {
100
101 firstnum = Double.parseDouble(txtDisplay.getText());
102 txtDisplay.setText("");
103 operations = "%";
104
105 }
106 });
107 btnModulus.setFont(new Font("Tahoma", Font.BOLD, 50));
108 btnModulus.setBounds(230, 80, 100, 100);
109 frame.getContentPane().add(btnModulus);
110
111 JButton btnPlus = new JButton("+");
112 btnPlus.addActionListener(new ActionListener() {
113 public void actionPerformed(ActionEvent arg0) {
114
115 firstnum = Double.parseDouble(txtDisplay.getText());
116 txtDisplay.setText("");
117 operations = "+";
118
119 }
120 });
121 btnPlus.setFont(new Font("Tahoma", Font.BOLD, 70));
122 btnPlus.setBounds(340, 80, 100, 100);
Page 2
DemoCalculator.java
123 frame.getContentPane().add(btnPlus);
124
125 //===================ROW 2=============================
126
127 JButton btn7 = new JButton("7");
128 btn7.addActionListener(new ActionListener() {
129 public void actionPerformed(ActionEvent arg0) {
130
131 String EnterValue = txtDisplay.getText() + btn7.getText();
132 txtDisplay.setText(EnterValue);
133
134 }
135 });
136 btn7.setFont(new Font("Tahoma", Font.BOLD, 70));
137 btn7.setBounds(10, 190, 100, 100);
138 frame.getContentPane().add(btn7);
139
140 JButton btn8 = new JButton("8");
141 btn8.addActionListener(new ActionListener() {
142 public void actionPerformed(ActionEvent arg0) {
143
144 String EnterValue = txtDisplay.getText() + btn8.getText();
145 txtDisplay.setText(EnterValue);
146
147 }
148 });
149 btn8.setFont(new Font("Tahoma", Font.BOLD, 70));
150 btn8.setBounds(120, 190, 100, 100);
151 frame.getContentPane().add(btn8);
152
153 JButton btn9 = new JButton("9");
154 btn9.addActionListener(new ActionListener() {
155 public void actionPerformed(ActionEvent arg0) {
156
157 String EnterValue = txtDisplay.getText() + btn9.getText();
158 txtDisplay.setText(EnterValue);
159
160 }
161 });
162 btn9.setFont(new Font("Tahoma", Font.BOLD, 70));
163 btn9.setBounds(230, 190, 100, 100);
164 frame.getContentPane().add(btn9);
165
166 JButton btnMinus = new JButton("-");
167 btnMinus.addActionListener(new ActionListener() {
168 public void actionPerformed(ActionEvent arg0) {
169
170 firstnum = Double.parseDouble(txtDisplay.getText());
171 txtDisplay.setText("");
172 operations = "-";
173
174 }
175 });
176 btnMinus.setFont(new Font("Tahoma", Font.BOLD, 70));
177 btnMinus.setBounds(340, 190, 100, 100);
178 frame.getContentPane().add(btnMinus);
179
Page 3
DemoCalculator.java
Page 4
DemoCalculator.java
Page 5
DemoCalculator.java
Page 6
DemoCalculator.java
Page 7