javaGUI小练习

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

javaGUI小练习_第1张图片

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);

    }
}

你可能感兴趣的:(javaGUI小练习)