import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Gui01 implements ActionListener{
JButton btn;
public static void main(String[] args) {
Gui01 gui = new Gui01();
gui.go();
}
public void go(){
//创建JFrame
JFrame frame = new JFrame("my first Gui");
//创建按钮
JButton btn = new JButton("click me");
//在window结束时,把程序结束
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//为按钮添加事件监听
btn.addActionListener(this);
//把button添加到frame 的 pane上
frame.getContentPane().add(btn);
//设置frame的大小
frame.setSize(500,500);
//显示frame
frame.setVisible(true);
}
//此方法为事件响应的回调函数
public void actionPerformed(ActionEvent e){
System.out.println("hello world");
try{
btn.setText("i've been clicked.");
}catch(Exception ex){
ex.printStackTrace();
}
}
}
朕日理万机
慕瓜9220888
相关分类