单击java的JList中的项目,如何打开新窗口或新框架?

时间:2013-11-21 07:44:42

标签: java

 private void channelItemsMouseClicked(java.awt.event.MouseEvent evt) {                                          
    // TODO add your handling code here:
    if(channelItems.getSelectedIndex()>=0)
    {

        JPanel p1=new JPanel();
        JLabel label = new JLabel("Enter your username below", SwingConstants.CENTER);
        p1.add(label);
        add(p1);

    }
 }

2 个答案:

答案 0 :(得分:1)

您需要双击MouseListener,否则每次点击鼠标都会打开对话框。

ListSelectionModel listSelectionModel..
JList list=new JList(listSelectionModel);


list.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent evt) {
    if (evt.getClickCount() == 2) {
          //...Show the JDialog or JOptionPane here, not JPanel.
         String name=JOptoinPane.showInputDialog(null, "Enter your username");
    }
  }
});

有关详情,请仔细阅读tutorial

答案 1 :(得分:0)

简单地说:

actionMethod(...){
    currentPanel.dispose();
    newPanel.setVisible(true);
}