添加鼠标侦听器后无法单击选项卡

发布于 2024-12-11 03:59:53 字数 4244 浏览 0 评论 0原文

我一直在尝试向 JTabbedPane 的选项卡标题添加一个弹出菜单,我已设法执行以下操作:

        tabbedPane.setTabComponentAt(a+2, x);


        x.addMouseListener(new MouseListener() {

            @Override
            public void mouseReleased(MouseEvent e) {
                maybeShowPopup(e);

            }

            @Override
            public void mousePressed(MouseEvent e) {
                maybeShowPopup(e);
            }


            @Override
            public void mouseExited(MouseEvent e) {}

            @Override
            public void mouseEntered(MouseEvent e) {}

            @Override
            public void mouseClicked(MouseEvent e) {}


            private void maybeShowPopup(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    popUpMenuTab.show(e.getComponent(),
                               e.getX(), e.getY());
                }
            }

        });

但是,如果您右键单击,您将获得上下文菜单,但如果您左键单击,则不会发生任何事情。有谁知道我如何才能在选项卡上恢复原始功能,以便它在左键单击时变为活动状态,但在右键单击时也有上下文菜单。

SSCCE 如下所示

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTabbedPane;

public class JTabbedPaneDemoextends JPanel {
    private JPopupMenu menu = new JPopupMenu();

    public JTabbedPaneDemo() {
        ImageIcon icon = new ImageIcon("java-swing-tutorial.JPG");
        JTabbedPane jtbExample = new JTabbedPane();

        JPanel jplInnerPanel1 = createInnerPanel("Tab 1 Contains Tooltip and Icon");
        jtbExample.addTab("One", icon, jplInnerPanel1, "Tab 1");
        jtbExample.setSelectedIndex(0);

        JPanel jplInnerPanel2 = createInnerPanel("Tab 2 Contains Icon only");
        jtbExample.addTab("Two", icon, jplInnerPanel2);

        JPanel jplInnerPanel3  = createInnerPanel("Tab 3 Contains Tooltip and Icon");
        jtbExample.addTab("Three", icon, jplInnerPanel3, "Tab 3");

        JPanel jplInnerPanel4 = createInnerPanel("Tab 4 Contains Text only");
        jtbExample.addTab("Four", jplInnerPanel4);

        menu.add(new JMenuItem("Item 1"));
        menu.add(new JMenuItem("Item 2"));

        JLabel tab4Label = new JLabel();
        tab4Label.setText("Four");  

        jtbExample.setTabComponentAt(3, tab4Label);


        tab4Label.addMouseListener(new MouseListener() {

            @Override
            public void mouseReleased(MouseEvent e) {
                maybeShowPopup(e);

            }

            @Override
            public void mousePressed(MouseEvent e) {
                maybeShowPopup(e);
            }


            @Override
            public void mouseExited(MouseEvent e) {}

            @Override
            public void mouseEntered(MouseEvent e) {}

            @Override
            public void mouseClicked(MouseEvent e) {}


            private void maybeShowPopup(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    menu.show(e.getComponent(),
                               e.getX(), e.getY());
                }
            }

        });


        //Add the tabbed pane to this panel.
        setLayout(new GridLayout(1, 1)); 
        add(jtbExample);
    }

    protected JPanel createInnerPanel(String text) {
        JPanel jplPanel = new JPanel();
        JLabel jlbDisplay = new JLabel(text);
        jlbDisplay.setHorizontalAlignment(JLabel.CENTER);
        jplPanel.setLayout(new GridLayout(1, 1));
        jplPanel.add(jlbDisplay);
        return jplPanel;
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("TabbedPane Source Demo");
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);}
        });

        frame.getContentPane().add(new JTabbedPaneDemo(), 
                                   BorderLayout.CENTER);
        frame.setSize(400, 125);
        frame.setVisible(true);
    }
}

,在这里您可以左键单击选项卡 4,但不能右键单击。

I have been trying to add a pop up menu to the tab title of a JTabbedPane, which I have managed to do as follows:

        tabbedPane.setTabComponentAt(a+2, x);


        x.addMouseListener(new MouseListener() {

            @Override
            public void mouseReleased(MouseEvent e) {
                maybeShowPopup(e);

            }

            @Override
            public void mousePressed(MouseEvent e) {
                maybeShowPopup(e);
            }


            @Override
            public void mouseExited(MouseEvent e) {}

            @Override
            public void mouseEntered(MouseEvent e) {}

            @Override
            public void mouseClicked(MouseEvent e) {}


            private void maybeShowPopup(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    popUpMenuTab.show(e.getComponent(),
                               e.getX(), e.getY());
                }
            }

        });

