package learning_swing;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Container;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
public class homework3_gaijin {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame jf=new JFrame("符氏计算器,给你\"人生\"的答案!");//jia ren men ,wo emo啦 ,呜呜 我装的^o^
jf.setLayout(new GridLayout(5, 2)); //布局
JLabel jl1 =new JLabel("请输入第一个数");
JLabel jl2 =new JLabel("请输入第二个数");
JTextField jt1=new JTextField();
JTextField jt2=new JTextField();
JRadioButton jia =new JRadioButton("+");
JRadioButton jian =new JRadioButton("-");
JRadioButton chen =new JRadioButton("*");
JRadioButton chu =new JRadioButton("/");
ButtonGroup giao=new ButtonGroup(); //设置成单选
giao.add(jia);giao.add(jian);giao.add(chen);giao.add(chu);
JLabel jl3=new JLabel("结果为");
JTextField jt3=new JTextField();
jia.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Double a=Double.parseDouble(jt1.getText());
Double b=Double.parseDouble(jt2.getText());
Double c=a+b;
jt3.setText(""+c);
}
});
jian.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Double a=Double.parseDouble(jt1.getText());
Double b=Double.parseDouble(jt2.getText());
Double c=a-b;
jt3.setText(""+c);
}
});
chen.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Double a=Double.parseDouble(jt1.getText());
Double b=Double.parseDouble(jt2.getText());
Double c=a*b;
jt3.setText(""+c);
}
});
chu.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Double a=Double.parseDouble(jt1.getText());
Double b=Double.parseDouble(jt2.getText());
if(b==0) {
new bc();
jt3.setText("");
}else {
Double c=a/b;
jt3.setText(""+c);
}
}
});
//加入小东西到jf里,
jf.add(jl1);jf.add(jl2);
jf.add(jt1);jf.add(jt2);
jf.add(jia);jf.add(jian);
jf.add(chen);jf.add(chu);
jf.add(jl3);jf.add(jt3);
jf.setBounds(500,400,300,400);
jf.setVisible(true);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
class bc extends JDialog{
public bc() {
this.setVisible(true);this.setBounds(500,250,200,80);Container c = this.getContentPane();
JLabel l1= new JLabel("分母不能为0",JLabel.CENTER);
c.add(l1);
}
}
中国地质大学北京信息管理与信息系统——Java程序设计第二次上机part5计算器改进
最新推荐文章于 2024-11-04 20:33:37 发布