-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddSubMulDivGUIVersionTwo.java
More file actions
100 lines (96 loc) · 2.83 KB
/
AddSubMulDivGUIVersionTwo.java
File metadata and controls
100 lines (96 loc) · 2.83 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
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class AddSubMulDivGUIVersionTwo extends JFrame implements ActionListener{
JButton add;
JButton sub;
JButton mul;
JButton div;
JTextField num1;
JTextField num2;
JLabel fn;
JLabel sn;
static int a, b, c;
static float d, e, f;
AddSubMulDivGUIVersionTwo(){
super("Playing with numbers again");
setSize(300, 220);
setExtendedState(JFrame.MAXIMIZED_BOTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane=new JPanel();
add= new JButton("Add");
add.addActionListener(this);
sub=new JButton("Subtract");
sub.addActionListener(this);
mul=new JButton("Multiply");
mul.addActionListener(this);
div= new JButton("Divide");
div.addActionListener(this);
num1=new JTextField(5);
num2=new JTextField(5);
fn=new JLabel("First Number");
sn=new JLabel("Second Number");
BorderLayout bord=new BorderLayout();
setLayout(bord);
pane.add(fn);
pane.add(num1);
pane.add(sn);
pane.add(num2);
pane.add(add);
pane.add(sub);
pane.add(mul);
pane.add(div);
add (pane);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent evt){
Object source=evt.getSource();
if (source == add){
int a=Integer.parseInt(num1.getText());
int b=Integer.parseInt(num2.getText());
c=a+b;
JOptionPane.showMessageDialog(null, "The sum of two given numbers is"+" "+c, "Answer", JOptionPane.PLAIN_MESSAGE);
num1.setText("");
num2.setText("");
}
else if (source == sub){
int a=Integer.parseInt(num1.getText());
int b=Integer.parseInt(num2.getText());
c=a-b;
JOptionPane.showMessageDialog(null, "The difference of two given numbers is"+" "+c, "Answer", JOptionPane.PLAIN_MESSAGE);
num1.setText("");
num2.setText("");
}
else if (source == mul){
int a=Integer.parseInt(num1.getText());
int b=Integer.parseInt(num2.getText());
c=a*b;
JOptionPane.showMessageDialog(null, "The product of two given numbers is"+" "+c, "Answer", JOptionPane.PLAIN_MESSAGE);
num1.setText("");
num2.setText("");
}
else if (source == div){
float e=Float.parseFloat(num1.getText());
float f=Float.parseFloat(num2.getText());
d=e/f;
JOptionPane.showMessageDialog(null, "The quotient of two given numbers is"+" "+d,"Answer", JOptionPane.PLAIN_MESSAGE);
num1.setText("");
num2.setText("");
}
repaint();
}
void setLookAndFeel() {
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
}
catch (Exception exc) {
System.err.println("Couldn't use the system "+"look and feel:"+exc);
}
}
public static void main(String[] args){
AddSubMulDivGUIVersionTwo asmdguivt=new AddSubMulDivGUIVersionTwo();
asmdguivt.setLookAndFeel();
}
}