However, with this if you right click you get the context menu, but if you left click nothing happnes. Does anyone know how I can get the orginal functionality back on the tab so that it changes to being active on a left click but also has the context menu on a right click.

An SSCCE is as follows

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTabbedPane;

public class JTabbedPaneDemoextends JPanel {
    private JPopupMenu menu = new JPopupMenu();

    public JTabbedPaneDemo() {
        ImageIcon icon = new ImageIcon("java-swing-tutorial.JPG");
        JTabbedPane jtbExample = new JTabbedPane();

        JPanel jplInnerPanel1 = createInnerPanel("Tab 1 Contains Tooltip and Icon");
        jtbExample.addTab("One", icon, jplInnerPanel1, "Tab 1");
        jtbExample.setSelectedIndex(0);

        JPanel jplInnerPanel2 = createInnerPanel("Tab 2 Contains Icon only");
        jtbExample.addTab("Two", icon, jplInnerPanel2);

        JPanel jplInnerPanel3  = createInnerPanel("Tab 3 Contains Tooltip and Icon");
        jtbExample.addTab("Three", icon, jplInnerPanel3, "Tab 3");

        JPanel jplInnerPanel4 = createInnerPanel("Tab 4 Contains Text only");
        jtbExample.addTab("Four", jplInnerPanel4);

        menu.add(new JMenuItem("Item 1"));
        menu.add(new JMenuItem("Item 2"));

        JLabel tab4Label = new JLabel();
        tab4Label.setText("Four");  

        jtbExample.setTabComponentAt(3, tab4Label);


        tab4Label.addMouseListener(new MouseListener() {

            @Override
            public void mouseReleased(MouseEvent e) {
                maybeShowPopup(e);

            }

            @Override
            public void mousePressed(MouseEvent e) {
                maybeShowPopup(e);
            }


            @Override
            public void mouseExited(MouseEvent e) {}

            @Override
            public void mouseEntered(MouseEvent e) {}

            @Override
            public void mouseClicked(MouseEvent e) {}


            private void maybeShowPopup(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    menu.show(e.getComponent(),
                               e.getX(), e.getY());
                }
            }

        });


        //Add the tabbed pane to this panel.
        setLayout(new GridLayout(1, 1)); 
        add(jtbExample);
    }

    protected JPanel createInnerPanel(String text) {
        JPanel jplPanel = new JPanel();
        JLabel jlbDisplay = new JLabel(text);
        jlbDisplay.setHorizontalAlignment(JLabel.CENTER);
        jplPanel.setLayout(new GridLayout(1, 1));
        jplPanel.add(jlbDisplay);
        return jplPanel;
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("TabbedPane Source Demo");
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);}
        });

        frame.getContentPane().add(new JTabbedPaneDemo(), 
                                   BorderLayout.CENTER);
        frame.setSize(400, 125);
        frame.setVisible(true);
    }
}

Here you can left click on tab 4 but you can't right click.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅冕 2024-12-18 03:59:53

更新:巧合的是,这个答案使用了与@Neifen相同的方法 答案,但该示例可能值得保留。

设置选项卡式窗格模型的选定索引似乎有效:getModel().setSelectedIndex(3)。我已经更新了您的示例,以使用事件调度线程,并通过使其成为一个 sscce self-包含。

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;

/** @see https://stackoverflow.com/questions/7818752 */
public class JTabbedPaneDemo extends JPanel {

    private JPopupMenu menu = new JPopupMenu();

    public JTabbedPaneDemo() {
        Icon icon = UIManager.getIcon("html.pendingImage");
        final JTabbedPane jtb = new JTabbedPane();

        JPanel jplInnerPanel1 = createInnerPanel("Tab 1: Tooltip and Icon");
        jtb.addTab("One", icon, jplInnerPanel1, "Tab 1");
        jtb.setSelectedIndex(0);

        JPanel jplInnerPanel2 = createInnerPanel("Tab 2: Icon only");
        jtb.addTab("Two", icon, jplInnerPanel2);

        JPanel jplInnerPanel3 = createInnerPanel("Tab 3: Tooltip and Icon");
        jtb.addTab("Three", icon, jplInnerPanel3, "Tab 3");

        JPanel jplInnerPanel4 = createInnerPanel("Tab 4: Text only");
        jtb.addTab("Four", jplInnerPanel4);

        menu.add(new JMenuItem("Item 1"));
        menu.add(new JMenuItem("Item 2"));

        JLabel tab4Label = new JLabel();
        tab4Label.setText("Four");
        jtb.setTabComponentAt(3, tab4Label);
        tab4Label.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseReleased(MouseEvent e) {
                maybeShowPopup(e);

            }

            @Override
            public void mousePressed(MouseEvent e) {
                maybeShowPopup(e);
            }

            private void maybeShowPopup(MouseEvent e) {
                jtb.getModel().setSelectedIndex(3);
                if (e.isPopupTrigger()) {
                    menu.show(e.getComponent(), e.getX(), e.getY());
                }
            }
        });

        setLayout(new GridLayout());
        add(jtb);
    }

    private JPanel createInnerPanel(String text) {
        JPanel jplPanel = new JPanel();
        JLabel jlbDisplay = new JLabel(text);
        jlbDisplay.setHorizontalAlignment(JLabel.CENTER);
        jplPanel.setLayout(new GridLayout(1, 1));
        jplPanel.add(jlbDisplay);
        return jplPanel;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame("TabbedPane Source Demo");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JTabbedPaneDemo());
                frame.pack();
                frame.setSize(400, 125);
                frame.setVisible(true);
            }
        });
    }
}

