Java Basic Learning--swing graphical user interface Programming __java application Development--Interface design

Source: Internet
Author: User
Tags int size visibility
GUI Overview

In early computer systems, computers provide users with a "command-line interface (CLI)" That is monotonous, boring, and purely character-state. Even now, we can still faintly see their figure: Open a DOS window in Windows, you can see the footprints of history.

Later, Apple took the lead in implementing a graphical user interface (graphical user Interface, or GUI) in the computer's operating system, but because of Apple's closed marketing strategy, it completed its own computer hardware, operating system, and application software through one-stop product, Not compatible with other PCs. This leaves Apple with a chance to unified a global PC.

Later, the famous Microsoft company launched the world's most popular Windows operating system, with its excellent graphical user interface, has laid the status of operating system standards. This has also created the world's richest man---Bill Gates and the IT industry Taishanno Microsoft.

In the popular world of graphical user interface today, an application software without a good GUI is not acceptable to users. The Java language is also aware of the importance of this, and it provides a set of tools that can easily build a GUI.

The GUI building tools provided in the Java language can be grouped into "components" (component) and "containers" (Container).

In the Java language, the following components are available: Button label check box radio button selection box list box text box scroll bar Canvas Menu

These components, which we have encountered when using Windows operating systems, enable us to interact with the program by manipulating this to cause the component.

While light has "components" that cannot be assembled into programs, we must assemble these "components" into a whole by using "containers". The Java language also provides the following container: Form dialog box (Dialog)

The Java language provides these GUI components through the AWT (Abstract Windowing Toolkit) and Java base classes (JFC or more commonly used swing).

Where java.awt is the most original GUI toolkit, stored in the java.awt package. There are many features that have been replaced by swing and have been greatly increased and improved, so we rarely use java.awt, but AWT also contains the most core functionality, usually a Java GUI program uses at least a few of the following classes: Java.awt.Color: Basic Color definition java.awt.Font: Basic font definition java.awt.Cursor: cursor operation definition

And swing is a rich set of platform-independent ways that Java provides to create a library of graphical user interfaces. In this article, we'll learn about swing GUI controls. Swing Overview

The Swing API expands GUI components to ease the developer's life creation based on Java front-end/gui applications. It is built on the AWT API and replaced as the AWT API because it corresponds to AWT control for almost every control.

The Swing component follows the MVC (model-View-Controller) architecture, and MVC separates the code that is responsible for displaying the code, the code that processes the data, the response to the interaction, and the driver that drives the change.

MVC

Let's say we're going to have a fashion show, and if it's just one person to do it, then the person will be in a number of jobs: design, modify fashion, and show fashion to the catwalk.

So if we use the MVC pattern to simulate this, we won't let a person do all the things. We let fashion models show their costumes, and they play the role of view. Models know the right way to show clothes (data), but they don't know how to design and make costumes. We let fashion Designers act as controllers, and fashion designers have no idea how to walk on the catwalk, but he can design and make costumes. Fashion models and designers are able to handle garments independently, but they all have their own areas of expertise. This is the concept behind the MVC design pattern: "Separating roles"

Swing Features

Lightweight -swing components are standalone native operating system APIs and swing API control rendering mostly in pure Java code, rather than underlying operating system calls.

Rich Control -swing provides a rich set of advanced control systems such as trees, jtabbedpane, sliders, color selectors, table controls

highly customizable-swing controls can customize the visual appearance of a very simple method, independent internal representations.

Pluggable look and feel -based on swing GUI application skins and styles are based on available values and can be changed at run time. Components and Containers

JComponent

The underlying building block for swing's entire visual component library is JComponent, which is the parent class of all components, and is an abstract class, so we can't create jcomponent objects directly, but as the basis for class hierarchies. From the API we see that it contains hundreds of methods that each component of swing might use.

Components are rectangular shapes, the component itself has a default coordinate system, and the coordinate value of the upper-left corner of the component is (0,0). If the width of a component is 20 and the height is 10, then the maximum value of the X coordinate in the coordinate system is the maximum value of the 20;y coordinate is 10.

The component default border is a black-edged rectangle, and we can set the component's border by using the method [Java] view plain copy public void SetBorder (Border Border). The parameter of the method is an interface that must pass an instance of the implementation interface border class to the parameter. If you pass a null, the component cancels the border.

Component color: public void SetBackground (color c): Sets the background color of the component. public void Setforeground (color c): Sets the foreground color of the component. Public color Getbackground (color c): Gets the background color of the component. Public color Getforeground (color c): Gets the foreground color of the component. Public color (int red,int green,ing blue): Creates a Color object in which red, green, and blue values are between 0 and 255.

Component font: public void SetFont (font f): Component calls this method to set the font on the component. Public font getfont (font f): The component calls this method to get the font on the component.

The font class in the java.awt package is used in the above method, and the font class is constructed by: [Java] view plain copy public Font (String name,int style,int size)//Create Word Body object. Name is the font, style determines how the font is styled, and the value is an integer.

