Java Learning Diary Num13

Source: Internet
Author: User

Learning content: GUI overview; containers and components; Layout manager; event listener; menu; jar Package Double-click Execute

First, GUI overview

>graphical user Interface (GUI). > Graphical way to display the interface of computer operation, so it is more convenient and more intuitive. CLI >command Lines User Interface (command line interface)
Java provides the GUI with objects that are stored in java.awt and javax.swing. AWT and Swing java.awt:Abstract window Tookit (Abstract window Toolkit) need to invoke the native system method implementation functionality. belongs to the heavyweight control. Theoretically: The result program can be run on any platform, but the perception effect depends on the target platform.
Javax.swing: On the basis of AWT, a set of graphical interface system, which provides more components, and completely Java implementation. Enhanced portability.
Ii. Containers and Components
The general Component object cannot be displayed independently and must be placed in a container object. Component


2.1 Container Container is a subclass of component, and the container subclass object can accommodate other component objects. The container object can use the method add to add additional component objects to it. Container is a subclass of component, so container objects can be added to other container objects as component objects. Common container Two kinds of objects window: The top-level windows that can be parked freely. Panel: Its objects can accommodate other component, but cannot exist independently and must be added to other container to display
2.2 Label A label is usually used to indicate the purpose of the project and cannot be edited by the user. Label (): Create a new empty note. Label (String labeltext): Creates a new label that contains the given text. Label (String labeltext, int aligment): Creates a new label that contains the given alignment, which can be label.left,label.right or label.center.
2.3 TextField text Box

2.4 TextArea Text field

2.5 button 2.6 checkbox check box 2.7 radio ButtonYou can create a series of mutually exclusive check boxes with check boxes to implement radio button functionality. First, create a CheckboxGroup object. Then create a radio button.2.8 Choice Selection BoxUsed to display a selection list to the user. You can add items by using the AddItem () method.
third, layout managerThe FlowLayout (Flow layout manager) is ordered from left to right. Panel default layout manager.
BorderLayout (Border layout manager) east, south, west, north, Middle frame default layout manager.
Matrix of GridLayout (Grid layout manager) rules
CardLayout (Card layout manager) tab
GridBagLayout (Grid package layout manager) irregular matrices
Four, the event monitoring event monitoring mechanism features: 1, event source. 2, event. 3, listener. 4, event handling.
Event Source: The graphical interface component in AWT, or swing. Event: Each event source has its own unique corresponding event and common events. Listener: The action that can trigger an event (more than one action) is already encapsulated in the listener. To know which build has a unique listener, you need to look at the functionality of that Component object.
4.1 window Listener WindowListeneris an interface that contains 7 abstract methods, if implementing the interface must cover 7 methods, but we may only use one of the methods, the other is useless, no need to overwrite. Because WindowListener's subclass Windowadapter is called an adapter, has implemented the WindowListener, and covered all of them, then we just inherit from the Windowadapter to overwrite the method we need.
4.2 button Listener ActionListenerThere is only one method: Actionperformed ();
Mouse keyboard events are common events, defined in component Addmouselistener (); Removemouselistene (); Mouselistener[] Getmouselisteners (); Addkeylistener (); Removekeylistener (); Keylistener[] Getkeylisteners ();
MouseListener and KeyListener have corresponding adapters.
MouseEvent GetClickCount ();
KeyEvent getKeyCode (); return encoding

An example: The demo enters a path and prints out all the files under this path. Implemented with GUI

Package listawt;

Import java.io.*;

Import java.awt.*;

Import java.awt.event.*;

Class Listawtdemo

{

Private Frame F;

Private Button but;

Private TextField TF;

Private TextArea ta;

Listawtdemo ()

{

Init ();

}

public void Init ()

{

f = new Frame ("my frame");

F.setbounds (200,100,600,500);

F.setlayout (New FlowLayout ());

but = New button ("Go to");

tf = new TextField (40);

F.add (TF);

F.add (But);

Ta = new TextArea (80,70);

F.add (TA);

F.setvisible (TRUE);

MyEvent ();

}

public void MyEvent ()

{

F.addwindowlistener (New Windowadapter () {

public void windowclosing (WindowEvent e)

{

System.exit (0);

}

});

But.addactionlistener (new ActionListener () {

public void actionperformed (ActionEvent e)

{

Ta.settext ("");

String s = tf.gettext ();

File F = new file (s);

if (F.exists () &&f.isdirectory ())

{

string[] names = F.list ();

for (String name:names)

{

Ta.append (name+ "\ r \ n");

}

}

Tf.settext ("");

}

});

}

public static void Main (string[] args)

{

Listawtdemo la = new Listawtdemo ();

}

}

v. Menu MenuBar is added to an object by means of the Setmenubar () method of the object described.
Menu can be added to menu or add MenuItem
Six, jar package editing format jar-cvfm destination file name. jar Profile Source Folder
Configuration file

Content format:main-class:< There must be spaces > main function class name < There must be a carriage return > "Note: The above is a fixed format"
How do I make a jar package that can be executed by double-clicking? 1, multiple classes are encapsulated in a package. 2, define the configuration information for a jar package. Defines a file a.txt. The contents of the file are: Main-class: (space) package name. Class Name (carriage return) 3, hit jar package. JAR-CVFM My.jar a.txt Package name 4, verify with the WinRAR program to see if custom configuration information is available in the jar's configuration file.
5, through the tool-folder option-file type--jar type file, through advanced, define the jar type File Open Action Association program. Jdk\bin\javaw.exe-jar

Java Learning Diary Num13

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.