The most basic component in the Swing library is JLabel. What it does is what you expect: stay there, look beautiful, describe other components. The following figure shows the actual application of the JLabel:
Not very attractive, but still useful. In fact, throughout the application, JLabel is used to describe not only the composition but also the picture. Whenever you see a picture in a Swing application, it can be JLabel. JLabel Not many unexpected ways for Swing beginners. Basic methods include setting text, pictures, alignment, and other components of the label description: get/settext (): gets/sets the text of the label. Get/seticon (): gets/sets the picture of the label. get/sethorizontalalignment (): gets/sets the horizontal position of the text. get/setverticalalignment (): gets/sets the vertical position of the text. get/setdisplayedmnemonic (): gets/sets the access key (underlined text) of the label. get/setlablefor (): gets/Sets the component attached to this tag, so when the user presses ALT + access key, the focus shifts to the specified component.
JButton
The basic action component JButton in Swing is the same as the OK and Cancel buttons that are visible in every window; they do exactly what you want them to do-something happens when you click them. What the hell is going to happen? You must define what is happening (see the event for more information). A JButton instance looks like the following:
After doing so, you get all the JFrame attributes described above, most importantly the operating system's native support for the Windows. The next step is to put the components on the screen. In this example, a null layout is used. Later in the tutorial, you'll learn more about layout and layout manager content. But for this example, you can use a number to represent the pixel position on the JFrame: Public HelloWorld () {super ();
This.setsize (300, 200);
This.getcontentpane (). setlayout (NULL);
This.add (Getjlabel (), NULL);
This.add (Getjtextfield (), NULL);
This.add (Getjbutton (), NULL);
This.settitle ("HelloWorld");
Private Javax.swing.JLabel Getjlabel () {if (JLabel = null) {JLabel = new Javax.swing.JLabel ();
Jlabel.setbounds (34, 49, 53, 18);
Jlabel.settext ("Name:");
return JLabel; Private Javax.swing.JTextField Getjtextfield () {if (JTextField = null) {JTextField = new javax . swing.
JTextField ();
Jtextfield.setbounds (96, 49, 160, 20);
return JTextField; Private Javax.swing.JButton Getjbutton () {if (JButton = null) {JButton = new Javax.swing.JButt
On ();
Jbutton.setbounds (103, 110, 71, 27);
Jbutton.settext ("OK");
return JButton; }
The components are now on the JFrame and need to display JFrame on the screen and allow the application to run. Just as in all Java applications, you must add a main method in order for the Swing application to run. In this main method, you simply create the HelloWorld Application object and then call its setvisible ():public static void Main (string[] args)
{
HelloWorld w = new HelloWorld ();
W.setvisible (true);
It's done. This is all the process of creating an application.The complete code is as follows:
Package cn.edu.jnu.www;
Import javax.swing.*;
Import javax.swing.event.*;
Import java.awt.*;
Import java.awt.event.*;
public class HelloWorld extends jframe{private JLabel JLabel;
Private JTextField JTextField;
Private JButton JButton;
Public HelloWorld () {super ();
This.setsize (300, 200);
This.getcontentpane (). setlayout (NULL);
This.add (Getjlabel (), NULL);
This.add (Getjtextfield (), NULL);
This.add (Getjbutton (), NULL);
This.settitle ("HelloWorld");
Private Javax.swing.JLabel Getjlabel () {if (JLabel = null) {JLabel = new Javax.swing.JLabel ();
Jlabel.setbounds (34, 49, 53, 18);
Jlabel.settext ("Name:");
return JLabel; Private Javax.swing.JTextField Getjtextfield () {if (JTextField = null) {JTextField = new javax.swing.JTextFi
Eld ();
Jtextfield.setbounds (96, 49, 160, 20);
return JTextField;
Private Javax.swing.JButton Getjbutton () {if (JButton = null) {JButton = new Javax.swing.JButton (); Jbutton.setbounds (103, 110, 71, 27);
Jbutton.settext ("OK");
return JButton;
public static void Main (string[] args) {HelloWorld w = new HelloWorld ();
W.setvisible (TRUE); }
}