[JavaSE] GUI (Action event), javasegui
Normalize the defined classes and separate the events and graphical components.
Define a FrameDemo class
Define the member attribute Frame
Define member attributes Botton
Define the constructor FrameDemo ()
Define the initialization method init ()
In the initialization method, the new Frame (), parameter: String form name
Call the setBounds () method of the Frame object. The parameter is x, y, width, and height.
Call the setLayout () method of the Frame object. Parameter: FlowLayout object
Get the Button object, new, and construct the String Button text.
Call the add () method of the Frame object. Parameter: Button Object
Call the setVisible () method of the Frame object. The parameter is true of Boolean.
Define the event Method myEvent ()
Call the addWindowListener () method of the Frame object. Parameter: WindowListener object. WindowListener is an interface. There are seven methods to implement it. Find the implementation subclass WindowAdapter and rewrite the windowClosing () method in the anonymous internal class, passed parameter: receivwevent object
Call the addActionListener () method of the Button object. The parameter: ActionListener object. This class is an interface. Therefore, use an anonymous internal class to implement this interface, implement the actionreceivmed () method, and pass in parameters: actionEvent object
Import java. awt. button; import java. awt. flowLayout; import java. awt. frame; import java. awt. event. actionEvent; import java. awt. event. actionListener; public class FrameDemo {private Frame frame; private Button button; public FrameDemo () {init ();}/*** initialize */public void init () {frame = new Frame ("test form"); frame. setBounds (300,200,200,200); frame. setLayout (new FlowLayout (); button = new Button ("exit"); frame. add (button); frame. setVisible (true); addEventAction ();}/*** add event */public void addEventAction () {// click the button to exit the button. addActionListener (new ActionListener () {@ Override public void actionreceivmed (ActionEvent e) {System. exit (0) ;}}) ;}/ *** @ param args */public static void main (String [] args) {new FrameDemo ();}}