如何將ActionListener
添加到JOptionPane
中的JButton
。所以當我按下按鈕時,它會執行某個代碼。 我試圖用這個代碼,但它不工作:將actionlistener添加到joptionPane中的按鈕(java)
JButton button1= new JButton("Button 1");
int value = JOptionPane.showOptionDialog(null, "Here's a test message", "Test", JOptionPane.YES_OPTION , JOptionPane.QUESTION_MESSAGE, null,new Object[]{button1}, button1);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//code to excute
System.out.println("code excuted");
}
});
你的意思是[this](http://stackoverflow.com/questions/12828389/actionlistener-on-joptionpane/12829264#12829264)?問題是,爲什麼? 'JOptionPane'將返回被激活的對象的'int'索引 – MadProgrammer
或類似[this](http://stackoverflow.com/questions/14591089/joptionpane-passing-custom-buttons/14591165#14591165)? – MadProgrammer