javaGUI小练习

题目:能实现两数加、减、乘、除,有运算、清除、退出功能。如图

这里写图片描述

public class CalTest extends JFrame{

    private TextField firText,secText,reText;
    private JLabel eqLabel;
    private JComboBox jbox;
    private String[] str;
    private Button calb,clearb,exitb;
    private Panel p,p2;

    public static void main(String[] args) {
        new CalTest("运算器");
    }

    public CalTest(){}
    public CalTest(String title){
        setTitle(title);
        setLocation(500, 200);
        setDefaultCloseOperation(3);
        setResizable(false);
        setLayout(new FlowLayout());    
        init();
        pack();
        setVisible(true);
    }

    public void init(){

        p = new Panel();
        p.setSize(300, 300);
        firText = new TextField(5);
        secText = new TextField(5);
        reText = new TextField(5);
        reText.setEditable(false);
        eqLabel = new JLabel("=");

        str = new String[]{"+","-","*","/"};
        jbox = new JComboBox(str);

        p.add(firText);
        p.add(jbox);
        p.add(secText);
        p.add(eqLabel);
        p.add(reText);
        add(p);

        p2 = new Panel();
        calb = new Button("cal");

        calb.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
        //getSelectedItem()返回JComboBox所选项,返回Objection类型
                String sign = (String) jbox.getSelectedItem();

                if(sign.equals("+")){
                    int num = Integer.parseInt(firText.getText());
                    int num2 = Integer.parseInt(secText.getText());
                    int add = num+num2;
                    reText.setText(add+"");
                }else if(sign.equals("-")){
                    int num = Integer.parseInt(firText.getText());
                    int num2 = Integer.parseInt(secText.getText());
                    int sup = num-num2;
                    reText.setText(sup+"");
                }else if(sign.equals("*")){
                    int num = Integer.parseInt(firText.getText());
                    int num2 = Integer.parseInt(secText.getText());
                    int sel = num*num2;
                    reText.setText(sel+"");
                }else{
                    int num = Integer.parseInt(firText.getText());
                    int num2 = Integer.parseInt(secText.getText());
                    int sele = num/num2;
                    reText.setText(sele+"");
                }
            }
        });
        clearb = new Button("clear");
        clearb.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                firText.setText(null);
                secText.setText(null);
                reText.setText(null);
            }
        });

        exitb = new Button("exit");
        exitb.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.exit(0);
            }
        });

        p2.add(calb);
        p2.add(clearb);
        p2.add(exitb);
        add(p2,BorderLayout.SOUTH);

    }
}
  • 6
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值