我需要创建一个简单的延迟(3-5秒),以便在处理之前可以读取窗口。
我已经在网上找到了一百万个例子,但他们似乎都给了我同样的问题。我的代码:
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import javax.swing.JLabel;
public class GrantedPane extends JPanel {
private static final long serialVersionUID = 1L;
public GrantedPane() {
JFrame frame = new JFrame();
frame.setUndecorated(true);
frame.setSize(400, 150);
frame.setContentPane(this);
frame.setLocationRelativeTo(null);
frame.setSize(400, 80);
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
setBorder(new CompoundBorder(BorderFactory.createLineBorder(Color.WHITE), new EmptyBorder(20, 20, 20, 20)));
JLabel grantTitle = new JLabel(" Key Accepted ");
grantTitle.setForeground(Color.YELLOW);
grantTitle.setFont(new Font("Enter The Grid", Font.PLAIN, 32));
grantTitle.setAlignmentX(Component.CENTER_ALIGNMENT);
add(grantTitle);
frame.setVisible(true);
delay(3);
frame.dispose();
}
@Override
protected void paintComponent(Graphics g) {
int w = getWidth(), h = getHeight();
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
GradientPaint gp = new GradientPaint(0, 0, Color.BLACK, 0, h, Color.GRAY);
g2d.setPaint(gp);
g2d.fillRect(0, 0, w, h);
}
void delay (int sec){
try {
TimeUnit.SECONDS.sleep(sec);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
所以这个问题(以及我尝试过的所有其他类型的技术,包括在绝望行为中嵌套的FOR循环......)就是窗口永远不会出现。
我假设它是因为睡眠发生在frame.setVisible()
生效之前。我也试过wait()
同样的效果。
有人可以帮我解决这个问题吗?
编辑 -
这是我如何实例化它(在另一个类中)。在此之前的屏幕变为绿色然后启动"接受" window。该程序有8个类,否则我只是扔掉了整个东西。值得注意的是,我得到了一个未使用过的'对实例化的警告,但我不确定原因。
void validCode() {
color1 = Color.GREEN;
color2 = Color.GREEN;
repaint();
GrantedPane granted = new GrantedPane();
}
答案 0 :(得分:2)
你有没有在任何地方实例化这个类?添加了主要方法和更新的导入。工作正常。
xts
答案 1 :(得分:1)
这里没有答案:从用户体验方面来看,你的想法没有意义。当然,从技术角度来看,它提供了一些学习方法。但除此之外,你正在用这种方法浪费你的时间。
不是自动显示和删除框架,而是设置模态选项对话框。只需要用户在完成阅读后单击按钮。
你知道,这是超时的问题:你从不让所有你的用户正确如果延迟时间足够长,以便99%的用户可以阅读该消息,则可能有50%的人会抱怨您的UI让他们等待的时间太长。 (1%是人们分心,从未注意到自闭窗口)。
最后你想要的东西:感觉你的应用程序的用户正在放慢速度。有众所周知的最佳实践向用户呈现消息。自动显示和删除它们不在最佳实践列表中。
答案 2 :(得分:1)
所以这个问题(以及我尝试过的所有其他类型的技术,包括绝望行为中的嵌套FOR循环......)都是窗口永远不会出现。
这是因为您似乎尝试过的每个解决方案都阻止了EDT。有关详细信息,请参阅Concurrency in Swing;有关Swing中用户可以安全使用的典型解决方案,请参阅How to use Swing Timers <(不会阻止EDT并可用于安全更新UI)
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
public class GrantedPane extends JPanel {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new GrantedPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Timer timer = new Timer(3000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.dispose();
}
});
timer.start();
}
});
}
private static final long serialVersionUID = 1L;
public GrantedPane() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
setBorder(new CompoundBorder(BorderFactory.createLineBorder(Color.WHITE), new EmptyBorder(20, 20, 20, 20)));
JLabel grantTitle = new JLabel(" Key Accepted ");
grantTitle.setForeground(Color.YELLOW);
grantTitle.setFont(new Font("Enter The Grid", Font.PLAIN, 32));
grantTitle.setAlignmentX(Component.CENTER_ALIGNMENT);
add(grantTitle);
}
@Override
protected void paintComponent(Graphics g) {
int w = getWidth(), h = getHeight();
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
GradientPaint gp = new GradientPaint(0, 0, Color.BLACK, 0, h, Color.GRAY);
g2d.setPaint(gp);
g2d.fillRect(0, 0, w, h);
}
}