【发布时间】:2012-02-07 19:06:39
【问题描述】:
我在 NetBeans 中制作了一个 GUI。这是一个聊天程序,我有 4 个突击队员,例如 /join、/leave、/whisper 和 /leave
private void CommandoActionPerformed(java.awt.event.ActionEvent evt) {
JOptionPane.showMessageDialog(rootPane, "The following commandos are:" + "\n" + "\n" + "/join Channel name" + "\n" + "/leave channel name" + "\n" + "/whisper nick message" + "\n" + "/quit - quit the program");
}
这没关系,但我想要 actionlister 而不是 showMessageDialog,所以我可以推动它们,它出现在我的 JTextField 中。我想我可以让他们在那里,但我不知道如何让 actionlistener 与此结合。
编辑: 我想要的是按下 Commando 按钮并打开一个窗口,其中有 4 个新按钮,每个按钮都有一个 commando(/join、/leave、/whisper 和 /exit),所以当我按下其中一个按钮时,我得到了 commando在我的文本字段中,所以我只需要写下其余部分。 所以如果我按下“/join”按钮,我只需要写下频道名称。
EDIT2:如果我在描述问题方面做得很糟糕,我可以展示我想要的以及到目前为止所做的:
private void showCommandActionPerformed(java.awt.event.ActionEvent evt) {
Object[] options = { "/join", "/leave", "/whisper", "/quit", "Ingenting" };
int choice= JOptionPane.showOptionDialog(rootPane, "What do u want to do? ", null, WIDTH, WIDTH, null, options, rootPane);
switch (choice) {
case 0:
skrivTekst.setText("/Join ");
skrivTekst.requestFocus();
break;
case 1:
skrivTekst.setText("/Leave");
skrivTekst.requestFocus();
break;
case 2:
skrivTekst.setText("/Whisper");
skrivTekst.requestFocus();
break;
case 3:
skrivTekst.setText("/Join ");
skrivTekst.requestFocus();
case 4:
System.exit(1); //this is wrong. i just want to close this window, not the whole program
default:
JOptionPane.showMessageDialog(null, "donno what!?!?!?!?!?!?!" + choice);
}
}
我希望这表明我想要什么以及我做了什么。 Ty :) 所以我剩下的唯一问题是关闭一个 JOptionPane 窗口而不是程序
【问题讨论】:
-
你的问题不是很清楚,介意改写吗?
-
如果你想将 actionListener 添加到 JButton 然后使用
button.addActionListener(actionListener)方法。
标签: java swing actionlistener jtextfield joptionpane