gpt4 book ai didi

java - thread.sleep(5000) 不起作用

转载 作者:行者123 更新时间:2023-12-02 03:29:54 25 4
gpt4 key购买 nike

实际上我正在开发一个项目,该项目在单击按钮时生成一个对话,并通过 Thread.sleep(5000) 在 5 秒内处理它我在运行方法下写了以下内容

void showpopup(String p, String t) throws IOException {
JPanel app = new JPanel();
popup = new JFrame();
// app.setSize(300,400);
GridBagConstraints c1 = new GridBagConstraints();
app.setLayout(new GridBagLayout());
app.setVisible(true);
app.setBackground(new Color(39, 170, 225));

popup.setUndecorated(true);
popup.setSize(300, 100);
popup.setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
JLabel header = new JLabel();
header.setText(p);
c1.gridx = 0;
c1.gridy = 0;
c1.weightx = 1.0f;
c1.weighty = 1.0f;
c1.insets = new Insets(5, 5, 5, 5);
c1.fill = GridBagConstraints.BOTH;
header.setFont(new Font("comfortaa", Font.BOLD, 15));
app.add(header, c1);

constraints.gridx = 0;
constraints.gridy = 0;
constraints.weightx = 0.3f;
constraints.weighty = 0.3f;
constraints.fill = GridBagConstraints.BOTH;

popup.add(app, constraints);

BufferedImage Butico = ImageIO.read(new File("C:/Users/utkarsh/Desktop/close.png"));
JButton close = new JButton(new ImageIcon(Butico));
close.setContentAreaFilled(false);
close.setBorder(BorderFactory.createEmptyBorder());
c1.gridx = 1;
c1.weightx = 0f;
c1.weighty = 0f;
c1.insets = new Insets(5, 5, 5, 5);
c1.fill = GridBagConstraints.NONE;
c1.anchor = GridBagConstraints.NORTH;
close.addMouseListener(new MouseAdapter() {

@Override
public void mouseClicked(MouseEvent arg0) {
popup.dispose();
}
});
app.add(close, c1);

JPanel cont = new JPanel(new GridBagLayout());
// cont.setSize(arg0, arg1);
String a = "hello";
JLabel text = new JLabel("<HtML>" + t);
text.setAlignmentX(Component.LEFT_ALIGNMENT);
cont.setVisible(true);
cont.setBackground(new Color(241, 242, 242));
c1.gridx = 0;
c1.gridy = 0;
c1.weightx = 1.0f;
c1.weighty = 1.0f;
c1.insets = new Insets(5, 5, 5, 5);
c1.anchor = GridBagConstraints.NORTH;
c1.fill = GridBagConstraints.BOTH;
cont.add(text, c1);
constraints.gridx = 0;
constraints.gridy += 1;
constraints.weightx = 2.5f;
constraints.weighty = 2.5f;
constraints.fill = GridBagConstraints.BOTH;
popup.add(cont, constraints);
text.setFont(new Font("Comfortaa", Font.PLAIN, 12));
text.setForeground(Color.DARK_GRAY);
header.setForeground(Color.WHITE);
popup.getRootPane().setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, new Color(188, 188, 188)));

popup.setVisible(true);
Dimension s = Toolkit.getDefaultToolkit().getScreenSize();
Insets th = Toolkit.getDefaultToolkit().getScreenInsets(popup.getGraphicsConfiguration());
popup.setLocation(s.width - popup.getWidth() - 5, s.height - th.bottom - popup.getHeight() - 5);
popup.setAlwaysOnTop(true);
new Thread() {

@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
popup.dispose();
}

}

}.start();
}

现在,当我单击按钮时,它工作正常,但对于多个按钮单击,会出现多个 Jdialog 弹出窗口5 秒后只有最后一个被处理

那么我必须做出哪些更改才能关闭每个 JDialog 框

最佳答案

弹出窗口不是此方法的本地窗口

popup = new JFrame();

因此该代码不是线程安全的。

每次单击,您都会替换 popup对象与新创建的对象,因此 popup.dispose();将处理掉最后一个。

关于java - thread.sleep(5000) 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38236679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com