Common Basic Controls
javax.swing.JButton
In graphical interface programs, buttons are probably one of the most used controls. The JButton class in the javax.swing package is used to create buttons. As shown in Table 1, it is a common construction method for JButton.
javax.swing.JLabel
JLabel control is one of the simplest Swing components used to display labels on forms. JLabel can display both text and images. As shown in Table 3, it is a commonly used construction method for JLabel.
Be careful:
JLabel can only be used to display text and icon information, and users can not modify it.
javax.swing.JTextField
JTextField is also a lightweight control that allows users to edit single-line text. As shown in Table 5, it is a commonly used construction method for JTextField.
javax.swing.JPasswordField
JPasswordField is a lightweight component that allows editing of a single line of text whose view indicates what to type, but does not display the original characters. It is a single-line password box control, which is used in the same way as JTextField, so it will not be repeated.
Now we use the control mentioned above to implement a landing window, the effect is as follows:
[Example 1] Use controls to implement login windows
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;
//Textbox control for input user name
private JTextField txtUserName;
//Password Box Control for Entering Password
private JPasswordField pwdUserPwd;
//Login and Exit Button Control
private JButton btnLogin;
private JButton btnQuit;
public LoginFrame() {
//Instantiate containers and 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("Sign in");
btnQuit = new JButton("Sign out");
init();
}
/**This method initializes windows*/
private void init() {
//Set the properties of the window
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Login window");
this.setSize(300, 220);
this.setResizable(false);
/*Setting the position and coordinates of each control
* setBounds()The first two parameters 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 containers to windows
this.add(pnlMain);
this.setVisible(true);
}
}
Full stack Java Notes is a series of notes that can help you grow from zero to full stack Java engineers. The author is Mr. G, who has 10 years experience in Java research and development. He has been engaged in software design and development in a research and development center of Shenzhou Digital and Space Academy. He has gradually become an engineer, senior engineer and architect since childhood. Proficient in Java platform software development, JAVAEE, familiar with various popular development frameworks.
Notes contain six parts from shallow to deep:
A-Java Initial Stage
B-Database from Introduction to Proficiency
C-Blade Mobile Front End and Web Front End
D-J2EE from Understanding to Practice
Elaboration of E-Java Advanced Framework
F-Linux and Hadoop