中国地质大学北京信息管理与信息系统——Java程序设计第二次上机part5计算器改进

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);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值