Java怎么实现一个窗口在用户3秒后不进行任何操作的情况下自动关闭啊。最好有例子代码啊,求教了啊!!
答案:2 悬赏:80 手机版
解决时间 2021-11-26 03:10
- 提问者网友:我们很暧昧
- 2021-11-25 23:32
Java怎么实现一个窗口在用户3秒后不进行任何操作的情况下自动关闭啊。最好有例子代码啊,求教了啊!!
最佳答案
- 五星知识达人网友:北城痞子
- 2021-11-26 00:55
单独显示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();
}
}
全部回答
- 1楼网友:十年萤火照君眠
- 2021-11-26 01:55
简单写了个,你参考下吧。
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;
}
});
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;
}
});
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();
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