Update: Coincidentally, this answer uses the same approach as @Neifen's answer, but the example may be worth preserving.

Setting the tabbed pane model's selected index seems to work: getModel().setSelectedIndex(3). I've updated your example to use the event dispatch thread and to be an sscce by making it self-contained.

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;

/** @see https://stackoverflow.com/questions/7818752 */
public class JTabbedPaneDemo extends JPanel {

    private JPopupMenu menu = new JPopupMenu();

    public JTabbedPaneDemo() {
        Icon icon = UIManager.getIcon("html.pendingImage");
        final JTabbedPane jtb = new JTabbedPane();

        JPanel jplInnerPanel1 = createInnerPanel("Tab 1: Tooltip and Icon");
        jtb.addTab("One", icon, jplInnerPanel1, "Tab 1");
        jtb.setSelectedIndex(0);

        JPanel jplInnerPanel2 = createInnerPanel("Tab 2: Icon only");
        jtb.addTab("Two", icon, jplInnerPanel2);

        JPanel jplInnerPanel3 = createInnerPanel("Tab 3: Tooltip and Icon");
        jtb.addTab("Three", icon, jplInnerPanel3, "Tab 3");

        JPanel jplInnerPanel4 = createInnerPanel("Tab 4: Text only");
        jtb.addTab("Four", jplInnerPanel4);

        menu.add(new JMenuItem("Item 1"));
        menu.add(new JMenuItem("Item 2"));

        JLabel tab4Label = new JLabel();
        tab4Label.setText("Four");
        jtb.setTabComponentAt(3, tab4Label);
        tab4Label.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseReleased(MouseEvent e) {
                maybeShowPopup(e);

            }

            @Override
            public void mousePressed(MouseEvent e) {
                maybeShowPopup(e);
            }

            private void maybeShowPopup(MouseEvent e) {
                jtb.getModel().setSelectedIndex(3);
                if (e.isPopupTrigger()) {
                    menu.show(e.getComponent(), e.getX(), e.getY());
                }
            }
        });

        setLayout(new GridLayout());
        add(jtb);
    }

    private JPanel createInnerPanel(String text) {
        JPanel jplPanel = new JPanel();
        JLabel jlbDisplay = new JLabel(text);
        jlbDisplay.setHorizontalAlignment(JLabel.CENTER);
        jplPanel.setLayout(new GridLayout(1, 1));
        jplPanel.add(jlbDisplay);
        return jplPanel;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame("TabbedPane Source Demo");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JTabbedPaneDemo());
                frame.pack();
                frame.setSize(400, 125);
                frame.setVisible(true);
            }
        });
    }
}
心意如水 2024-12-18 03:59:53

这是我的解决方案:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTabbedPane;

public class JTabbedPaneDemo extends JPanel {
    private JPopupMenu menu = new JPopupMenu();
    private JTabbedPane jtbExample;

    public JTabbedPaneDemo() {
        ImageIcon icon = new ImageIcon("java-swing-tutorial.JPG");
        jtbExample = new JTabbedPane();

        JPanel jplInnerPanel1 = createInnerPanel("Tab 1 Contains Tooltip and Icon");
        jtbExample.addTab("One", icon, jplInnerPanel1, "Tab 1");
        jtbExample.setSelectedIndex(0);

        JPanel jplInnerPanel2 = createInnerPanel("Tab 2 Contains Icon only");
        jtbExample.addTab("Two", icon, jplInnerPanel2);

        JPanel jplInnerPanel3 = createInnerPanel("Tab 3 Contains Tooltip and Icon");
        jtbExample.addTab("Three", icon, jplInnerPanel3, "Tab 3");

        JPanel jplInnerPanel4 = createInnerPanel("Tab 4 Contains Text only");
        jtbExample.addTab("Four", jplInnerPanel4);

        menu.add(new JMenuItem("Item 1"));
        menu.add(new JMenuItem("Item 2"));

        JLabel tab4Label = new JLabel();
        tab4Label.setText("Four");

        jtbExample.setTabComponentAt(3, tab4Label);

        tab4Label.addMouseListener(new TabbedMouseListener(3));

        // Add the tabbed pane to this panel.
        setLayout(new GridLayout(1, 1));
        add(jtbExample);
    }

