用java编写一个窗口一,窗口上有一个按钮,按钮记录单击按钮的次数.

2024-12-19 22:40:02
推荐回答(1个)
回答1:

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class JFrameTest extends JFrame{

private JButton button;
private Container content;
private int count=0;
public JFrameTest(){

super("JFrameTest");
this.setBounds(100,100,300,200);
content=this.getContentPane();
content.setLayout(new FlowLayout());

button=new JButton("还没有点击哦");
content.add(button);
button.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {
count++;
button.setText("点击了"+count+"次");
}

});

this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main(String[] args){
new JFrameTest();
}

}