定时器中,定义的任务是每10秒弹出一个窗口,窗口停留3秒后自动消失。
代码如下:
package kankan;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.Timer;
//判断是否在线
public class CloseWindowIn5 extends JFrame{
private JButton button=new JButton("签到")
public void window(){
this.setSize(300, 400);
this.setLocation(40, 50);
this.setVisible(true);
setLayout(null);
button.setBounds(110,180,70,70);
add(button);
button.addActionListener(new ActionListen
public void actionPerformed(ActionEvent
System.out.println(“在线”);
}
});
}
public void run() {
try {
Thread.sleep(5000);
this.dispose();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
当我点击button时,他将会在DOS窗口下打印在线。但是当我不点击时他将会打印不在线,那么这个功能怎么实现? java 界面
[解决办法]
对楼主的代码改造完了。。应该可以完成要求了。。
另外PS下,那叫console控制台,不叫dos窗口
package com;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class Test extends JFrame {
private JButton button = new JButton("签到");
public boolean flag=false;
public Test() {
this.setSize(300, 400);
this.setLocation(40, 50);
this.setVisible(true);
setLayout(null);
button.setBounds(110, 180, 70, 70);
add(button);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("在线");
flag=true;
}
});
}
public void run() {
try {
Thread.sleep(5000);
this.dispose();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
Test test = new Test();
while(true){
test.flag=false;
test.show(true);
try {
Thread.currentThread().sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
test.show(false);
if(!test.flag){
System.out.println("不在线");
}
try {
Thread.currentThread().sleep(10000);
System.out.println("等待10秒");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
}
}