Java graphical interface Design-container (JFrame)

Source: Internet
Author: User
Tags new set

Java graphical interface Design-container (JFrame)

The program is for the convenience of users, so the implementation of graphical interface programming is the inevitable trend of development of all languages, the program run at the command prompt allows us to understand the basic knowledge of Java program architecture, now into the Java graphical interface programming.

First, Java Basic Class (JFC)

Java Basic Class ("Java foundationclasses", JFC), consists of a number of packages. These packages mainly include some of the following application interfaces (APIs):
• Abstract Window toolset (AWT) (1.1 and later).
· Swing widget.
· Java 2D Application Interface (2D API).
• Compatible program interfaces.
The application interfaces listed above can be difficult to appear in multiple packages. For example: The 2D API exists in both the java.awt and Java.awt.image packages, although some special packages like Java.awt.geom support 2DAPI, but a large number of 2D API classes exist in the java.awt package.
AWT (version 1.1 and above) is the core of JFC. AWT provides the following basic structure for the composition of JFC:
• Agent Event model.
• Light weight components.
• Clipboard and data transfer.
• Print and no mouse operation.

1. awtabstract window Toolkit (AWT) Abstract Windowing Toolkit

AWT is the older dual-interface function library, it still highly relies on the work platform itself has the function of drawing function, and with a smaller than the inclusion Program (Wrapper) is also a small design to remove, absorb the graphics functional differences between the various platforms, so that the Java program after the cross-platform portability of the rendering is not consistent, This has led to numerous criticisms of AWT. This means that the AWT widget relies on the original widget function of the operating platform itself, and the programmer must understand the differences in the widgets of each job platform while developing and writing, thus deviating from the original purpose of the Java programming language: cross-platform consistency.

AWT is provided by the java.awt package, which provides classes and interfaces that support GUI design, while the AWT-provided build is generally referred to as a heavyweight component, which is implemented by local methods. Features of AWT:

The graphical functions in AWT have a one by one correspondence with the graphics functions provided by the operating system .

L AWT realizes its platform independence by sacrificing function

L AWT does not meet the needs of graphical user interface development

2. Swing Package

To address the shortcomings of AWT components, especially cross-platform issues, A new swing package was provided after the JDK1.2 version, with javax.swing provided, Swing is a new set of graphical interface components built on the basis of AWT, all of which are written by Java and have a good cross-platform, since swing does not use the native method to implement the graphics function, it proposes to put the swing Components are called lightweight components.

Note: The swing build is based on the AWT, not the alternative AWT, AWT is the foundation and swing is the development.

Swing components are almost all lightweight components, except for top-level containers: forms, small applications, Windows, and dialog boxes. Because lightweight components are drawn in the window of their container, rather than in their own windows, lightweight components must eventually be included in a weight container. As a result, swing forms, small applications, Windows, and dialog boxes must all be weight components to provide a window on which swing lightweight components can be drawn.

Swing includes more than 250 classes, some of which are UI components, and some are support classes. To separate the UI components from the supporting classes, the name of the swing component begins with J. Table 1-1 Lists the J components provided by swing. The component represented in italics is an alternative component of the AWT component.

Table 1-1 Swing UI Components
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
   Component Class Description
─────────────────────────────────
JApplet extension of the Java.applet.Applet class, which contains an instance of JRootPane
   JButtonA button that displays text and graphics, which is an alternative component of the AWT button component
   JcheckboxA check box that displays text and graphics, which is an alternative component of the AWT selection component
   JCheckBoxMenuItemA check box menu item, which is an alternative component of the AWT CheckBox menu item Component
   JComboBoxA text box with a drop-down list, which is an alternative component of the AWT selection component
JComponent base class for all light weight J components
JDesktopPane Container for internal forms
   JDialogThe base class for the Swing dialog box, which extends the AWT Dialot class
JEditorPane Text pane for editing text
   JFrameExternal forms for extending java.awt.Frame
JInternalFrame internal forms that appear in JDesktopPane
   JLabelA label that displays text and icons, which is an alternative component of the AWT label component
JLayeredPane can display the container of a component on different layers
   JListA component that displays a list of options, which is an alternative component of the AWT list component
   JMenuA menu displayed in the menu bar, which is an alternative component of the AWT menu component
   JMenuBarmenu bar for displaying menus, which is an alternative component of the AWT menu bar component
   JMenuItemmenu item, which is an alternative component of the AWT menu item component
   JoptionpaneDisplays standard dialog boxes, such as: Messages and Issues dialog box
   JPanelGeneric container, which is an alternative component of the AWT panel and canvas components
JPasswordField the JTextField extension so that the input characters are not visible
   JPopupMenuPop-up menu, which is an alternative component of the AWT pop-up menu component
JProgressBar Progress Indicator
   JradiobuttonRadio button, which is an alternative component of the AWT check box component
