Java graphical programming

Source: Internet
Author: User

Reproduced

To learn the graphical programming of Java Swing, we first need to understand the three most basic concepts: top-level containers, controls, layouts.

Here's a look at these three basic concepts

1. Top-level containers

What is a top-level container? When we use Java for graphical programming, where do the graphs draw? We need a container that can provide graphical drawing, which is called the top-level container, and you can think of it as a window. The top-level container is the basis for graphical programming, and all graphical things must be included in the top-level container. In swing, we have three types of top-level containers that we can use, namely:

· JFrame: Used to design an application similar to a window form in a Windows system.

· JDialog: Similar to JFrame, except that JDialog is used to design dialog boxes.

· JApplet: Used to design Java applets that can be embedded in a Web page.

If you need to use swing to make a window class program, our code should look like this:

Import javax.swing.*;

public class Kyodaiui extends JFrame {

......

}

2. Controls

Controls are the basic elements that make up an application interface, such as buttons, text boxes, progress bars, and so on, which are controls. Control (Here we only discuss visual controls) and can be divided into container controls and non-container controls. In a literal sense, a container control is a special control that can contain other controls, for example, JPanel controls in Java are container-type controls, and we can place non-container controls such as buttons, text boxes, and so on in JPanel, and you can even place several JPanel controls in JPanel ( It is important to note that the top-level container is also a container-type control, and there can be only one top-level container control in each window application, in other words, the top-level container cannot be included in other controls.

There are many container controls in Java, except for the JPanel mentioned earlier, there are JTabbedPane, JScrollPane and so on, non-container control has JButton, JLabel, JTextField and so on. If you need to add a control to a container-type control, you can use the Add (Component Comp) method to implement it, such as:

>jpanel panel = new JPanel ();

JButton button = new JButton ();

Panel.add (button);

3. Layout

What is a layout? Layout is an interface management system used in Java to control the placement of controls. People who have used other visual programming languages have always felt that the Java interface design was awkward when they first touched the Java interface design: There was no way to provide WYSIWYG control coordinates! However, it turns out that the layout management system provided by Java itself is as good as it can be, and is more advantageous in cross-platform performance.

The common layouts are:

· BorderLayout: The interface is divided into the upper and lower and the middle area of the management system, in the BorderLayout layout, you can only put up to 5 controls, if more than 5 controls, it is recommended to choose another layout system.

· Gridlayout:gridlayout is a layout management system that cuts the user interface into a chessboard. GridLayout is undoubtedly the best choice if we are to design a calculator software that is similar to the one that comes with Windows.

· Flowlayout:flowlayout is not the same as the above two types of layout management system, in FlowLayout you do not have to specify where each control is placed, you only need to add the control to the FlowLayout, FlowLayout will place the controls in the order in which you added the controls, and will wrap automatically if there is not enough space.

Iv. General statement:

All of the components in swing are placed in containers, and the main containers are: Jframe,jpanel,jwindow,jdialog,jpanle. JFrame is the main framework of Java, and almost all Java application interfaces are designed within the main framework. Some containers are not a layer, but consist of several layers of panels, which will be used in this article: Split panel (JSplitPane), Content Panel, tab-style panel (TabbedPane)

Split panel: Splits the two subcomponents in the specified direction and manner, remembering that a split panel can only split two sub-components, and if you want to split three, let's start by thinking about it. The answer is given at the end of this section.

Content panel: Jframe,jwindow,jdialog is the container with multilayer panels mentioned above, and if you want to place components on top of those containers, you must place them on their content Panel (pane). We can get the content panel of the current container through the function Getcontentpane ().

tab-style panel: This panel is like a card, it provides a folder-style tab on it, currently only displays one page, if you want to go to the next page, just click on the tab above.

Some, such as JPanel, are containers where components can be placed directly. So you don't need to use a panel.

If you want to arrange the components placed on the container according to your own ideas, we must use the layout manager to manage them. The default layout manager for the Java system is the boundary Manager (BorderLayout). It divides the layout into five chunks, the central Area (center), the top (north), the bottom (south), the left (West), the right (East), and you may find that our main interface is the layout structure used. Several other layout managers are:

Flow layout (FlowLayout): Arranges the components in the flow from left to right, top to bottom.

Grid layout (GridLayout): Place components in each grid of settings in the layout

Unordered Grid Layout (GridBagLayout): Similar to Grid layout, but more powerful and more complex. Can handle all the layouts.

Java graphical programming

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.