Button button = new Button();
button.addActionListener(this);
@Override
actionPerformed(ActionEvent e) {
// do something
}
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class AWTFrame {
private Frame frame;
private Button btn;
public AWTFrame(){
frame = new Frame();
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setBackground(Color.RED);
frame.setLayout(new BorderLayout());
frame.addWindowListener(new WindowAdapter () {
public void windowClosing (final WindowEvent e) {
System.exit(0);
}
});
btn = new Button("Klick mich !");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(frame.getBackground().equals(Color.RED)){
frame.setBackground(Color.GREEN);
}
else{
frame.setBackground(Color.RED);
}
}
});
frame.add(btn, BorderLayout.SOUTH);
frame.setVisible(true);
}
}