Size and position of the component: public void setSize (int width,int height): Sets the size of the component. public void setlocation (int x,int y): Sets the position of the component in the container, and the component is away from the container's left, top boundary x, y pixel. Public Dimension GetSize (): Returns a reference to a Dimension object that is the width and height of the current component in the object entity. Public point getLocation (int x,int y): Returns a reference to a point object that contains the x-coordinate and y-coordinate of the upper-left corner of the component in the container's coordinate system. public void setbounds (int x,int y,int width,int height): Sets the location of the component in the container and the size of the component. Public Rectangle getbounds (): Returns a reference to a Rectangle object that contains the x-coordinate and y-coordinate, width, and height of the upper-left corner of the current component in the container's coordinate system.

Components are opaque by default, and we can set the component opaque by using the method [Java] view plain copy public void Setopaque (Boolean isopaque). When Isopaque is false, the component is set to transparent, and the component is set to opaque when Isopaque is true. Method [Java] view plain Copy public boolean Isopaque () The method returns True when the component is opaque, or false.

Activation and visibility of components: public void setenabled (Boolean B): Setting whether a component can be activated.
When parameter B evaluates to TRUE, the component can be activated.
When parameter B evaluates to False, the component is not active.
By default, components can be activated.
public void setvisible (Boolean B): Sets the visibility of the component in the container.
When B evaluates to true, the component is visible in the container.
When b evaluates to False, the component is not visible in the container.
In addition to window-type components, other types of components are visible by default.

Let's get to know a couple of ways: Add (): Adds a component to the container. RemoveAll (): Removes all components from the container. Remove (Component c) Removes the component specified by the parameter in the container. Validate (): Whenever a container adds a new component or removes a component, the method is invoked to ensure that the components in the container are displayed correctly.

JFrame

The JFrame class is a top-level window and a container that allows you to add other components to it, organize them, and present them to the user. It has many other benefits, we first look at the simplest picture:

JFrame actually allows you to not only put the components in there and present them to the user. It is actually the most complex component in the Swing package, compared to its apparent simplicity. To maximize the simplification of components, JFrame plays a role between the operating system-independent swing components and the operating systems that actually run these components. JFrame is registered as a window in the native operating system, so you can get many familiar features of the operating system window: minimizing/maximizing, resizing, moving.

Common construction Methods: JFrame (): You can create a window without a caption. JFrame (String title): Create a window titled title

Common method: GetTitle ()/settitle (): Gets/Sets the caption of the window. Getstate/setstate (): Gets/sets the window to minimize, maximize, and so on. IsVisible ()/setvisible (): Gets/sets the visual state of the window (that is, whether it is displayed on the screen). Getlocation/setlocation (): Gets/sets the position where the window should appear on the screen. GetSize ()/setsize (): Gets/Sets the size of the window. SetBounds (): Gets/sets the initial position and size of the window when it appears on the screen. Setresizable (): Sets whether the window can be resized, and the window is resizable by default. Add (): Adds a component to the window. Setdefaultcloseoperation (): Sets how the program will handle when you click the Close icon in the upper-right corner of the form.

JDialog

dialog box class.

The JDialog class is a subclass of Windows. The dialog box must be dependent on a window or component, the dialog box disappears when the window or component it relies on disappears, and the dialog is automatically restored when the window or component it relies on is visible.

We create a dialog class by creating JDialog subclasses. You cannot add components directly to a dialog box, you cannot set a layout for a dialog box, you can use the Getcontentpane () method to get the content panel.

dialog boxes can be divided into modeless and modal.

If a dialog box is a modal dialog box, when the dialog box is active, only the program responds to events inside the dialog box, the program cannot activate the window or component it relies on, and it will block the current thread's execution until the dialog disappears.

When a modeless dialog box is active, the program can still activate the window or component it relies on, and it does not block the execution of the thread.

message dialog box:

The message dialog box is a modal dialog box. Before you perform an important action, it is a good idea to pop up a message dialog box to determine the operation.

You can use the static method of the Joptionpane class in the javax.swing package: [Java] view plain copy public static void Showmessagedialog (Component pare Ntcomponent,string message,string title, int messagetype)

Create a message dialog box. The parameters are the components on which the dialog box depends, the message displayed on the dialog box, the title of the dialog box, and the appearance of the dialog box.

Confirmation dialog box:

The confirmation dialog box is a modal dialog box. You can use the static method of the Joptionpane class in the javax.swing package: [Java] view plain copy public static int Showconfirmdialog (Component paren Tcomponent,object message,string title,int Optiontype)

Create a confirmation dialog box.

Color dialog box:

You can use the static method of the JColorChooser class in the javax.swing package: [Java] view plain copy public static Color ShowDialog (Component Componen T,string Title,color Initialcolor)

The

Creates a color dialog box. Parameter component specifies the component that the dialog box depends on, title specifies the caption of the dialog box, and Initialcolor specifies the initial color returned by the dialog box.

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.