首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >显示多个JPopupMenus

显示多个JPopupMenus
EN

Stack Overflow用户
提问于 2013-06-05 18:15:52
回答 2查看 671关注 0票数 1

你知道如何以一种好的方式同时显示多个弹出式菜单吗?(对于JPopupMenu)我尝试@Override show(Component invoker, int x, int y),并设法通过移除setInvoker(invoker);同时显示多个弹出式菜单。这样做的问题是我无法以任何方式删除弹出窗口。

问:你知道如何让JPopupMenu在显示更多JPopupMenu时仍然可见,但在其他情况下照常工作(关闭/隐藏其他操作)?

代码语言:javascript
运行
复制
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;
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-06-05 18:25:23

当前Swing中的

  • 不可能同时显示两个轻量级弹出式容器,第二个弹出式容器hide()首先立即(更改/因为在Java4中/从Java4开始)
  • 创建JWindow (JTextComponents不可编辑)或未修饰的JDialog并覆盖

代码语言:javascript
运行
复制
1. setVisible for escape key (add KeyBindings) and focusLost / (better) WindowFocusListener  
2. add there JPanel with JButtons (firing setVisible as 1st code line, rest is wrapped into invokeLater, delayed by invokeLater)
3. then you can to put JComboBox as JMenuItem (not possible see description in my 1st. sentence)

票数 2
EN

Stack Overflow用户

发布于 2013-06-05 19:33:37

代码语言:javascript
运行
复制
public class MultiPopupMenu {

    public static void main(String[] args){

        // Create popup
        JWindow popup1 = createPopup("First label");
        JWindow popup2 = createPopup("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.setLocationRelativeTo(null);
        frame.setVisible(true);

        // Show popups
        popup1.pack();
        popup2.pack();

        Point floc = frame.getLocation();
        Point loc = label1.getLocation();
        System.out.println(floc);
        popup1.setLocation((int)(floc.getX()+loc.getX())-20, (int)(floc.getY()+loc.getY())+40);
        loc = label2.getLocation();
        popup2.setLocation((int)(floc.getX()+loc.getX())+20, (int)(floc.getY()+loc.getY())+40);

        popup1.setBackground(Color.YELLOW);
        popup1.setVisible(true);
        popup2.setVisible(true);
    }

    private static JWindow createPopup(String label){
        JWindow popup = new JWindow();
        JLabel lblTest = new JLabel(label);
        popup.add(lblTest);
        popup.getContentPane().setBackground(Color.YELLOW);
        return popup;
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16937283

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档