JRootPane top-level container, which contains a pane of glass, a layer pane, a content pane, and an optional menu bar
   JscrollbarScroll bar, which is an alternative component of the AWT ScrollBar component
   JScrollPaneScroll pane, which is an alternative component of the AWT scrolling pane component
Jseparator Horizontal OR Vertical separator bar
JSlider Sliding Bar
JSplitPane a container with two compartments, which can be arranged horizontally or vertically and the size of the divider automatically adjusts
JTabbedPane with tabs pane
JTable form
Jtableheader table Header
   JTextAreaText field for entering multiline text, which is an alternative component of the AWT text field component
   jtestcomponentThe base class of the text component, which replaces the AWT TextComponent class
   JTextFieldSingle-line Text field, which replaces AWT's single-line text field component
   JtextpaneA simple text editor
Jtogglebutton button for two states, which is the base class for Jcheckbox and Jradiobutton components
JToolBar Tool Bar
JToolTip when the cursor rests on a component, a line of text that appears on the component
JTree structure controls for organizing data in a button hierarchy
Jviesport for navigating the viewport of scrollable components
   JWindowExternal window, which is an extension of Java.awt.Window
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Note: Italics represent an alternative component of AWT

Ii. the difference between AWT and swing

AWT is an abbreviation for the abstract window ToolKit (Abstraction Windows Toolkit), which provides a set of interfaces for interacting with the local graphical interface. The graphical functions in AWT have a one by one correspondence with the graphical functions provided by the operating system, which we call peers. In other words, when we use AWT to build a graphical user interface, we are actually using the graphics library provided by the operating system. Features that exist on one platform may not exist on another because of the different functionality that is provided by the graphics library of the operating system. In order to implement the concept of "once compiled, run everywhere" in the Java language, AWT has to implement its platform independence by sacrificing functionality, that is to say, the graphical capabilities provided by AWT are the intersection of the graphical functions provided by various general-purpose operating systems. Since AWT relies on local methods to implement its functionality, we often refer to AWT controls as heavyweight controls.

Swing is a new graphical interface system built on AWT that provides all the functionality that AWT can provide, and dramatically expands the functionality of AWT with pure Java code. For example, not all operating systems provide support for tree controls, and Swing uses the basic graphing methods provided in AWT to simulate tree controls. Because Swing controls are implemented with 100% of Java code, tree controls designed on one platform can be used on other platforms. Since there is no local method to implement graphics functionality in swing, we often refer to swing controls as lightweight controls.

The basic difference between AWT and swing: AWT is a local method-based C/s + + program that runs faster, and swing is an AWT-based Java program that runs slower. For an embedded application, the hardware resources of the target platform are often very limited, and the running speed of the application is an important factor in the project. In this case, the simple and efficient AWT is, of course, the first choice for embedded Java. In standard Java applications based on PCs or workstations, the limitations of hardware resources on applications are often not critical in the project, so swing is advocated in the standard version of Java, that is, by sacrificing speed to implement the functionality of the application.

Briefly speaking:

AWT is the abstract Window Component Toolkit, the earliest Java development package for programming graphical programming applications.

Swing is a newly developed package to solve the problem of AWT, which is based on AWT.

Iii. the basic framework of swing


The relationship between AWT and Swing is listed, and swing is inherited from AWT. This paper mainly uses swing to design graphical interface program.

Four, graphic design steps

Graphical interface program Everyone has used, the general use of a program process is: Open a program appears a window or dialog box, which generally has a menu, toolbars, text boxes, buttons, radio boxes, checkboxes and other controls (component is the control), the user input relevant data, click the relevant menu, button, The program processes the data, displays or saves the processed data, and finally closes the program.

Use the Java programming related design steps to decompose the above program to run the process as shown:

1. Create a top-level container

corresponding to the program's initial Presentation window, the window is placed in other menus, toolbars, text boxes, buttons and other components

The top-level container is the basis for graphical interface display, and all other components (controls) are displayed directly or indirectly in the top-level container. There are three top-level containers in Java, namely JFrame (frame window, the usual window), JDialog (dialog box), JApplet (for designing Java applets embedded in Web pages).

2. Create intermediate containers, components

Corresponds to the menus, Toolbars (intermediate containers), text boxes, buttons, radio boxes, check boxes, and other controls that appear in the program.

There are many swing components that can be used, see the previous Swing UI component table.

3. Adding components to the container

After you create a component in Java, you also need to put the component in the appropriate container to display the component in the top-level container, such as a window.

4. Set the position of the components within the container

Component to the container, you must also set the display location of the component, there are generally two ways to set the display location of the assembly, the first is the relative distance (in pixels) of the container, the exact location of the control, and the other is to use the layout manager to manage the position of the component within the container.

5. Handling events generated by the component

That is, when the user executes the selection menu, clicks the button, and so on, executes the corresponding command, carries on the related program processing, this needs to set up the component's event.

V. Use of component containers

Component containers in Java contain top-level containers and intermediate containers.

