package hanyudabaobei.lesson1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GUI_homework
{
public static void main(String[] args) {
JFrame frame=new JFrame("Roye_ack Calculator");
frame.setBounds(400,500,350,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p=new JPanel();
p.setLayout(new FlowLayout(FlowLayout.LEFT,10,20));
JLabel la=new JLabel("+");
JLabel lb=new JLabel("=");
TextField text_a=new TextField(8);
TextField text_b=new TextField(8);
TextField text_c=new TextField(8);
p.add(text_a);
p.add(la);
p.add(text_b);
p.add(lb);
p.add(text_c);
JButton b1=new JButton("加");
JButton b2=new JButton("减");
JButton b3=new JButton("乘");
JButton b4=new JButton("除");
JButton b5=new JButton("清除");
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(b5);
frame.add(p);
//给 加法 按钮建立事件
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String a= text_a.getText();
String b= text_b.getText();
String c=" "+(Integer.parseInt(a)+Integer.parseInt(b));
text_c.setText(c);
}
});
//给 减法 按钮建立事件
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String a=text_a.getText();
String b=text_b.getText();
String c=" "+(Integer.parseInt(a)-Integer.parseInt(b));
text_c.setText(c);
}
});
//给 乘法 按钮建立事件
b3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String a=text_a.getText();
String b=text_b.getText();
String c=" "+(Integer.parseInt(a)*Integer.parseInt(b));
text_c.setText(c);
}
});
b4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String a=text_a.getText();
String b=text_b.getText();
String c=" "+(1.0*Integer.parseInt(a)/Integer.parseInt(b));
text_c.setText(c);
}
});
b5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
text_a.setText("");
text_b.setText("");
text_c.setText("");
}
});
frame.setVisible(true);
}
}