JFrame

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
  
public class KeyDemo extends JFrame{
    int index;
    JLabel jl = new JLabel("你好啊~");
    JButton jb = new JButton("点击改变标签上的文字");
    public KeyDemo() {
        //设置界面的布局为边界布局
        this.setLayout(new BorderLayout());
        //设置标签文字的位置在 布局的中间
        this.add(jl, BorderLayout.CENTER);
        //设置按钮在布局的南部
        this.add(jb, BorderLayout.SOUTH);
        //设置窗口的位置和大小
        this.setBounds(350, 100, 200, 120);
        //设置窗口的关闭事件的响应,如果点击关闭按钮,那么就退出
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        //设置窗口的标题
        this.setTitle("窗口");
        //设置窗口是否可见
        this.setVisible(true);
          
        //为按钮注册事件响应,有了这句代码就能让按钮能够响应点击事件了
        jb.addActionListener(new MyActionListener());
    }
    public static void main(String[] args) {
        //实例化窗口对象
        new KeyDemo();
    }
    //实现动作Listener接口。实现里面的actionPerformed方法
    class MyActionListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            index++;
            jl.setText("你点击了"+index+"次按钮");
        }
    }
}

  

原文地址:https://www.cnblogs.com/nicebaby/p/6273006.html