显示许多JPopupMenus

时间:2013-06-05 10:15:52

标签: java swing jpopupmenu

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

问题:当显示更多JPopupMenu时,如果显示JPopupMenu s仍然可见,但其他情况正常工作(关闭/隐藏其他操作)?

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;
    }
}

2 个答案:

答案 0 :(得分:2)

  • 在当前的Swing中不可能同时显示两个轻量级弹出容器,第二个弹出容器hide()首先是immmediatelly(更改/自/从Java4开始)

  • 创建JWindow(JTextComponents不可编辑)或未修饰的JDialog并覆盖

    1. 用于转义键的setVisible(添加KeyBindings)和focusLost /(更好)WindowFocusListener

    2. 使用JButtons添加JPanel(将setVisible作为第一个代码行触发,rest被包装到invokeLater中,由invokeLater延迟)

    3. 然后你可以将JComboBox作为JMenuItem(不可能在我的第1句中看到描述)

答案 1 :(得分:1)

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;
    }
}