http://tecfa.unige.ch/guides/java/staf2x/ex/gui/ButtonTest2.java
import java.awt.*;
import java.awt.event.*;
public class ButtonTest2 extends Frame implements ActionListener {
/* The Button test Program Java 1.1 by T Abbott and J Bishop Oct 1997
* a simplified version SMM / TECFA Nov 98
* Prints a warning message, and
* Illustrates Buttons, Listeners and the
* handling of events. */
Button OkButton;
Label TheLabel;
public ButtonTest2 () {
/* The constructor is responsible for setting
* up the initial buttons and colour background. */
setLayout(new FlowLayout(FlowLayout.CENTER));
setBackground(Color.black);
setForeground(Color.cyan);
TheLabel = new Label("W A R N I N G");
add(TheLabel);
// create & add a button, plus an ActionListener to the button
OkButton = new Button("Ok");
OkButton.addActionListener(this);
add(OkButton);
}
public void actionPerformed(ActionEvent e) {
setVisible(false);
dispose();
System.exit (0);
}
public static void main(String[] args) {
Frame f = new ButtonTest2();
f.setTitle("Button Test 2");
f.setSize(160,60);
f.setVisible(true);
// allows to close the application
f.addWindowListener (new WindowAdapter () {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
} }
Voir aussi une variante:
http://tecfa.unige.ch/guides/java/staf2x/ex/gui/ButtonTest.java
public class ButtonTest2 extends Frame implements ActionListener {....}
public class ButtonTest2 extends Frame implements ActionListener {....}
setLayout(new FlowLayout(FlowLayout.CENTER));
OkButton = new Button("Ok");
OkButton.addActionListener(this);
add(OkButton);