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);
}
}
最佳答案
您需要监听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 .
关于java - 单击java JList中的项目时,如何打开新窗口或新框架?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20115139/