知道如何以一种好的方式同时显示多个弹出菜单吗?(对于 JPopupMenu
)
我尝试了 @Override
show(Component invoker, int x, int y)
并设法通过删除 setInvoker(invoker); 使许多同时可见
。问题是我无法以任何方式删除弹出窗口。
问题:知道如何让 JPopupMenu
在显示更多 JPopupMenu
时仍然可见,但在其他情况下照常工作(关闭/隐藏其他操作)吗?
public class MultiPopupMenu {
public static void main(String[] args){
// Create popup
JPopupMenu menu1 = createPopupMenu("First label");
JPopupMenu menu2 = createPopupMenu("Second label");
// Create labels
JLabel label1 = new JLabel("abcde");
JLabel label2 = new JLabel("1234");
JPanel panel = new JPanel();
panel.add(label1);
panel.add(label2);
// Add labels
JFrame frame = new JFrame();
frame.add(panel);
frame.setPreferredSize(new Dimension(200,100));
frame.pack();
frame.setVisible(true);
// Show popups
menu1.show(label1,-40,20); // Not showing
menu2.show(label2, 0,20);
}
private static JPopupMenu createPopupMenu(String label){
JPopupMenu popup = new JPopupMenu();
JLabel lblTest = new JLabel(label);
popup.add(lblTest);
popup.setBackground(Color.YELLOW);
return popup;
}
}
最佳答案
在当前的 Swing 中不可能同时显示两个轻量级弹出容器,第二个弹出容器 hide() 首先立即(更改/自 Java4 以来/从 Java4 开始)
创建 JWindow(JTextComponents 不可编辑)或未修饰的 JDialog 并覆盖
为转义键设置 Visible(添加 KeyBindings)和 focusLost/(更好)WindowFocusListener
在其中添加带有 JButton 的 JPanel(将 setVisible 作为第一行代码触发,其余部分包装到 invokeLater 中,由 invokeLater 延迟)
然后你可以把 JComboBox 作为 JMenuItem(不可能见我第一句的描述)
关于java - 显示多个 JPopupMenus,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16937283/