package guo.test;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Test2 extends JFrame implements ActionListener{
//获得屏幕宽度
public static int width = Toolkit.getDefaultToolkit().getScreenSize().width;
//获得屏幕高度
public static int height = Toolkit.getDefaultToolkit().getScreenSize().height;
private JButton button;
public Test2(){
this.setLayout(null);
this.setSize(500,400);
//设置界面位置
this.setLocation((width-500)/2, (height-500)/2);
//设置可以关闭
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置可见性
this.setVisible(true);
//设置不可变大小
button =new JButton("在监听器中刷新");
button.setBounds(150,230,200,25);
button.addActionListener(this);
//将属性添加到界面
this.add(button);
//刷新(线程方法)
this.repaint();
}
//线程方法
@Override
public void actionPerformed(final ActionEvent e) {
// TODO Auto-generated method stub
new Thread(new Runnable() {
@Override
public void run() {
if(e.getSource()==button){
//对界面的操作
button.setText("点我干嘛");
try {
//休眠线程使刷新线程能够启动
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}).start();
//刷新界面
repaint();
}
public static void main(String[] args) {
Test2 t=new Test2();
}
}