- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的应用程序中有一个弹出菜单,我想将其替换为自定义菜单,以便它与应用程序其余部分的外观相匹配。本质上,我不想在弹出窗口中使用普通菜单项,而是想重用应用程序中其他地方已经存在的组件,让您以“分页”方式而不是使用子菜单在项目层次结构中导航。因此,如果您单击列表中包含子项的项目,则会显示下一页,用所单击项目的子项列表替换列表中的当前项目。
使用“分页”组件的优点是它可以很好地适应应用程序的其余部分(它已经在其他非弹出窗口的地方使用)并且在页面之间导航时有一些漂亮的动画效果.
我遇到的问题是,当显示不同的页面时,分页组件的首选高度会发生变化(列表中的项目数量会发生变化),我希望弹出菜单的高度能够更新,以便它完全适合分页组件, 但到目前为止,我尝试在弹出菜单可见时更新它的高度都失败了。
下面是一个演示问题的示例应用程序:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
public class PopupSizeTestFrame extends JFrame {
private static final int PREFFERED_WIDTH = 300;
private static JPanel resizingPanel;
private static JPopupMenu popupMenu;
private static PopupSizeTestFrame frame;
private static void resizePopupMenu() {
// What should I do here so that the size of the popup menu is updated?
/**
* Calling pack() works in this example, but I cannot call pack in the
* actual application since there is a smooth transition animation when
* clicking on the inner panel and pack() essentially re-adds the
* components of the popup menu (removeNotify and addNotify is called),
* which interferes with the animation and causes the menu to flicker.
* The flickering when clicking the inner panel can also be seen in this
* example when uncommenting the pack() call.
*/
//popupMenu.pack();
//
// I tried combinations of the following, without success:
//popupMenu.invalidate();
//popupMenu.revalidate();
//popupMenu.validate();
//popupMenu.doLayout();
// Repaint
popupMenu.repaint();
}
public static void main(String args[]) {
// Create popup child panel with height that changes when clicked
resizingPanel = new JPanel(new BorderLayout());
int initialHeight = 30;
final JLabel label = new JLabel("Click me (" + initialHeight + "px)");
resizingPanel.add(label);
resizingPanel.setBackground(Color.GREEN);
resizingPanel.setPreferredSize(new Dimension(PREFFERED_WIDTH, initialHeight));
resizingPanel.addMouseListener(new MouseAdapter() {
private int clickCount = 0;
@Override
public void mouseClicked(MouseEvent e) {
int height = ((clickCount % 3) + 1) * 50;
resizingPanel.setPreferredSize(new Dimension(PREFFERED_WIDTH, height));
clickCount++;
label.setText("Click me (" + height + "px)");
resizePopupMenu();
}
});
// Create popup
popupMenu = new JPopupMenu();
popupMenu.setLayout(new BorderLayout());
popupMenu.add(new JLabel("Header"), BorderLayout.NORTH);
popupMenu.add(resizingPanel, BorderLayout.CENTER);
popupMenu.add(new JLabel("Footer"), BorderLayout.SOUTH);
// Create frame
frame = new PopupSizeTestFrame();
frame.setLayout(new BorderLayout());
frame.setPreferredSize(new Dimension(400, 400));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
popupMenu.show(frame, e.getX(), e.getY());
}
}
});
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
frame.setVisible(true);
}
});
}
}
当运行上面的示例时,您将看到如果在单击内部面板后关闭和打开弹出菜单大小会更新,但当弹出菜单可见时不会更新。
我可以在 resizePopupMenu() 中做什么来更新 JPopupMenu 的高度?
最佳答案
对于问题中的测试应用程序,弹出窗口始终是一个重量级组件,并且以下组件有效(如 mKorbel 所建议的)
private static void resizePopupMenu() {
SwingUtilities.getWindowAncestor(popupMenu).pack();
}
对于实际应用程序,当弹出窗口位于边界内时,应用程序将其创建为轻量级组件,以防止使用 pack()
调整其大小,也防止其调整大小超过应用程序边界.
我试过设置这个属性...
JPopupMenu.setDefaultLightWeightPopupEnabled(false);
但是随后创建了一个中等重量的组件,它仍然不会调整大小超过应用程序边界。
所以我不得不首先使用以下不幸的代码强制“所有者”组件的所有弹出窗口成为重量级
// Set owner to component on which popup is shown or any of its parents
final Component owner = ...;
AccessController.doPrivileged(new PrivilegedAction<Void>() {
@Override
public Void run() {
try {
Field field;
if (System.getProperty("java.version").startsWith("1.6.0")) {
Class clazz = Class.forName("javax.swing.PopupFactory");
field = clazz.getDeclaredField("forceHeavyWeightPopupKey");
} else { //JDK 1.7.0, 1.8.0
Class clazz = Class.forName("javax.swing.ClientPropertyKey");
field = clazz.getDeclaredField("PopupFactory_FORCE_HEAVYWEIGHT_POPUP");
}
field.setAccessible(true);
owner.putClientProperty(field.get(null), Boolean.TRUE);
} catch (Exception ex) {
Exceptions.printStackTrace(ex);
}
return null;
}
});
强制弹出菜单始终是重量级组件后this
SwingUtilities.getWindowAncestor(popupMenu).pack();
还努力更新弹出窗口的大小,甚至超出应用程序的范围。
关于java - 可见时更新 JPopupMenu 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26214246/
我有一个 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
我是一名优秀的程序员,十分优秀!