我在NetBeans中创建了一个GUI。这是一个聊天程序,我有4名突击队员像/加入,/留下,/耳语和/离开Java GUI JButton到actionlistner
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。
编辑: 我想要的是推动突击队按钮,并得到一个窗口,我有4个新的按钮,每个有一个突击队(/加入,/离开,/耳语和/退出),所以当我推1这些按钮中,我得到了我的文本字段中的突击队,所以我只需要写下其余的。 所以,如果我推“/加入”按钮,我只需要写通道名称。
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 to all :) 所以我留下的唯一问题是关闭一个JOptionPane窗口而不是程序
你的问题不是很清楚,心态要改变吗? – MByD 2012-01-11 12:19:07
如果你想添加actionListener到JButton,然后使用'button.addActionListener(actionListener)'方法。 – 2012-01-11 12:20:52
也许你已经看过http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html,有http://docs.oracle.com/javase/tutorial/uiswing/components/dialog。 html#按钮和http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html#input, – mKorbel 2012-01-11 12:41:50