import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Tim extends JFrame{
JPanel pnl;
JButton btn1=new JButton("开始");
JButton btn2=new JButton("停止");
FlowLayout flowLayout=new FlowLayout();
Timer obj;
Class cl;
Tim(){
super("Timer");
men();
action();
}
void men(){
pnl=(JPanel)getContentPane();
pnl.setLayout(flowLayout);
setSize(new Dimension(400,300));
pnl.add(btn1);
pnl.add(btn2);
this.setVisible(true);
}
void action(){
btn1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
obj=new Timer(5000,this);
obj.start();
System.out.println("aaaaaaaaaaaaaaaa");
}
});
btn2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
obj.stop();//这里的obj为null,为什么我在上面属性区创建了引用,在btn1里面声明,影响不到btn2里面来呢?这个问题如何解决啊?
}
});
}
public static void main(String[] args){
Tim cc=new Tim();
}
}