In Java, you use AWT to write your own third graphical interface with a menu bar

Source: Internet
Author: User

The function of the program is to appear the menu bar, click the menu exit option to exit the form!

Package com.xywei.awt;

Import Java.awt.Button;
Import Java.awt.FlowLayout;
Import Java.awt.Frame;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import Java.awt.event.WindowAdapter;
Import java.awt.event.WindowEvent;

public class MyFrame {
Create a Frame
Frame MyFrame = new Frame ();
Create a button
Button MyButton = New button ();

public void MyFrame () {
Create a Frame
MyFrame = new Frame ();
Create a button
MyButton = new Button ();
Mybutton.setlabel ("OK button");
Set the various properties of the frame, distance left 400, top 300, window frame 200 wide, 150 high
Myframe.setbounds (400, 100, 700, 450);
Set the layout inside the window, with flow layout, border layout, grid layout
Myframe.setlayout (New FlowLayout ());
Add a button to the window frame
Myframe.add (MyButton);
Add an event to the form
Myframe.addwindowlistener (New Windowadapter () {
public void windowclosing (WindowEvent e) {
System.out.println ("===== will close the window =======");
System.out.println ("window is closed! ");
System.exit (0);
}

public void windowactivated (WindowEvent e) {

System.out
. println ("This is public void windowactivated (WindowEvent e) {}");

}

public void windowopened (WindowEvent e) {
System.out
. println ("public void windowopened (WindowEvent e) {} What is it???? ");
}

});

Add an event to the button
Mybutton.addactionlistener (new ActionListener () {

private int count = 1;

@Override
public void actionperformed (ActionEvent e) {
TODO auto-generated Method Stub
Button bt= (button) E.getsource ();
Frame fm= (frame) bt.getparent ();
Fm.add (New button ("button==" +count++));
Fm.validate ();
System.out.println ("count==" +count++);

}
});

Myframe.settitle ("This is my first window interface");
setting is visible
Myframe.setvisible (TRUE);
}

public static void Main (string[] args) {
MyFrame frame1 = new MyFrame ();
Frame1.myframe ();
}
}

In Java, you use AWT to write your own third graphical interface with a menu bar

Related Article

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.