gpt4 book ai didi

java - JDialog 闪烁/闪烁作为通知

转载 作者:行者123 更新时间:2023-12-02 00:26:52 26 4
gpt4 key购买 nike

我正在使用 JDialog 在应用程序的右下角显示通知。我最多显示 4 条通知,最新的通知位于顶部。通知是根据服务器的内容显示的,因此可能只有 1 个或 3 个通知,最多 4 个。这会导致 JDialog 的大小调整,而调整大小会导致 JDialog 的位置发生变化,以便始终在右下角对齐。更改每个新通知收入的位置会导致 JDialog 闪烁。

下面是我每次收到新通知时调用的代码:

private void updateDialog()
{

Point p = frame.getLocationOnScreen();
p.x += frame.getWidth()-getWidth()-5;
p.y += frame.getHeight()-getHeight()-25;

setLocation(p);
pack();
repaint();
}

有人遇到过类似的问题吗?你是怎么解决的?关于我应该尝试做什么还有其他建议吗?

最佳答案

  • 与 repaint() 的代码行相反,删除它,

  • 如果 void updateDialog() 中没有其他代码行,那就更好了,否则 pack() 应该包装到 invokeLater();

代码

private void updateDialog() {
setVisible(false);
Point p = frame.getLocationOnScreen();
p.x += frame.getWidth() - getWidth() - 5;
p.y += frame.getHeight() - getHeight() - 25;
setLocation(p);
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
pack();
//repaint(); // useless remove this codeline
setVisible(true);
}
});
}

关于java - JDialog 闪烁/闪烁作为通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9820023/

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