-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram10.java
More file actions
71 lines (57 loc) · 2.21 KB
/
Copy pathProgram10.java
File metadata and controls
71 lines (57 loc) · 2.21 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
/**Write a program that creates a user interface to perform integer divisions. The user enters two
numbers in the text fields, Num1 and Num2. The division of Num1 and Num2 is displayed in
the Result field when the Divide button is clicked. If Num1 or Num2 were not an integer, the
program would throw a NumberFormatException. If Num2 were Zero, the program would
throw an Arithmetic Exception Display the exception in a message dialog box. */
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Program10 implements ActionListener{
public static void main(String[] args){
JFrame mainframe =new JFrame("calculator.exe");
mainframe.setSize(500,500);
JLabel label1=new JLabel("num1");
JLabel label2=new JLabel("num2");
JTextField tf1=new JTextField();
JTextField tf2=new JTextField();
JTextField res=new JTextField();
JButton calc= new JButton("calculate");
JButton clr= new JButton("clear");
label1.setBounds(0,0,100,50);
label2.setBounds(0,60,100,50);
tf1.setBounds(110,0,100,50);
tf2.setBounds(110,60,100,50);
res.setBounds(0,80,100,50);
calc.setBounds(0,140,100,30);
clr.setBounds(0,180,100,30);
calc.addActionListener(e->{
try{
int a= Integer.parseInt(tf1.getText());
int b= Integer.parseInt(tf2.getText());
int soln= a/b;
res.setText("result: "+soln);
}
catch(ArithmeticException ex){
res.setText("arithmetic exeption ");
}
catch(NumberFormatException ex){
res.setText("number format exception ");
}
});
clr.addActionListener(e->{
tf1.setText("");
tf2.setText("");
});
mainframe.add(label1);
mainframe.add(label2);
mainframe.add(tf1);
mainframe.add(tf2);
mainframe.add(calc);
mainframe.add(clr);
mainframe.setLayout(null);
mainframe.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e){
}
}