Developing GUI programs with swing (basic knowledge)

Source: Internet
Author: User

GUI (graphics user Interface) graphical UI, which allows the user to look at the interface operation, is more intuitive than the console program and can provide richer functionality.

In Java, the support API for GUI operations is generally stored in the java.awt and javax.swing packages, but generally we choose javax.swing because the Swing interface development API is more powerful than AWT.

First, use the window


(i) using the JFrame Type Development window

In general, the Javax.swing.JFrame class is used for window display, JFrame common constructors are:

Public JFrame (String title) throws Headlessexception

Pass in an interface title and instantiate the JFrame object.

The main window operator functions inside the JFrame class are:

1. Setting title: public void Settitle (String title)

2. Set the position on the screen: public void setlocation (int x,int y)

where x is the horizontal axis of the upper-left corner of the window on the screen, and y is the vertical axis of the upper-left corner of the window. The upper-left corner of the screen is the Origin point.

3. Set size: public void setSize (int width,int Height)

4. Set Visibility: public void setvisible (Boolean B), True is visible, false is not visible

When you click the Close button on the window and the window disappears but the program does not end up running, you need to call the method:

Xxx.setdefaultcloseoperation (Jframe.exit_on_close); XXX is the window name.


(ii) Development window with JDialog type

The window created with the JDialog class is a dialog box. The most common constructors are:

Public JDialog (Frame owner,string Title,boolean model) throws Headlessexception

Where owner indicates that the dialog box's parent window is displayed, title is the caption, the model value indicates whether the window is modal, Window B is opened from window A, then a is the parent window of B, and if B is not closed, B is a modal window.


(iii) Use of controls

Controls, also known as components (Component), are collectively referred to as panels, buttons, text boxes, and so on. Controls generally have corresponding classes to implement, such as the JButton class to implement a Button control.

to add controls to the window, in order to better organize the control, usually add the control to the Panel, that is, the JPanel, and then add the Panel to the window, the popular speaking JFrame equivalent to the table, and jpanel for cloth, Jbutton,jlabel, etc. equivalent to the cup , chopsticks, cups, table-ware can be placed directly on the desk but not very standard, should be put on the tablecloth to

Since the interface is likely to be more complex, it is generally not written in the main function of the interface generation process, but rather write a class to inherit JFrame, in its constructor to initialize the interface.

  1. Tags: public JLabel (String text);

  2. Button: Public JButton (String text);

  3. Text box: public jtextfield (int columns), the number of columns that the argument displays for the text box

  4. Multiline text box: public jtextarea (int rows,int columns), the default text box does not have scroll bars, you need to use the JScrollPane class to use the scroll bar to pass the JTextArea object to its constructor. The JScrollPane object is then added to the interface.

  5. Password box: public jpasswordfield (int columns)

  6. Menu:

    The development menu has steps: First add the menu bar, then add the menu with the Add () method on the menu bar, and finally add the menu items to the menu, the constructors are:

    Instantiating the menu bar: Public JMenuBar ()

    Materialized menu: Public JMenu (String s)

    Instantiated menu item: Public JMenuItem (String s)



(iv) Use of Joptionpane

The Joptionpane class is commonly used to display a number of message boxes, input boxes, confirmation boxes, and so on.

  1. To display a message box:

    public static void Showmessagedialog (Component parentcomponent,object message) throws Headlessexception, where parameter one is the parent component, can be null, parameter two represents message content

  2. Show Input Box:

    public static String Showinputdialog (Object message) throws Headlessexception

  3. Show Confirmation Box:

    public static int Showconfirmdialog (Component parentcomponent,object message) throwsheadlessexception, same as parameter


Write so much, but Goose does not have any effect, because the Java document has everything above, in the process of interface development, we can not remember every function, read more documents is the right path.



This article from "Guardian" blog, reproduced please contact the author!

Developing GUI programs with swing (basic knowledge)

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.