Simple Introduction to Java Swing

Source: Internet
Author: User

Swing is a GUI toolkit designed for Java.

Swing is part of the Java Foundation Class.

Swing includes graphical user interface (GUI) devices such as text boxes, buttons, splitter panes, and tables.

Swing offers many better screen display elements than AWT. They are written in plain Java, so they can run across platforms as well as Java itself, unlike AWT. They are part of the JFC. They support replaceable panels and themes (specific themes that are the default for various operating systems), but instead of actually using the devices provided by the native platform, they mimic them only on the surface. This means that you can use any panel supported by Java on any platform. The disadvantage of lightweight components is that execution is slow, and the advantage is that uniform behavior can be applied across all platforms.

Hello World Program

The Helloworldswing.java file code is as follows:

ImportJavax.Swing.*;Public Class Helloworldswing { /**{* Create and display GUI. For thread-safety reasons, * This method is called in the event invocation thread. */ Private Static voidCreateandshowgui() { Make sure a nice look style JFrame.setdefaultlookandfeeldecorated(True); Creating and setting windows JFrameFrame= New JFrame("Helloworldswing");Frame.Setdefaultcloseoperation(JFrame.Exit_on_close); Add "Hello World" tab JLabelLabel= New JLabel("Hello World");Frame.Getcontentpane().Add(Label); Display windowFrame.Pack();Frame.setvisible(True); } Public Static voidMain(String[]Args) { //show apps Gui Javax.< Span class= "PLN" >swing. Swingutilities. Invokelater (new runnable  ()  { publicvoid Run ()  {  Createandshowgui (); } });  }}       /span>                

Execute the following command to output the result:

Helloworldswing.  Helloworldswing 

A User login Box instance

The Swingloginexample.java file code is as follows:

ImportJavax.Swing.JButton;ImportJavax.Swing.JFrame;ImportJavax.Swing.JLabel;ImportJavax.Swing.JPanel;ImportJavax.Swing.JPasswordField;ImportJavax.Swing.JTextField; Public Class Swingloginexample { Public Static voidMain(String[]Args) { Creating an JFrame instance JFrameFrame= New JFrame("Login Example"); Setting the width and height of frameFrame.SetSize(350, 200);Frame.Setdefaultcloseoperation(JFrame.Exit_on_close); /* Create panel, this HTML-like div tag * We can create multiple panels and specify locations in JFrame * Panel we can add text fields, buttons and other components. */ JPanelPanel= New JPanel(); Add panelFrame.Add(Panel); /* * Call user-defined methods and add components to panel */Placecomponents(Panel); The settings interface is visibleFrame.setvisible(True); } Private Static voidPlacecomponents(JPanelPanel) { /* Layout section We don't have much to do here. * This way set the layout to NULL */Panel.SetLayout(Null); Create JLabel JLabelUserlabel= New JLabel("User:"); /* This method defines the location of the component. * SetBounds (x, y, width, height) * x and Y Specify the new position in the upper-left corner, and the new size is specified by width and height. */Userlabel.SetBounds(10,20,80,25);Panel.Add(Userlabel); /* * Create a text field for user input */ JTextFieldUsertext= New JTextField(20);Usertext.SetBounds(100,20,165,25);Panel.Add(Usertext); Enter a text field for the password JLabelPasswordlabel= New JLabel("Password:");Passwordlabel.SetBounds(10,50,80,25);Panel.Add(Passwordlabel); /* This is similar to the text field for input * But the information entered is replaced with a dot, which is used to contain the security of the password */ JPasswordFieldPasswordtext= New JPasswordField(20);Passwordtext.SetBounds(100,50,165,25);Panel.Add(Passwordtext); Create Login button jbutton Loginbutton =  jbutton ( "login"  Loginbutton. (10, 80, 80, 25< Span class= "pun");  Panel.loginbutton}}          /span>                

Execute the following command to output the result:

Swingloginexample.  Swingloginexample 

Concept Analysis:

The basic idea of JFrame –java GUI program is based on JFrame, which is the object of window on screen, which can be maximized, minimized and closed.

JPanel
–java Graphical user interface (GUI) Toolkit The Panel container class in swing, included in the Javax.swing package, can be nested, and functionality is a lightweight container that combines components with the same logical functionality in a form. Can be added to the JFrame form:

JLabel –jlabel objects can display text, images, or both. You can specify where the label content is aligned in the label display area by setting vertical and horizontal alignment. By default, labels are centered vertically within their display area. By default, labels that display only text are the start edge alignment, while labels that display only the image are centered horizontally.

JTextField – A lightweight component that allows you to edit a single line of text.

JPasswordField – allows us to enter a line of characters like the input box, but hide the asterisk (*) or point create password (password)

JButton An instance of the –jbutton class. Used to create "Login" in a similar instance of a button.

Simple Introduction to Java Swing

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.