【JavaGUI设计简易计算器界面】

//附:更加高级一点的https://blog.csdn.net/pointer_5/article/details/103073511请见。
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * GUI设计实验-梁达林
 * 日期:2019.11.10
 * 项目:简易计算机页面设计+事件处理
 */

public class Experiment4_1 {
    public static void main(String[] args) {
        Calculator calculator = new Calculator();
    }
}

class Calculator extends JFrame {
    JFrame jFrame;
    JLabel jLabel1, jLabel2, jLabel3, jLabel4;
    JTextField jTextField1, jTextField2, jTextField3;
    JButton jButton1, jButton2, jButton3;

    public Calculator() {

        /*创建顶层容器并设定相关参数*/
        jFrame = new JFrame("简易计算器");
        jFrame.setVisible(true);
        jFrame.setSize(300, 450);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setLayout(null);

        /*创建四个JLabel组件并设定相关参数*/
        jLabel1 = new JLabel("简易计算器");
        jLabel2 = new JLabel("运算数一");
        jLabel3 = new JLabel("运算数二");
        jLabel4 = new JLabel("运算结果");
        jLabel1.setSize(60, 15);
        jLabel2.setSize(60, 15);
        jLabel3.setSize(60, 15);
        jLabel4.setSize(60, 15);
        jLabel1.setLocation(110, 30);
        jLabel2.setLocation(50, 80);
        jLabel3.setLocation(50, 130);
        jLabel4.setLocation(50, 180);

        /*创建三个JTextField组件并设定相关参数*/
        jTextField1 = new JTextField(10);
        jTextField2 = new JTextField(10);
        jTextField3 = new JTextField(15);
        jTextField1.setSize(100, 20);
        jTextField2.setSize(100, 20);
        jTextField3.setSize(100, 20);
        jTextField1.setLocation(120, 80);
        jTextField2.setLocation(120, 130);
        jTextField3.setLocation(120, 180);

        /*创建三个JButton组件并设定相关参数*/
        jButton1 = new JButton("相加");
        jButton2 = new JButton("相减");
        jButton3 = new JButton("全部清零");
        jButton1.setSize(70, 20);
        jButton2.setSize(70, 20);
        jButton3.setSize(90, 20);
        jButton1.setLocation(50, 230);
        jButton2.setLocation(150, 230);
        jButton3.setLocation(100, 280);

        /*将组件添加到容器中*/
        jFrame.getContentPane().add(jLabel1);
        jFrame.getContentPane().add(jLabel2);
        jFrame.getContentPane().add(jLabel3);
        jFrame.getContentPane().add(jLabel4);
        jFrame.getContentPane().add(jTextField1);
        jFrame.getContentPane().add(jTextField2);
        jFrame.getContentPane().add(jTextField3);
        jFrame.getContentPane().add(jButton1);
        jFrame.getContentPane().add(jButton2);
        jFrame.getContentPane().add(jButton3);
        init();
    }

    void init() {
        /*事件处理*/
        jButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jTextField3.setText(null);
                jTextField3.setText(String.valueOf(Integer.valueOf(jTextField1.getText()) + Integer.valueOf(jTextField2.getText())));
            }
        });
        jButton2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jTextField3.setText(null);
                jTextField3.setText(String.valueOf(Integer.valueOf(jTextField1.getText()) - Integer.valueOf(jTextField2.getText())));
            }
        });
        jButton3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jTextField1.setText(null);
                jTextField2.setText(null);
                jTextField3.setText(null);
            }
        });
    }
}

运行结果如图:(如果运行看不到界面的话,稍微拉大一点点即可)

【JavaGUI设计简易计算器界面】_第1张图片

附:更加高级一点的https://blog.csdn.net/pointer_5/article/details/103073511请见。

你可能感兴趣的:(JavaSE)