14.4-Full Stack Java notes: javax.swing What are the common controls? How to use it?

Source: Internet
Author: User

Common basic controls

Javax.swing.JButton

In the graphical interface program, the button is probably one of the most used controls, and the JButton class in the Javax.swing package is used to create the button. As shown in table 1, the common construction method for JButton.

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/9E/73/wKiom1mRSt2i9ibUAAEhQZBNBM0156.png "title=" Tu 1. PNG "alt=" Wkiom1mrst2i9ibuaaehqzbnbm0156.png "/>

Javax.swing.JLabel

The JLabel control is one of the simplest swing components for displaying labels on a form, JLabel can display both text and images. As shown in table 3, the common construction method for JLabel.

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/9E/73/wKiom1mRSvyhTlFGAACuq9PAL7A247.png "title=" Tu 2. PNG "alt=" Wkiom1mrsvyhtlfgaacuq9pal7a247.png "/>

Attention:

JLabel can only be used to display text and icon information, and users cannot modify it.

Javax.swing.JTextField

JTextField is also a lightweight control that allows users to edit single-line text. As shown in table 5, the common construction method for JTextField.

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/9E/61/wKioL1mRSxPybEYVAAD3IVXaAps513.png "title=" Tu 3. PNG "alt=" Wkiol1mrsxpybeyvaad3ivxaaps513.png "/>

Javax.swing.JPasswordField

JPasswordField is a lightweight component that allows you to edit a single line of text whose view indicates what you typed but does not display the original characters. It is a single-line password box control, specifically used in the same manner as JTextField, so do not repeat.

Now we use the above mentioned control to implement a landing window, the effect is as follows:

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/9E/73/wKiom1mRSzbTO_Y1AAC74Xh1hUI517.png "title=" Tu 4. PNG "alt=" Wkiom1mrszbto_y1aac74xh1hui517.png "/>

"Example 1" using a control to implement a login window

Package cn.sxt.views.testlogin;

Import Javax.swing.JButton;

Import Javax.swing.JFrame;

Import Javax.swing.JLabel;

Import Javax.swing.JPanel;

Import Javax.swing.JPasswordField;

Import Javax.swing.JTextField;

public class Loginframe extends JFrame {

Container

Private JPanel Pnlmain;

Label control

Private JLabel lbltitle;

Private JLabel Lblusername;

Private JLabel lbluserpwd;

Enter a text box control for the user name

Private JTextField txtUserName;

Enter a password for the Password box control

Private JPasswordField pwduserpwd;

Login and Exit button controls

Private JButton Btnlogin;

Private JButton Btnquit;

Public Loginframe () {

Instantiating containers and various controls

Pnlmain = new JPanel (null);

Lbltitle = new JLabel ("User login");

Lblusername = new JLabel ("User name:");

Lbluserpwd = new JLabel ("User password:");

txtUserName = new JTextField ();

Pwduserpwd = new JPasswordField ();

Btnlogin = new JButton ("login");

Btnquit = new JButton ("Exit");

Init ();

}

/** The method initializes the window.

private void init () {

Setting individual properties of a window

This.setdefaultcloseoperation (Jframe.exit_on_close);

This.settitle ("Login Window");

This.setsize (300, 220);

This.setresizable (FALSE);

/* Set the position and coordinates of each control

* The first two parameters of SetBounds () are the upper-left coordinates of the control, and the last two parameters are the width and height of the control

*/

Lbltitle.setbounds (100, 10, 100, 30);

Lblusername.setbounds (20, 60, 75, 25);

Lbluserpwd.setbounds (20, 100, 75, 25);

Txtusername.setbounds (100, 60, 120, 25);

Pwduserpwd.setbounds (100, 100, 120, 25);

Btnlogin.setbounds (50, 140, 75, 25);

Btnquit.setbounds (150, 140, 75, 25);

Press all controls on the container

Pnlmain.add (Lbltitle);

Pnlmain.add (Lblusername);

Pnlmain.add (LBLUSERPWD);

Pnlmain.add (txtUserName);

Pnlmain.add (PWDUSERPWD);

Pnlmain.add (Btnlogin);

Pnlmain.add (Btnquit);

Add a container to a window

This.add (Pnlmain);

This.setvisible (TRUE);

}

}



"Full stack Java notes" is a can help you from Zeto long for the full stack of Java Engineer Series of notes. The author is called Mr. G,10 years Java research and development experience, has been in the digital, Aerospace Institute of a Research and development center engaged in software design and research and development work, from small white gradually achieve engineers, senior engineers, architects. Proficient in Java Platform Software development, Master Java EE, familiar with various popular development framework.


The notes contain six parts from shallow into deep:

A-java Introductory Phase

B-Database from beginner to proficient

C-hand Edge mobile front end and web front end

D-j2ee from understanding to combat

E-java Advanced Frame Fine Solution

F-linux and Hadoop


This article is from the "full stack Java Notes" blog, be sure to keep this source http://javanew.blog.51cto.com/12931675/1956117

14.4-Full Stack Java notes: javax.swing What are the common controls? How to use it?

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.