Java怎么实现一个窗口在用户3秒后不进行任何操作的情况下自动关闭啊。最好有例子代码啊,求教了啊!!
展开全部
单独显示Dialog略奇怪,所以我用JFrame做例子,原理是一样的
建立一个线程,睡3秒,醒来后就把JFrame窗体关闭。
给JFrame加事件监听,包括所有你想监听的事件,我这里为了简便,只监听了鼠标单击。一旦事件发生,则吵醒那个线程,这样它就睡不足3秒,并且直接进入下一个迭代,继续睡3秒。
直到有一次单击后3秒内没有操作,这时候线程不会触发InterruptedException 异常,就会执行关闭窗体的操作
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
public class DummyTest {
public static void main(String[] args) {
final JFrame frame = new JFrame();
final Thread thread = new Thread(new Runnable() {
public void run() {
while (true) {
try {
Thread.sleep(3000);
frame.dispose();
return;
} catch (InterruptedException e) {}
}
}
});
frame.setSize(400, 400);
frame.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.out.println("clicked");
thread.interrupt();
}
});
frame.setVisible(true);
thread.start();
}
}
建立一个线程,睡3秒,醒来后就把JFrame窗体关闭。
给JFrame加事件监听,包括所有你想监听的事件,我这里为了简便,只监听了鼠标单击。一旦事件发生,则吵醒那个线程,这样它就睡不足3秒,并且直接进入下一个迭代,继续睡3秒。
直到有一次单击后3秒内没有操作,这时候线程不会触发InterruptedException 异常,就会执行关闭窗体的操作
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
public class DummyTest {
public static void main(String[] args) {
final JFrame frame = new JFrame();
final Thread thread = new Thread(new Runnable() {
public void run() {
while (true) {
try {
Thread.sleep(3000);
frame.dispose();
return;
} catch (InterruptedException e) {}
}
}
});
frame.setSize(400, 400);
frame.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.out.println("clicked");
thread.interrupt();
}
});
frame.setVisible(true);
thread.start();
}
}
展开全部
简单写了个,你参考下吧。
import java.awt.Container;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Test014 {
private static int time = 0;
static JFrame f = new JFrame("test");
static Dialog dialog = new Dialog(f, "警告");
static JButton b1 = new JButton(" 妞妞一");
static JButton b2 = new JButton("妞妞二");
static JLabel label = new JLabel("");
static JLabel labe2 = new JLabel("赶紧按哦,要是5秒不按就木有机会啦,忽忽 ");
static JLabel labe3 = new JLabel("");
public static void createDialog() {
dialog.setLayout(new FlowLayout());
dialog.setSize(400, 200);
dialog.add(b1);
dialog.add(b2);
dialog.add(label);
dialog.add(labe2);
dialog.add(labe3);
dialog.setVisible(true);
}
public static void main(String[] args) {
Container con = f.getContentPane();
con.setLayout(new FlowLayout());
JButton b0 = new JButton("打开对话框");
con.add(b0);
f.setSize(600, 400);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b0.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
label.setText("你按了 妞妞一 ");
time = 0;
createDialog();
}
});
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
label.setText("你按了 妞妞一 ");
time = 0;
}
});
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
label.setText("你按了 妞妞二 ");
time = 0;
}
});
/**
* 创建一个线程来做定时器,呵呵,时间到了就关闭Frame
*/
new Thread(new Runnable() {
@Override
public void run() {
boolean flag = true;
while (flag) {
try {
Thread.sleep(1000);
time++;
labe3.setText("计时开始:" + time + " s");
if (time > 5) {
dialog.setVisible(false);
dialog.disable();
dialog = null;
flag = false;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}) {
}.start();
}
}
import java.awt.Container;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Test014 {
private static int time = 0;
static JFrame f = new JFrame("test");
static Dialog dialog = new Dialog(f, "警告");
static JButton b1 = new JButton(" 妞妞一");
static JButton b2 = new JButton("妞妞二");
static JLabel label = new JLabel("");
static JLabel labe2 = new JLabel("赶紧按哦,要是5秒不按就木有机会啦,忽忽 ");
static JLabel labe3 = new JLabel("");
public static void createDialog() {
dialog.setLayout(new FlowLayout());
dialog.setSize(400, 200);
dialog.add(b1);
dialog.add(b2);
dialog.add(label);
dialog.add(labe2);
dialog.add(labe3);
dialog.setVisible(true);
}
public static void main(String[] args) {
Container con = f.getContentPane();
con.setLayout(new FlowLayout());
JButton b0 = new JButton("打开对话框");
con.add(b0);
f.setSize(600, 400);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b0.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
label.setText("你按了 妞妞一 ");
time = 0;
createDialog();
}
});
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
label.setText("你按了 妞妞一 ");
time = 0;
}
});
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
label.setText("你按了 妞妞二 ");
time = 0;
}
});
/**
* 创建一个线程来做定时器,呵呵,时间到了就关闭Frame
*/
new Thread(new Runnable() {
@Override
public void run() {
boolean flag = true;
while (flag) {
try {
Thread.sleep(1000);
time++;
labe3.setText("计时开始:" + time + " s");
if (time > 5) {
dialog.setVisible(false);
dialog.disable();
dialog = null;
flag = false;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}) {
}.start();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询