There are three top-level containers in Java, namely JFrame (frame window, normal window), JDialog (dialog box), JApplet (Java applet designed for embedding in Web pages), and top-level containers as the basis for other components, i.e. The design of a graphical program must have a top-level container.

A Java intermediate container is a container that can contain other corresponding components, but the intermediate containers and components cannot exist alone and must be attached to the top-level container.

The Common intermediate containers are:

JPanel: The most flexible, most commonly used intermediate container.

JScrollPane: Similar to JPanel, but also provides scroll bars around large components or extensible components.

JTabbedPane: Contains multiple components, but only one component at a time. Users can easily switch between components.

JToolBar: Arranges a set of components (usually buttons) by row or column.

1, the use of JFrame frame window



The above is a common method of JFrame is described, the following examples illustrate.

Example 1: Create a window that directly defines an object of the Jframe class

Jframedemo1.java

Import javax.swing.*; With the swing class, you must introduce a swing package

public class jframedemo1{

Public staticvoid Main (String args[]) {

Define a Form object F, the form name is "a simple window"

Jframe f = new Jframe ("A simple Window");

/* Set the coordinates of the upper left corner of the form with the upper left corner of the display

300 pixels from the top edge of the display, 300 pixels from the left edge of the display */

F.setlocation (300, 300); F.setlocationrelativeto (NULL); This statement implements the window in the center of the screen

F.setsize (300,200); Set the size of the form to 300*200 pixels

F.setresizable (FALSE); Set whether the form can be resized, the parameter is a Boolean value

The setup form is visible, without the statement, the form will not be visible, this statement must have, otherwise no interface will be meaningless

F.setvisible (TRUE);

What the program does when the user clicks the Close button of the window

F.setdefaultcloseoperation (F.exit_on_close);

}

}

The results of the program run are as follows:

You can modify the corresponding method parameters to see the effect according to the comments of the program code.

setdefaultcloseoperation (int operation): Sets the action that the user performs by default when "Close" is initiated on this form. The parameters in the method are explained as follows:

L is "0" or Do_nothing_on_close:

(defined in windowconstants): do nothing; ask the program to handle the operation in the Windowclosing method of the registered WindowListener object.

For example, change the instance program code to F.setdefaultcloseoperation (f. do_nothing_on_close), or f.setdefaultcloseoperation (0), and then look at the effect, You can see that the window cannot be closed, and the following is the same usage, no longer explained.

L is "1" or hide_on_close

The form is automatically hidden after any registered WindowListener object is called. The program is not closed at this time, only the program interface is hidden.

You can open Task Manager, you can see a process called "Java.exe" (if debugging runs multiple Java programs, you will see more than one "java.exe" process), if you use EditPlus test program, you will find that when you click the window's Close button to close the window, It is not possible to debug the program again because the program thread is not closed, and the Java.exe (if there are multiple "java.exe" processes, which are closed first, and then tested for the problem), will not be able to recompile the program until it is shut down in Task Manager.

L is "2" or dispose_on_close

Automatically hides and releases the form after calling any registered WindowListener object. However, you continue to run the application, freeing up the resources used in the form.

L is "3" exit_on_close (defined in JFrame): Exits the application using the System exit method. Used only in the application. End the application.

By default, this value is set to Hide_on_close.

When commenting out the F.setdefaultcloseoperation (F.exit_on_close) in the instance, the effect and the F.setdefaultcloseoperation (f. Hide_on_close) When the statement is played; or f.setdefaultcloseoperation (1);

Example 2: Create a class-inheriting JFrame class to create a window

Jframedemo2.java

importjavax.swing.*;

Create subclass MyFrame Inherit parent class JFrame

Class Myframeextends jframe{

Defines a constructor with four parameters for setting the window position and size

MyFrame (int x,int y,int H,int w) {

Super ("A simple Window"); Overriding the parent class method, defining a caption for the window

/* The following method call does not have an object name added before, and it is not possible to add the object name to be created without knowing what it is. However, after the object is created, the object automatically obtains these initial values. */

setlocation (x, y);

SetSize (H,W);

Setresizable (FALSE);

SetVisible (TRUE);

Setdefaultcloseoperation (Exit_on_close);

}

}

Publicclass jframedemo2{

public static void Main (String args[]) {

Instantiate class object, prompt to set parameters

MyFrame f = new MyFrame (300,300,300,200);

}

}

The above example can also be used without a constructor, as in the instance, the code can be changed to the following pattern.

Example 3: A simple window

Jframedemo3.java

importjavax.swing.*;

Class Myframeextends jframe{

MyFrame () {

}

}

Publicclass jframedemo3{

public static void Main (String args[]) {

MyFrame f = new MyFrame ();

F.settitle ("A simple Window");

F.setlocationrelativeto (NULL);

F.setsize (300,200);

F.setresizable (FALSE);

F.setvisible (TRUE);

F.setdefaultcloseoperation (3);

}

}

http://blog.csdn.net/liujun13579/article/category/1172073

Java graphical interface design-containers (JFrame)

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.