2016-03-02 76 views

回答

0

您可以通过extanding JPopupMenu中创建自己的PopMenu。通过向按钮添加一个动作侦听器,您可以对任何想要的事情做出反应(例如关闭弹出窗口)。这里是一个例子:

public class TestPopupMenu extends JPopupMenu { 

private JLabel testLabel; 
private JButton testButton; 

public TestPopupMenu() { 
    super(); 
    initMenuItems(); 
} 

private void initMenuItems() { 
    add(getTestLabel()); 
    add(getTestButton());  
} 

private JButton getTestButton() { 
    if (testButton == null) { 
     testButton = new JButton("Test"); 
     testButton.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       // Do something what you want     
      } 
     }); 
    } 
    return testButton; 
} 

private JLabel getTestLabel() { 
    if (testLabel == null) { 
     testLabel = new JLabel("#Some text");   
    } 
    return testLabel; 
} }