【问题标题】:Right click JPopupMenu on JTabbedPane右键单击 JTabbedPane 上的 JPopupMenu
【发布时间】:2017-10-20 00:38:28
【问题描述】:

我有一个带有自定义选项卡组件的 JTabbedPane。我希望能够右键单击选项卡上的任意位置并显示 JPopupMenu。我遇到的问题是每个选项卡上都有死区,JPopupMenu 没有通过右键单击出现。我相信这是因为我正在将听众附加到jpanel中,该jpanel充当了选项卡组件,但jpanel并没有“填充”整个选项卡。

有没有办法将鼠标侦听器附加到整个选项卡?

这里有一个例子来说明我所看到的。在选项卡的黄色区域,我可以右键单击并弹出菜单,但在选项卡的灰色区域,右键单击不被拦截。

public class TabExample {

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                frame.setBounds(100, 100, 1024, 768);

                JTabbedPane pane = new JTabbedPane();

                for (int i = 0; i < 15; i++) {
                    JPanel panel = new JPanel();
                    JLabel label = new JLabel("Panel " + i);
                    panel.add(label);
                    pane.addTab("", panel);

                    final JPanel tabComponentPanel = new JPanel(new BorderLayout());

                    final JLabel tabComponentLabel = new JLabel("My Tab " + i);

                    final JLabel tabComponentImageLabel = new JLabel();
                    ImageIcon icon = new ImageIcon(getImage());
                    tabComponentImageLabel.setHorizontalAlignment(JLabel.CENTER);
                    tabComponentImageLabel.setIcon(icon);

                    tabComponentPanel.add(tabComponentImageLabel,BorderLayout.CENTER);
                    tabComponentPanel.add(tabComponentLabel,BorderLayout.SOUTH);

                    tabComponentPanel.setBackground(Color.YELLOW);

                    tabComponentPanel.addMouseListener(new MouseAdapter() {

                        @Override
                        public void mouseClicked(MouseEvent e) {

                            if (e.getButton() == MouseEvent.BUTTON1) {
                                pane.setSelectedComponent(panel);
                            } else if (e.getButton() == MouseEvent.BUTTON3) {
                                JPopupMenu jPopupMenu = new JPopupMenu();
                                JMenuItem menuItem = new JMenuItem("Menu Item");
                                jPopupMenu.add(menuItem);
                                menuItem.addActionListener(new ActionListener() {

                                    @Override
                                    public void actionPerformed(ActionEvent e) {
                                        System.out.println("Clicked ");

                                    }
                                });

                                jPopupMenu.show(tabComponentPanel, e.getX(),
                                        e.getY());
                            }
                        }

                    });

                    pane.setTabComponentAt(pane.indexOfComponent(panel),
                            tabComponentPanel);
                }

                frame.add(pane);

                frame.setVisible(true);

            }

        });
    }

    private static BufferedImage getImage() {
        BufferedImage bimage = new BufferedImage(16, 16,
                BufferedImage.TYPE_BYTE_INDEXED);

        Graphics2D g2d = bimage.createGraphics();

        g2d.setColor(Color.red);
        g2d.fill(new Ellipse2D.Float(0, 0, 16, 16));
        g2d.dispose();

        return bimage;
    }
}

【问题讨论】:

    标签: java swing jtabbedpane


    【解决方案1】:

    有没有办法将鼠标侦听器附加到整个选项卡?

    您可以将 MouseListener 添加到 JTabbedPane。

    然后在 MouseEvent 上,您可以使用 getUI() 方法获取 BasicTabbedPaneUI 类。这个类有一个getTabBounds(...) 方法。

    因此您可以遍历所有选项卡以查看任何选项卡的边界是否与鼠标点匹配。

    编辑:

    BasicTabbedPaneUI 有一个tabForCoordinate(...) 方法,它不需要迭代逻辑。

    【讨论】:

    • 天才!谢谢你。我想有一种更清洁的方法,但这很好用。
    • @mainstringargs I figured there was a cleaner way - 你是对的。见编辑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-13
    • 2010-12-15
    相关资源
    最近更新 更多