3. java gui Programming Frame window, 3. javaguiframe

Source: Internet
Author: User

3. java gui Programming Frame window, 3. javaguiframe

Create a graphical interface:

1. Create a frame form;

2. Perform basic settings on the form;

Such as size, location, Layout

3. Define components;

4. add the component to the form using the add method;

5. display the form using setVisible (ture ).

------------------------------

Create a new class named studyframe

The functions are as follows:

1. Create a window;

2. The window has a title;

3. Window Size settings;

4. Window position settings;

5. The window is visible.

The Code is as follows:

Import java. awt. frame; class studyframe {public static void main (String [] args) {Frame f = new Frame (); // construct a new Frame instance () that is initially invisible ().
F. setTitle ("window -- by Nebula"); // you can specify the title of the window. setSize (500,400); // set the window size, width 500, height 400 f. setLocation (300,200); // set the window position to 300 from the horizontal direction on the left of the screen, and 200 f from the vertical direction on the top. setVisible (true); // sets the visible form. }}

Note: (the event listener is not mentioned in this article, so the form in this article cannot be closed directly.

To disable the service, you must stop the process through the process manager .)

The running effect is as follows:

The above code can also be written as follows:

Import java. awt. frame; class studyframe {public static void main (String [] args) {Frame f = new Frame ("window -- by Nebula "); // construct a new, initially Invisible Frame object with the specified title.
F. setSize (500,400); // set the window size, width 500, height 400 f. setLocation (300,200); // set the window position to 300 from the horizontal direction on the left of the screen, and 200 f from the vertical direction on the top. setVisible (true); // sets the visible form. }}

******************************

The functions are as follows:

1. Create a window;

2. The window has a title;

3. Window Size settings;

4. Window position settings;

5. The window is visible.

6. Add a button with a text label;

Import java. awt. button; import java. awt. frame; class studyframe {public static void main (String [] args) {Frame f = new Frame (); // construct a new Frame instance () that is initially invisible ().
F. setTitle ("window -- by Nebula"); // you can specify the title of the window. setSize (500,400); // set the window size, width 500, height 400 f. setLocation (300,200); // set the window position to 300 from the horizontal direction on the left of the screen, and 200 f from the vertical direction on the top. setVisible (true); // sets the visible form.
Button B = new Button ("I Am a Button"); // Add a Button in the window; f. add (B); // add the button to the window ;}}

The running effect is as follows:

Note: For details, see Borderlayout (border layout manager)

******************************

The above is definitely not easy to understand. We will optimize the layout mode.

The functions are as follows:

1. Create a window;

2. The window has a title;

3. Window Size settings;

4. Window position settings;

5. The window is visible.

6. Add a button with a text label;

7. Set the layout of the form.

Import java. awt. button; import java. awt. flowLayout; import java. awt. frame; class studyframe {public static void main (String [] args) {Frame f = new Frame (); // construct a new Frame instance () that is initially invisible (). F. setTitle ("window -- by Nebula"); // you can specify the title of the window. setSize (500,400); // set the window size, width 500, height 400 f. setLocation (300,200); // set the window position to 300 from the horizontal direction on the left of the screen, and 200 f from the vertical direction on the top. setVisible (true); // sets the visible form. F. setLayout (new FlowLayout (); // set the form layout to a streaming layout. Button B = new Button ("I Am a Button"); // add a Button in the window; f. add (B); // add the Button to the window }}

The running effect is as follows:

The above code can also be written like this,

Import java. awt. *; class studyframe {public static void main (String [] args) {Frame f = new Frame (); // construct a new Frame instance () that is initially invisible (). F. setTitle ("window -- by Nebula"); // you can specify the title of the window. setSize (500,400); // set the window size, width 500, height 400 f. setLocation (300,200); // set the window position to 300 from the horizontal direction on the left of the screen, and 200 f from the vertical direction on the top. setVisible (true); // sets the visible form. F. setLayout (new FlowLayout (); // set the form layout to a streaming layout. Button B = new Button ("I Am a Button"); // add a Button in the window; f. add (B); // add the Button to the window }}

 

References:

 

 

FrameIs a top-level window with a title and a border.

 

 

WindowAn object is a top-level window without borders and menu bar. The default window layout isBorderLayout.

When constructing a window, it must have a form, dialog box, or other Windows defined as its owner.

This class creates a tag button.

 

 


What are the basic methods for implementing GUI programming in Java?

In fact, no matter what platform, the basic development methods of GUI applications are similar. Generally, the following four steps are involved:

① Create a container

First, you need to create a GUI application. You need to create a carrier to accommodate all other GUI component elements, which is called a container in Java. Typical types include Window, Frame, Dialog box, and Panel. Only those containers are created first, and other interface elements such as buttons (Button/JButton), labels (Label/JLabel), and text boxes (TextField/JTextField) can be stored locally.

② Add Components

To implement GUI application functions and exchange with users, you need to add various components/controls on the container. This depends on the specific functional requirements. For example, if you need to prompt information, you can use a Label (Label/JLabel). If you need to enter a small amount of text, you can use a text box (TextField/JTextField). If you need to enter more text, available text area (TextArea/JTextArea); if you need to enter a password, the available password field (JPasswordField) and so on.

③ Arrange Components
Unlike traditional GUI software development tools in Windows, in order to better implement cross-platform, the positions and sizes of Components in Java programs are generally not measured in absolute quantities, it is measured by the relative amount. For example, the component location of the program is identified by the orientation of "East/East", "West/West", "South/South", "North/North", and "Medium/Center ".. Therefore, in addition to the types of components, you must also consider how to arrange the positions and sizes of these components. This is generally achieved by setting the Layout Manager and its related attributes. In fact, the above-mentioned components are arranged by orientation using the BorderLayout layout manager in multiple layout managers in Java.

④ Event handling

In order to complete the functions that a GUI application should possess, in addition to properly arranging various components to generate beautiful interfaces, it is also necessary to handle various Interface Element events so as to truly achieve exchanges with users, complete the functions of the program. In Java programs, this is generally done by implementing the appropriate event listener interface. For example, to respond to button events, you need to implement the ActionListener listener interface. To respond to window events, you need to implement the WindowListener listener interface.

Java gui Programming

Place the image or image display component in a scroll panel. ScrollPane is used in AWT and JScrollPane is used in swing. Ex:
// Swing
JLabel l_pic;
JScrollPane jscp;
//
Rochelle PIC = new JLabel (new ImageIcon ("xxx.jpg "));
Jscp = new JScrollPane (l_pic); // JLabel is constructed as a parameter. When the image size is larger than the form, the horizontal/vertical scroll bar is used for automatic adjustment.

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.