我想在JTabbedPane的标签页处右击鼠标,然后弹出一个popupMenu,请高手指点!
这个问题应该涉及到mouseEvent和mouseListener,但是,JTabbedPane只有一个ChangeListener
到这儿我就不知道该怎么做了,请各位大哥帮帮忙,如能给一些代码那就更好了,先谢了!!

解决方案 »

  1.   

    homesos 你能说的再详细点吗?谢谢
      

  2.   

    先創建一個JPopupMenu, 然後調用JTabbedPane的setComponentPopupMenu(JPopupMenu popup)方法.
    你試試吧.
    我覺得還是用swt/jface寫介面比較好.
      

  3.   

    JTabbedPane有关于mouse的方法(是继承的)java.lang.Object
      |
      +--java.awt.Component
            |
            +--java.awt.Container
                  |
                  +--javax.swing.JComponent
                        |
                        +--javax.swing.JTabbedPane
    java.awt.Component:
    addMouseListener, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp,
      

  4.   

    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;import javax.swing.*;public class TabbedPanePopupMenuTest
    {
    public static void main(String[] args)
    {
    try {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
    e.printStackTrace();
    }
    final JTabbedPane tabbedPane = new JTabbedPane(); final JPopupMenu popupMenu = new JPopupMenu();
    final JMenuItem newTabMenuItem = new JMenuItem("New Tab");
    final JMenuItem closeTabMenuItem = new JMenuItem("Close Tab"); popupMenu.add(newTabMenuItem);
    popupMenu.add(closeTabMenuItem);

    newTabMenuItem.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    String title = JOptionPane.showInputDialog(tabbedPane, "Input Tab title:");
    if (title != null) {
    JPanel panel = new JPanel();
    tabbedPane.addTab(title, panel);
    tabbedPane.setSelectedComponent(panel);
    }
    }
    });

    closeTabMenuItem.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    Integer tabIndex = (Integer) closeTabMenuItem.getClientProperty("CloseIndex");
    if (tabIndex != null && tabIndex.intValue() <= tabbedPane.getTabCount()-1) {
    tabbedPane.remove(tabIndex.intValue());
    }
    }
    }); tabbedPane.addTab("Tab1", new JPanel());
    tabbedPane.addTab("Tab2", new JPanel());
    tabbedPane.addTab("Tab3", new JPanel()); tabbedPane.addMouseListener(new MouseAdapter() {
    public void mousePressed(MouseEvent e)
    {
    if (e.getButton() == MouseEvent.BUTTON3) {
    int tabIndex = tabbedPane.indexAtLocation(e.getX(), e.getY());
    if (tabIndex != -1) {
    closeTabMenuItem.setVisible(true);
    closeTabMenuItem.putClientProperty("CloseIndex", new Integer(tabIndex));
    }
    else {
    closeTabMenuItem.setVisible(false);
    }
    popupMenu.show(tabbedPane, e.getX(), e.getY());
    }
    }
    });
    JFrame f = new JFrame("TabbedPanePopupMenuTest");
    f.getContentPane().add(tabbedPane, BorderLayout.CENTER);
    f.setSize(300,200);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
    }
    }