- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
第一遍似乎要么使可绘制区域无效,要么绘制背景。第二遍渲染菜单。如果有任何延迟(如下面的示例所示),则会出现灰色方 block 闪烁效果。
这是 Linux 上的 JDK8。
如何停止这种闪烁效果?
public class MenuTester {
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setBounds(100, 100, 300, 200);
final JButton button = new JButton("Show Menu");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
final JPopupMenu popupMenu = new JPopupMenu();
popupMenu.add(new JMenuItem("aaaa"));
popupMenu.add(new JMenuItem("bbbb"));
popupMenu.add(new JMenuItem("cccc"));
popupMenu.setLocation(100, 100);
popupMenu.setVisible(true);
try {
Thread.sleep(2000); // Leave enough time to clearly see the ?invalidated/background? area.
} catch (InterruptedException ex) {
// Nothing to do
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// Hide after 1 second
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
// Nothing to do
}
popupMenu.setVisible(false);
}
});
}
});
frame.add(button);
frame.setVisible(true);
}
}
最佳答案
据我所知,Swing 在显示窗口时存在“延迟”,这可能与操作系统实现帧与 native 消息和事件队列的连接之间的时间有关,但这纯粹是观察
我拿了你的代码,只需将框架的创建包装到 EventQueue.invokeLater
中就能够获得类似的行为
根据系统的启动和配置,您将在不同的系统上获得不同的结果
What is the event that causes the window to be rendered in the first pass?
我所做的就是获取您的代码并将 UI 的创建包装在 EventQueue.invokeLater
中,例如...
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class MenuTester {
public static void main(String[] args) {
new MenuTester();
}
public MenuTester() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
final JFrame frame = new JFrame();
frame.setBounds(100, 100, 300, 200);
final JButton button = new JButton("Show Menu");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
final JPopupMenu popupMenu = new JPopupMenu();
popupMenu.add(new JMenuItem("aaaa"));
popupMenu.add(new JMenuItem("bbbb"));
popupMenu.add(new JMenuItem("cccc"));
popupMenu.setLocation(100, 100);
popupMenu.setVisible(true);
// try {
// Thread.sleep(2000); // Leave enough time to clearly see the ?invalidated/background? area.
// } catch (InterruptedException ex) {
// // Nothing to do
// }
// SwingUtilities.invokeLater(new Runnable() {
// @Override
// public void run() {
// // Hide after 1 second
// try {
// Thread.sleep(1000);
// } catch (InterruptedException ex) {
// // Nothing to do
// }
// popupMenu.setVisible(false);
// }
// });
}
});
frame.add(button);
frame.setVisible(true);
}
});
}
}
And is there a way to render the window as transparent during that first pass and opaque during the 2nd?
这不是一个新问题,自从我在 Java 1.3 中开始使用 Swing 以来,这一直是这种情况。您所问的意味着您知道油漆 channel 何时完成并完成。 Swing 并不完全愚蠢,它可以做出一些明智的决定来优化渲染过程(例如不绘制可见的组件)
另一个问题是,对于 JPopupMenu
,您实际上并不知道它是否已显示在窗口中(或者只是作为组件显示在玻璃 Pane 上),因此整个事情非常复杂
关于java - 为什么 JPopupMenu 在渲染之前要经过 2 遍 UIThread?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45155842/
我有一个 Java 小程序,它将由用户必须与之交互的几个弹出菜单组成。但是,添加时 JPopupMenu 不会显示。这是我的代码: public class Parser extends JApple
我正在尝试创建带有两个弹出菜单的系统托盘图标,一个应该用鼠标左键调用,另一个应该用鼠标右键调用。 AWT 提供了创建 PopupMenu 的简单方法,但在不创建不可见的 JFrame 或其他内容的情况
在上一个问题中,我向社区询问为什么我的 JPopupMenu 没有出现在屏幕上。 我无法想出一个简单的、可运行的、可编译的示例。 所以,这就是我为你们所做的: 区域太小,无法绘制弹出窗口吗? 我希望我
我有下一个结构:JPopupMenu包含JPanel,其中包含JMenuItems。问题是,我无法使用它,因为当鼠标进入任何菜单项时,JPopupMenu就会消失。 SSCCE: public cla
这是我的 JPopupMenu 的代码以及我如何添加它,当我右键单击表格时它应该做出响应: JMenuItem deleteRows = new JMenuItem("Delete Row")
我有一个弹出菜单,其中包含一个带有图标 (I) 的 JMenuItem 和一个 JList。见下图。 ------------------------ | I | Clear |
我正在制作一个程序,它有一个带有两个按钮的弹出菜单,其中一个按钮应该关闭弹出菜单,但我不知道如何做到这一点,而且谷歌搜索也不太顺利。 我尝试过使用 popup.hide() 但菜单不会回来,尽管当我尝
我想在翻转事件时更改 jMenuItems 的背景颜色: 现在它是蓝色的,我想要白色的,我该怎么办? (我使用netbeans GUI) 最佳答案 你可以使用类似的东西: UIManager.put(
我在 JTable 上的弹出菜单以及该 JTable 允许多个间隔选择这一事实上遇到问题。我将详细解释我的情况,希望尽可能清楚。 我有一个基本数据类,我们称之为 Item,带有一个字符串 id(名称)
我有一个用于运行锦标赛的 Java 应用程序,在其中我构建了一个自动建议功能,该功能从数据库中获取名称并将其显示在 JPopupMenu 中。我无法根据需要复制这个错误,但偶尔一个 JPopupMen
我编写了一个简单的程序来了解 JPopupMenu 的工作原理。但我的代码有问题,它没有正确显示弹出菜单。请问谁能告诉我原因吗? public class PopUpMenu extends JFra
有关 JPopupMenu 行为的问题。我希望 JPopupMenu 在出现时不要失去焦点。此外,当 JPopupMenu 获得焦点时,用户应该能够单击/更新 Applet 的其他部分。 这可能吗?原
因此,对于我的学校项目,我正在创建一个类图制作器。我已经完成了 95%,我所需要的就是让 Jpopup 菜单出现。在核心中我有 3 个文件。扩展 JFrame 的 ApplicationModel、扩
我有一个弹出菜单,当鼠标悬停在组件上时会打开该菜单。现在我希望能够在弹出窗口仍然打开时开始拖动组件,但弹出菜单关闭事件总是消耗第一次鼠标单击。有解决方法吗? 最佳答案 public clas
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 7 年前。 Improve this ques
我需要添加右键JPopupMenu到世界风显示。世界风显示位于 JPanel 。我几乎只是复制了ApplicationTemplate.AppPanel的成员变量和方法。 World Wind 示例中
下面我准备了一个小测试用例。我的问题是当我右键单击窗口时。 JPopupMenu 出现,但如果我单击 JWindow 菜单外的任何地方,它不会消失。我必须单击窗口上的某处才能摆脱它,这不是预期的行为。
我正在用 Java Swing 编写一个程序,在这个程序中会有一个搜索栏(这将是一个 JTextField),当在此字段中输入内容时,我想直接在弹出菜单中显示搜索结果在搜索栏下方,就像谷歌一样。我不必
如何获取当前显示在屏幕上的所有 JPopupMenu 组件的列表。 或者获取所有 JPopupMenu 组件,然后可以通过可见性、有效性等进行过滤。 我需要这个来编写测试。卡在 JPopupMenu-
我需要删除 JPopupMenu 中的背景和 Border。 JPopupMenu 应该是完全透明的。覆盖 paintComponent 不会给出肯定的结果。我也尝试在 BasicMenuItemUI
我是一名优秀的程序员,十分优秀!