下面的两个程序,为何第一个不能达到预期的效果,而第二个可以?
还请各位指点!程序1:
package swingTimer;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
public class NakeTimer implements ActionListener{ int count = 0;
Timer timer;
public NakeTimer(){ timer = new Timer(1000,this);
timer.start();
}
public static void main(String[] args){
new NakeTimer();
}
public void actionPerformed(ActionEvent e){
count++;
System.out.println(count+"Hello!");
}
}程序2:
package swingTimer;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
public class NakeTimer implements ActionListener{
JFrame f=null;
JLabel label = null;
int count = 0;
Timer timer;
public NakeTimer(){
f=new JFrame();
label = new JLabel();
f.getContentPane().add(label);
f.setSize(200,100);
f.setLocation(300,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
timer = new Timer(1000,this);
timer.start();
}
public static void main(String[] args){
new NakeTimer();
}
public void actionPerformed(ActionEvent e){
count++;
label.setText(count+"Hello!");
}
}
还请各位指点!程序1:
package swingTimer;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
public class NakeTimer implements ActionListener{ int count = 0;
Timer timer;
public NakeTimer(){ timer = new Timer(1000,this);
timer.start();
}
public static void main(String[] args){
new NakeTimer();
}
public void actionPerformed(ActionEvent e){
count++;
System.out.println(count+"Hello!");
}
}程序2:
package swingTimer;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
public class NakeTimer implements ActionListener{
JFrame f=null;
JLabel label = null;
int count = 0;
Timer timer;
public NakeTimer(){
f=new JFrame();
label = new JLabel();
f.getContentPane().add(label);
f.setSize(200,100);
f.setLocation(300,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
timer = new Timer(1000,this);
timer.start();
}
public static void main(String[] args){
new NakeTimer();
}
public void actionPerformed(ActionEvent e){
count++;
label.setText(count+"Hello!");
}
}
解决方案 »
- 求助,JTable对象建立好后还能使用getToolTipText(MouseEvent event)吗?
- 如何获知socket client端已经断开连接
- JAVA子类对象如何调用父类被overridden的函数?
- 关于用二进制表示数据的问题
- 小弟初学,求一算法
- 实现:在键盘上输入一个数字序列并存储在Vector中,最后在屏幕上打印出每位数字相加的结果。编译可以通过,最后出不来结果。
- 想做个网站,不知道怎么入手,请教!
- 求助,初学者一个关于数组的问题
- 高分求救:一个关于java.awt.Dialog的问题
- JTable 高难问题............
- 哪有这么干的。 jdom居然不向下兼容。
- 如何生成一个可执行文件
主线程结束了
而第二个中主线程一直在运行
只要往第一个程序里随便加一个无关的JFrame等,不让他自动结束,就可以实现了。