如何将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