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