Lecture 1: GUI (User Graphic Interface)
I. overview
1. GUI: Graphical User Interface (GUI), a graphical user interface, is a way for computers to interact with users.
2. Two ways of computer-user interaction: GUI and CLI
GUI: Graphical User Interface, Graphical User Interface, graphical user interface, to display the computer operation interface, convenient and intuitive.
CLI: Command Line User Interface, Command Line User Interface, Command Line User Interface, which is a common Dos command line operation, must remember some commands, the operation is not intuitive.
3. Java also encapsulates this interface as an object, in which the objects are placed in two packages: the java.Awt package and the javax.Swing package.
The java.Awt package: Abstract Window Toolkit, or abstract window toolkit. To call the local system method to realize the function, it is a heavyweight control.
Javaax.Swing package: A set of graphical interface system based on AWT, which provides more components and is completely implemented by java, enhances portability and is a lightweight control.
II. Succession Diagram
Container For containers, it is a special component in which other components can be added through the add method.
Container Common Subclass: Window Panel.
Common subclasses of Window s: Frame Dialog
3. Layout Manager
1. Layout: Component Arrangement in Containers
2. Common layout managers:
FlowLayout: Flow Layout: Flow Layout Manager. Arrange from left to right, which is the default layout manager for Panel
BorderLayout: BorderLayout, the border layout manager, is the default layout manager for Frame. If there is only one component in the form, the entire form will be covered.
3) GridLayout: Grid Layout Manager, Rule Matrix
CardLayout: Card Layout: Card Layout Manager, Tab
5) GridBayLayout: Grid Packet Layout Manager, Irregular Matrix
3. If there are many layout modes, how to create the form interface? Steps:
1) First divide the form Frame into large areas, set up its layout manager, and add panel panel Panel.
2) Add components to Panel and set the layout manager of panel.
4. Simple Form Creation Process
1. Create Frame Form:
Frame f = new Frame("my Frame"); // Caption can be set, that is, form name
2. Basic settings for forms: size, location, layout, etc.
f.setSize(int wight,int hight); //Form size settings
f.setLocation(int x,int y); //Form Display Position Settings, Lateral and Longitudinal Coordinates
f.setBounds(int x,int y,int wight,int hight) can also be used directly to set size and location
f.setLayout(Layout layout), with parameters specified for layout managers, such as FlowLayout
3. Define components:
For example, Button b = new Button("my Button"); and // the name of the configurable component
4. Adding components to a form through the form's add method:
f.add(b); //Add button components to forms
5. Let the form display:
f.setVisible(boolean b); // Whether the form is displayed by setting whether the parameter is true or false
Example:
V. Event Monitoring Mechanism
1. Composition:
1) Event source (component): GUI components in awt or swing packages
Event: Each event source has its own unique corresponding and common events
3) Listener: Encapsulate actions that trigger an event (more than one action) into the listener.
4) Event handling: the way to deal with events after they are triggered.
2. Instructions for Use
The first three components have been defined in java and can be used directly by capturing their objects. What we need to do is to process the generated actions.
Steps:
1) Identify the source of the event (container or component). The listener is registered on the event source by the addXXXListener() method of the event source object. This method receives subclass objects of XXXListener, or subclass objects of XXXAdapter, a subclass of XXXListener.
2) Generally expressed by anonymous inner classes. When overriding a method, the parameters of the method are generally accepted by variables of type XXXEvent.
Such as:
Explain:
1) When an event is triggered, it is packaged as an object and passed to the variable of the parameter in the replication method. Including event source objects. Get it through getSource() or getComponent().
2) If you use subclasses to implement the Windows Listener interface, you need to override seven of these methods. You can only use the closing action in them. Other actions are not used, but you have to rewrite all of them. Because Windows Adapter, a subclass of Windows Lister, has implemented this interface and covered all of its methods. Then just inherit the Windows Adapter and override the required methods.
3) Identify events and process them. In fact, adding any listener requires adding any event.
Example:
Practice:
Lecture 2: Application
Dialog
When a dialog box needs to be generated: This object needs to be created when it is invoked. For example, when misoperation occurs, a dialog box prompting error information is needed to create the object at this time.
Example:
Enhanced functionality for listing examples of specified directory content.
Menu: Menu
1. Menu Inheritance
2, explain
Menu: Menu, inherit MenuItem; there are icons in the right triangle, Menu and MenuItem can be added.
2) MenuBar: Menu bar, you can add menus and menu items. Generally, first create the menu bar, then create the menu.
Menu Item: Menu item, also known as menu item, has no right triangle icon and is the final menu item.
4) Menu event processing is the same as component, adding activity listener to event source of type MenuItem and Menu, and processing related events.
5) Add the menu to the Frame through the setMenuBar() method.
Example:
3. Double-click execution of jar package
Since it is a graphical interface, you need to run the program in the form of a graphical interface, not on the Dos command line, so how to execute the program by double-clicking the program? This requires packing the class file of the program.
The steps are as follows:
1. First, import a package in a java file, and then create a package if not, such as package mymenu.
2. Generating packages: By compiling Java c-d:myclass MyMenu.java, all. class files are generated under the MyClass folder on the C disk.
3. Create a new file in this directory, such as 1.txt or any other file with any extension, and then edit it in a fixed format: "Main-Class: mymenu.MenuDemo", which only writes the contents in quotation marks. You need to have a space after the colon and return to the car at the end of the file.
Compile: jar-cvfm my.jar 1.txt mymenu. If you want to add other information, you can compile jar directly and get the corresponding command.
5. Double-click at this time to execute.
Explain:
1) In a fixed format:
(a) If there are no spaces: IO exceptions are reported at compilation time, indicating invalid header fields. This shows that 1.txt is read by IO stream.
b. If there is no carriage return, the information of loading the main class will not be added to the list. MF, that is to say, the name of the main class of the attribute of the configuration list will not be loaded into the list, and it will not be executed.
2) The jar file must be registered in the system before it can run. The registration method is as follows:
A. For XP systems:
Open any dialog box, click the Tool button in the menu bar, and select the folder option
b. Select the new - > extension, set the extension to jar, and confirm
c. Select Advanced, change the icon, and then click New, named open.
d. In executable applications, click browse, add the entire file path of bin under jdk, and add - jar after the path.
For win7 system:
a. Change the way you open it: Right-click the.jar file, click the way you open it, and select the javaw.exe application in bin under jdk as the default program.
b. Modify the registry of the associated program: Open the registry (win+r), find the registry path HKEY_CLASSES_ROOT Aplications javaw.exe shell open command, right-click the string value, and add-jar to the original path, such as: Program Files Java j6 bin javaw.exe - jar "%1". Note that there should be spaces on both sides of the jar. Save.
c. Double-click to execute the jar program, and if it still can't execute, download the latest version of jdk.
Example: