// 1)
JButton okButton = new JButton("Ok");
okButton.setActionCommand("ok");
JButton cancelButton = new JButton("Abbrechen");
cancelButton .setActionCommand("cancel");
//...
void actionPerformed(ActionEvent event)
{
if (event.getActionCommand().compareTo("ok") == 0)
// Ok geklickt
else if (event.getActionCommand().compareTo("cancel") == 0)
// Abbrechen geklickt
}
// 2)
public class MyWindow extends JFrame implements ActionListener
{
protected JButton okButton;
protected JButton cancelButton;
public MyWindow()
{
//initialisieren
}
public void actionPerformed(ActionEvent event)
{
if (event.getSource().equals(okButton))
//Ok geklickt
else if (event.getSource().equals(cancelButton))
//Abbrechen geklickt
}
}