Use AWT to write your own second graphical interface in Java

Source: Internet
Author: User
Tags delete key

This example inserts a text box, entry button, single button, double click button and other action events!

Package com.xywei.awt;

Import Java.awt.Button;
Import Java.awt.FlowLayout;
Import Java.awt.Frame;
Import Java.awt.TextArea;
Import Java.awt.TextField;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import Java.awt.event.KeyAdapter;
Import java.awt.event.KeyEvent;
Import Java.awt.event.MouseAdapter;
Import java.awt.event.MouseEvent;
Import Java.awt.event.WindowAdapter;
Import java.awt.event.WindowEvent;

public class Myframe_2 {

Frame FR = new frame ();
Button BT = New button ();
TextField TXF = new TextField (70);
TextArea Txa = new TextArea ();

public void Mouseandkeyevent () {
Fr.setbounds (400, 100, 600, 400);
Fr.setlayout (New FlowLayout ());
Fr.settitle ("This example is an example showing the keyboard and mouse");
Bt.setlabel ("button");
Fr.add (TXF);
Txf.setsize (300, 300);
Fr.add (BT);
Bt.setsize (200, 100);
Fr.setvisible (TRUE);
MyEvent ();

}

public void MyEvent () {
Fr.addwindowlistener (New Windowadapter () {
public void windowclosing (WindowEvent e) {
SYSTEM.OUT.PRINTLN ("Prepare to close window ...");
System.out.println ("closed");
System.exit (0);
}

});

Txf.addkeylistener (New Keyadapter () {
/**
* @parame keypressed Set keyboard listener events, set only 0 to 9 digits
* You can delete a number using the DELETE key
*/
public void keypressed (KeyEvent e) {
Set the obtained key number, which is stored in ASCII
int keycode = E.getkeycode ();
if (! ( KeyCode >= keyevent.vk_0 && keycode <= keyevent.vk_9)) {
if (keycode! = keyevent.vk_back_space) {
System.out.println (keycode + = = = Key ILLEGAL!!!!!! ===");
E.consume ();
}
}
}

});

Bt.addactionlistener (new ActionListener () {

int countclick=0;
/**
* Click the mouse event
* @Override
*/
public void actionperformed (ActionEvent e) {
TODO auto-generated Method Stub
System.out.println ((++countclick) + "= = = Click mouse = = =");
}
});
Bt.addmouselistener (New Mouseadapter () {
int count=1;
/**
* @param mouseentered as soon as the mouse activity enters the button
* Just show it!
*/
public void mouseentered (MouseEvent e) {
System.out.println ((count++) + "= = = Mouse entered the button = = =");
}
/**
* mouseclicked Double click mouse event
*/
public void mouseclicked (MouseEvent e) {
int doubleclick=1;
if (E.getclickcount () ==2) {
System.out.println ("= = =" + (++doubleclick) + "= = = You double-click the mouse = = =");
}
}
});

}

/**
* Listen to the keyboard input number is 0-9
*
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
New Myframe_2 (). Mouseandkeyevent ();
}

}

Use AWT to write your own second graphical interface in Java

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.