    protected JPanel createInnerPanel(String text) {
        JPanel jplPanel = new JPanel();
        JLabel jlbDisplay = new JLabel(text);
        jlbDisplay.setHorizontalAlignment(JLabel.CENTER);
        jplPanel.setLayout(new GridLayout(1, 1));
        jplPanel.add(jlbDisplay);
        return jplPanel;
    }

    /**
     * 
     * Mouselistener for the Tabbedpane
     * 
     */
    class TabbedMouseListener extends MouseAdapter {
        private final int index;

        /**
         * Constructor
         * 
         * @param index
         *            the index of the tab
         */
        public TabbedMouseListener(int index) {
            this.index = index;
        }

        public void mouseReleased(MouseEvent e) {
            maybeShowPopup(e);
        }

        private void maybeShowPopup(MouseEvent e) {
            if (e.isPopupTrigger()) {
                menu.show(e.getComponent(), e.getX(), e.getY());
            }
            else {
                jtbExample.setSelectedIndex(index);
            }
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("TabbedPane Source Demo");
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        frame.getContentPane().add(new JTabbedPaneDemo(), BorderLayout.CENTER);
        frame.setSize(400, 125);
        frame.setVisible(true);
    }
}

我已经将侦听器作为一个类。当您声明侦听器时,您可以将选项卡的索引作为参数。
我还将 tabbedPane 设为全局

This is my Solution:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTabbedPane;

public class JTabbedPaneDemo extends JPanel {
    private JPopupMenu menu = new JPopupMenu();
    private JTabbedPane jtbExample;

    public JTabbedPaneDemo() {
        ImageIcon icon = new ImageIcon("java-swing-tutorial.JPG");
        jtbExample = new JTabbedPane();

        JPanel jplInnerPanel1 = createInnerPanel("Tab 1 Contains Tooltip and Icon");
        jtbExample.addTab("One", icon, jplInnerPanel1, "Tab 1");
        jtbExample.setSelectedIndex(0);

        JPanel jplInnerPanel2 = createInnerPanel("Tab 2 Contains Icon only");
        jtbExample.addTab("Two", icon, jplInnerPanel2);

        JPanel jplInnerPanel3 = createInnerPanel("Tab 3 Contains Tooltip and Icon");
        jtbExample.addTab("Three", icon, jplInnerPanel3, "Tab 3");

        JPanel jplInnerPanel4 = createInnerPanel("Tab 4 Contains Text only");
        jtbExample.addTab("Four", jplInnerPanel4);

        menu.add(new JMenuItem("Item 1"));
        menu.add(new JMenuItem("Item 2"));

        JLabel tab4Label = new JLabel();
        tab4Label.setText("Four");

        jtbExample.setTabComponentAt(3, tab4Label);

        tab4Label.addMouseListener(new TabbedMouseListener(3));

        // Add the tabbed pane to this panel.
        setLayout(new GridLayout(1, 1));
        add(jtbExample);
    }

    protected JPanel createInnerPanel(String text) {
        JPanel jplPanel = new JPanel();
        JLabel jlbDisplay = new JLabel(text);
        jlbDisplay.setHorizontalAlignment(JLabel.CENTER);
        jplPanel.setLayout(new GridLayout(1, 1));
        jplPanel.add(jlbDisplay);
        return jplPanel;
    }

    /**
     * 
     * Mouselistener for the Tabbedpane
     * 
     */
    class TabbedMouseListener extends MouseAdapter {
        private final int index;

        /**
         * Constructor
         * 
         * @param index
         *            the index of the tab
         */
        public TabbedMouseListener(int index) {
            this.index = index;
        }

        public void mouseReleased(MouseEvent e) {
            maybeShowPopup(e);
        }

        private void maybeShowPopup(MouseEvent e) {
            if (e.isPopupTrigger()) {
                menu.show(e.getComponent(), e.getX(), e.getY());
            }
            else {
                jtbExample.setSelectedIndex(index);
            }
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("TabbedPane Source Demo");
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        frame.getContentPane().add(new JTabbedPaneDemo(), BorderLayout.CENTER);
        frame.setSize(400, 125);
        frame.setVisible(true);
    }
}

I have made a the listener as a class. When you declare the listener you can put the index of the tab as parameter.
I also made the tabbedPane global

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文