内部类的使用

import java.awt.*;
import java.awt.event.*;

public class Main{
    public static void main(String[] args) {
        new Calculate().loadFrame();
    }
}

class Calculate extends Frame {

    Frame frame = new Frame("calculate");
    TextField textField1 = new TextField();
    Label label1 = new Label("+");
    TextField textField2 = new TextField();
    Button label2 = new Button("=");
    TextField textField3 = new TextField();

    public void loadFrame() {
        add(textField1);
        add(label1);
        add(textField2);
        add(label2);
        add(textField3);

        frame.pack();
        setVisible(true);
        setLayout(new FlowLayout());
        setSize(200,200);
        setLocation(200,200);

        label2.addActionListener(new MyActionListener());

        new CloseFrame().close(this);
    }

    // 内部类
    class MyActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            int n1 = Integer.parseInt(textField1.getText());
            int n2 = Integer.parseInt(textField2.getText());
            textField3.setText(""+(n1+n2));
        }
    }
}

效果图
内部类的使用

上一篇:得到文本框输入的内容


下一篇:QML TextField输入的文本传输到C++