Normalize the classes you define and separate events and graphical components.
Define a class FrameDemo
Define member attribute Frame frame
Define member attribute Botton
Define the constructor FrameDemo()
Define the initialization method init()
In the initialization method, new Frame() comes out, parameter: String's form name
Call the setBounds() method of the Frame object, parameters: x, y, width, height
Call the setLayout() method of the Frame object, parameter: FlowLayout object
Get the Button object, new, construct the parameter: String button text
Call the add() method of the Frame object, parameters: Button object
Call the setVisible() method of the Frame object, parameter: true of Boolean
Define event method myEvent()
Call the addWindowListener() method of the Frame object, parameters: WindowListener object, WindowListener is an interface, there are seven methods to implement, find the subclass WindowAdapter, rewrite the windowClosing() method of the anonymous internal class, and pass in the parameters: WindowEvent object
Call the addActionListener() method of the Button object, parameter: ActionListener object, this class is an interface, so anonymous internal class is used to implement this interface, implement the method actionPerformed() method, and pass in the parameter: 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(); } /** * Initialization*/ 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(){ //Button exit button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); } /** * @param args */ public static void main(String[] args) { new FrameDemo(); }}The above article briefly talks about javaSE GUI (Action event) is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.