-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwingArithmeticOperations.java
More file actions
127 lines (114 loc) · 3.52 KB
/
Copy pathSwingArithmeticOperations.java
File metadata and controls
127 lines (114 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package swingarithmeticoperations;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
*
* @author the_developer
*/
class Start implements ActionListener
{
double a,b,c;
JFrame frame;
JTextField number1,number2;
JButton add,subtract,multiply,divide,modulo;
JLabel title,result;
public Start() {
setupFrame();
setUI();
add_UI();
add.addActionListener(this);
subtract.addActionListener(this);
multiply.addActionListener(this);
divide.addActionListener(this);
modulo.addActionListener(this);
}
private void setupFrame() {
frame = new JFrame();
frame.setSize(300, 300);
frame.setVisible(true);
frame.setTitle("Simple Calci");
frame.setLayout(new FlowLayout(1,30,30));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void setUI() {
number1 = new JTextField(10);
number2 = new JTextField(10);
add = new JButton("ADD");
subtract = new JButton("SUBTRACT");
multiply = new JButton("MULTIPLY");
divide = new JButton("DIVIDE");
modulo = new JButton("MODULOUS");
title = new JLabel("CALCULATOR");
title.setForeground(Color.BLUE);
result = new JLabel();
}
private void add_UI() {
frame.add(title);
frame.add(number1);
frame.add(number2);
frame.add(add);
frame.add(subtract);
frame.add(multiply);
frame.add(divide);
frame.add(modulo);
frame.add(result);
}
@Override
public void actionPerformed(ActionEvent ae) {
if(ae.getSource() == add)
{
a = Double.parseDouble(number1.getText());
b = Double.parseDouble(number2.getText());
c = a+b;
result.setText("The Answer is "+String.valueOf(c));
}
else if(ae.getSource() == subtract)
{
a = Double.parseDouble(number1.getText());
b = Double.parseDouble(number2.getText());
c = a-b;
result.setText("The Answer is "+String.valueOf(c));
}
else if(ae.getSource() == multiply)
{
a = Double.parseDouble(number1.getText());
b = Double.parseDouble(number2.getText());
c = a*b;
result.setText("The Answer is "+String.valueOf(c));
}
else if(ae.getSource() == divide)
{
a = Double.parseDouble(number1.getText());
b = Double.parseDouble(number2.getText());
c = a/b;
result.setText("The Answer is "+String.valueOf(c));
}
else
{
a = Double.parseDouble(number1.getText());
b = Double.parseDouble(number2.getText());
c = a%b;
result.setText("The Answer is "+String.valueOf(c));
}
}
}
public class SwingArithmeticOperations {
public static void main(String[] args) {
// TODO code application logic here
Start start = new Start();
}
}