Java 22-day GUI graphical interface programming (i) __gui

Source: Internet
Author: User
Tags event listener
GUI (graphical user interface)

Import Java.awt.Button;
Import Java.awt.FlowLayout;
Import Java.awt.Frame;
Import Java.awt.event.WindowAdapter;

Import java.awt.event.WindowEvent;
 /** * GUI (graphical user interface) * Graphical user Interface (graphical user interface) * Graphically, to display the interface of computer operation, so that more convenient and intuitive.
 * * CLI * command line user Interface (command-line users interface) * is the Common DOS command line operation.
 * Need to memorize some common commands. The operation is more intuitive. * Examples: * For example: Create a folder, or delete a folder, etc. * md haha del haha * * Java GUI provides objects that are present in Java. AWT and javax.
 Swing two in a package. * * java. Awt:abstract window ToolKit (Abstract Window Toolkit) * You need to invoke the Local System method implementation feature. is a heavyweight control (not strong enough across platforms) * java. Swing: A set of graphical interface systems, based on AWT, that provide more components, * and are fully implemented by Java, enhance portability, and belong to lightweight controls.
 (cross-platform) * * Java.swt:IBM Company develops Eclipse's component tools that can be used when the Eclipse Web site is downloaded.
 * * * Layout Manager * 1) the way in which the components in a container are emitted is the layout.   * 2 Common Layout Manager * FlowLayout (streaming layout manager) * Order from left to right * panel default layout manager * BorderLayout (Justification layout manager) * Cardinal Medium * Frame Default layout manager * does not specify layout, default full screen overlay, add a full screen overlay * GridLayout (Grid layout manager) * Rules of the Matrix * CardLayout (Card cloth Bureau manager) * tabs
 * GridBagLayout (Grid Package layout manager) * Irregular Matrix * * Event listener mechanism * Event Source: * Events: Event * Listener: Listener * Time Processing: (Post event handling mode
 * * Event Source: These are the image interface components in the AWT package or swing package.
 * Event: Each event source has its own specific corresponding time and common time.
 * Listener: Actions that can start an event are encapsulated in the listener.
		* * Public class Guidemo {public static void main (string[] args) {frame f=new frame (' my awt ');
		F.setsize (500,400);
		F.setlocation (300,200);
		
		F.setlayout (New FlowLayout ());
		
		Button B=new button ("I am a button");
		
		F.add (b);
		
		F.addwindowlistener (New Mywin ());
		F.setvisible (TRUE);
	System.out.println ("Hello world!");
}//Because all the methods in the interface Windowlinstener are windowadapter implemented by the quilt class. and covers all of these methods, then we can only inherit Windowadapter override our method to class Mywin extends windowadapter{@Override public void windowclosing (WindowEvent e)
		{//TODO auto-generated Method Stub//system.out.println ("Window closing" +e.tostring ());
		System.out.println ("I turned it off");
	System.exit (0); @Override public void windowactivated (WindowEvent e) {//The SYSTEM.OUT.PRINTLN is triggered each time the focus is received ("I'm Alive").);
	Super.windowactivated (e);
		@Override public void windowopened (WindowEvent e) {//TODO auto-generated Method stub System.out.println ("I opened");
	Super.windowopened (e);

 }
	
}


Simple frame

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 Framedemo {//defines the reference of the required component in the image private Frame F;
	
	Private Button but;
		
	Framedemo () {init ();
		public void init () {f=new Frame (' my freame ');
		F.setbounds (300,100,600,500);  F.setlayout (New FlowLayout ());
		Use Flow layout but=new button ("my button");
		
		Add the component to the frame f.add (but);
		Load the events on the form.
		MyEvent ();
	Display Form f.setvisible (true); private void MyEvent () {F.addwindowlistener (new Windowadapter () {@Override public void windowclosing (Windoweven
				T e) {//TODO auto-generated Method stub//super.windowclosing (e);
			System.exit (0);
		
		}
		});
		 Let the button have the function of exiting the program/* button is the event source * Then select the Listener?
		 * By closing the form instance, learn that the next thing you want to know about that component is the unique listener, * you need to view the functionality of the Component object. //Add an active listener But.addactionlistener (new ActionlistenER () {@Override public void actionperformed (ActionEvent e) {//TODO auto-generated method stub System.
				Out.println ("Exit, Button dry");
			System.exit (0);
		
	}
		});
	public static void Main (string[] args) {Framedemo f=new framedemo ();
 }

}


keyboard and mouse events

Import Java.awt.Button;
Import Java.awt.FlowLayout;
Import Java.awt.Frame;
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 Mouseandkeyevent {private Frame F;
	Private Button but;
	
	Private TextField TF;
	Public mouseandkeyevent () {init ();
		private void Init () {F=new frame ("Me frame");
		F.setbounds (300, 200, 600, 500);
		
		F.setlayout (New FlowLayout ());
		Tf=new TextField (20);
		
		But=new button ("My Botton");
		F.add (TF);
		
		F.add (But);
		
		Event ();
	F.setvisible (TRUE); The private void event () {F.addwindowlistener (new Windowadapter () {@Override public void windowclosing (Windoweven
			T e) {//TODO auto-generated method stub system.exit (0);
		}
		
		}); Tf.addkeylistener (New KeyAdapter () {public void keypressed (KeyEvent e) {int code=e.getkeycode (); if (!) ( Code>=keyevent.vk_0 && code<=keyevent.vk_9)) {System.out.println (code+).
					illegal input ");  E.consume ();
				Do not perform the Join text box.
		
		
		
		}
				
			}
			
		}); But.addactionlistener (new ActionListener () {@Override public void actionperformed (ActionEvent e) {//TODO
			auto-generated method Stub System.out.println ("Actionperformed activity Once");
		
		}
		});
			But.addmouselistener (New Mouseadapter () {private int count=0;
			private int clickcount=1;
			public void mouseentered (MouseEvent e) {System.out.println ("mouse into the reshuffle" +count++);
				public void mouseclicked (MouseEvent e) {if (E.getclickcount () ==2) {System.out.println ("double-click Action");
				
			}else System.out.println ("click action" +clickcount++);
		}
			
		}); Add keyboard event But.addkeylistener (new Keyadapter () {public void keypressed (KeyEvent e) {System.out.println (E.getke Ychar () + "..." +e.getkeycode ());
				System.out.println (Keyevent.getkeytext (E.getkeycode ()) + "..." +e.getkeycode ());
					Enter to Exit/*if (E.getkeycode () ==keyevent.vk_enter) System. Exit (0); *//ctrl + Enter Send message if (E.iscontroldown () && E.getkeycode () ==keyevent.vk_enter) {Syste
				M.OUT.PRINTLN ("I want to send a message!");
	}
				
			}
		
		});
	public static void Main (string[] args) {//TODO auto-generated method Stub new Mouseandkeyevent ();

 }

}

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.