[JavaSE] GUI (Action event), javasegui

Source: Internet
Author: User

[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 ();}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.