Introduction to Swing (ii) Simple Swing widgets

Source: Internet
Author: User

-As with all the "x primer" tutorials, this tutorial also contains essential HelloWorld demos. But this example is useful not only to observe how Swing applications work, but also to ensure that they are set correctly. Once this simple application can run successfully, each subsequent example will also be able to run.


JLabel

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:

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:

JButton

The method used to change the JButton property is similar to the JLabel method (you may find that these properties are similar in most Swing components). They control text, pictures, and orientation: 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 access key (underscore character), when combined with the Alt button, causes the button to click.

In addition to these methods, I also want to introduce another set of methods that JButton contains. These methods take advantage of all the different states of the button. A state is a property that describes a component, usually with a true/false setting. In JButton, you can include the following possible states: Active/Inactive, checked/unchecked, mouse over/mouse away, pressed/not pressed, and so on. In addition, you can combine these states, such as buttons that can be selected while the mouse is passing. Now you may ask yourself what you want to do with these states. As an example, take a look at the back button on your browser. Notice how the picture changes when the mouse passes through it, and how the picture changes when the button is pressed. This button utilizes a different state. Each state takes a different picture, which is a common and effective way to prompt user interaction. The State method on the JButton is: Get/setdisabledicon () Get/setdisableselectedicon () Get/seticon () Get/setpressedicon () () get/ Setrollovericon () Get/setrolloverselectedicon () Get/setselectedicon ()
JTextField

The basic text component in Swing is JTextField, which allows the user to enter text in the UI. I'm sure you're familiar with text fields: to master this tutorial, you must enter a user name and password using a text field. You enter text, delete text, select text, move text around--swing you are responsible for all of these tasks. As a UI developer, you don't actually need to do anything when you use Jjtextfield.

In any case, this is what the JTextField looks like when it's actually used:

JTextField

When dealing with JTextField, you just need to focus on one method-which should be obvious-this is the way to set the text: Get/settext (), to get/set the text in JTextField. JFrame

So far, I've introduced three basic building blocks of Swing: Labels, buttons, and text fields, but now you need a place to put them, and you want users to know how to handle them. The JFrame class does this-it's a container that allows you to add other components to it, organize them, and present them to the user. It has many other benefits, but I think it's easiest to look at its pictures first:

JFrame

JFrame actually allows you to not only put the components in there and present them to the user. It is actually the most complex component in the Swing package, compared to its apparent simplicity. To maximize the simplification of components, JFrame plays a role between the operating system-independent Swing components and the operating systems that actually run these components. JFrame is registered as a window in the native operating system, so you can get many familiar features of the operating system window: minimizing/maximizing, resizing, moving. But for the purposes of this tutorial, it is sufficient to treat JFrame as a palette for the component. Some of the modified properties that can be invoked on JFrame are: get/settitle (): Gets/Sets the caption of the frame. get/setstate (): gets/sets the status of minimizing and maximizing frames. is/setvisible (): gets/sets the visual state of the frame, in other words, whether it is displayed on the screen. get/setlocation (): gets/sets the position where the frame should appear on the screen. get/setsize (): gets/sets the size of the frame. Add (): adds a component to the frame.
Simple Application

As with all the "x primer" tutorials, this tutorial also contains essential HelloWorld demos. But this example is useful not only to observe how Swing applications work, but also to ensure that they are set correctly. Once this simple application can run successfully, each subsequent example will also be able to run. The following illustration shows the completed example:

HelloWorld Sample

The first step is to create the class. Swing applications that place components on JFrame need to inherit the JFrame class, as follows:

public class HelloWorld extends JFrame

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); }

}


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.