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 ProgramThe 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.