创建一个简单的Java Swing登入窗口

连接数据图就不弄了,要连接数据库的自行添加

为了方便排版,Layout用SpringLayout类型

1.账号框采用JTextField 类型

2.密码框采用JPasswordField类型

两者的区别是:

JPasswordField显示的文本是这样的:

 创建一个简单的Java Swing登入窗口

 

 

文本框JTextField显示的文本是这样的:

创建一个简单的Java Swing登入窗口

3.建立两个JButton,一个是“登入”按钮,另一个是“重置”按钮

4.建立两个Checkbox:一个是记住密码,另一个是隐身登入

5.最后一步加入背景图

 

代码:

package l;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.SpringLayout;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import javax.swing.JLayeredPane;
import java.awt.Checkbox;

public class Login extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JPasswordField passwordField;

    
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Login frame = new Login();
                    frame.setVisible(true);                            //窗口可见
                    frame.setResizable(false);                     //设置登入窗口不能调整大小
                    ImageIcon img = new ImageIcon("d:/img.jpg");//这是背景图片
                     JLabel imgLabel = new JLabel(img);//将背景图放在标签里。
                     frame.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));          
                     imgLabel.setBounds(0,0,img.getIconWidth(), img.getIconHeight());
                        Container cp=frame.getContentPane();
                        ((JPanel)cp).setOpaque(false);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    
    public Login() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        SpringLayout sl_contentPane = new SpringLayout();
        contentPane.setLayout(sl_contentPane);
        
        JLabel lblNewLabel = new JLabel("账号:");
        lblNewLabel.setFont(new Font("黑体", Font.PLAIN, 19));
        sl_contentPane.putConstraint(SpringLayout.NORTH, lblNewLabel, 34, SpringLayout.NORTH, contentPane);
        sl_contentPane.putConstraint(SpringLayout.WEST, lblNewLabel, 41, SpringLayout.WEST, contentPane);
        contentPane.add(lblNewLabel);
        
        JLabel lblNewLabel_1 = new JLabel("密码:");
        lblNewLabel_1.setFont(new Font("黑体", Font.PLAIN, 19));
        sl_contentPane.putConstraint(SpringLayout.NORTH, lblNewLabel_1, 29, SpringLayout.SOUTH, lblNewLabel);
        sl_contentPane.putConstraint(SpringLayout.WEST, lblNewLabel_1, 0, SpringLayout.WEST, lblNewLabel);
        contentPane.add(lblNewLabel_1);
        
        textField = new JTextField();
        textField.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                String temp=textField.getText();
                if(temp.equals("")) {
                    textField.setForeground(Color.GRAY);                             //JTextField提示输入QQ号
                    textField.setText("请输入QQ号");
                }
            }
            
            public void focusGained(FocusEvent e) {
                String temp=textField.getText();
                if(temp.equals("请输入QQ号")) {
                    textField.setText("");
                }
            }
        });
        sl_contentPane.putConstraint(SpringLayout.NORTH, textField, 1, SpringLayout.NORTH, lblNewLabel);
        sl_contentPane.putConstraint(SpringLayout.WEST, textField, 52, SpringLayout.EAST, lblNewLabel);
        sl_contentPane.putConstraint(SpringLayout.EAST, textField, 196, SpringLayout.EAST, lblNewLabel);
        contentPane.add(textField);
        textField.setColumns(10);
        
        passwordField = new JPasswordField();
        sl_contentPane.putConstraint(SpringLayout.NORTH, passwordField, 26, SpringLayout.SOUTH, textField);
        sl_contentPane.putConstraint(SpringLayout.WEST, passwordField, 52, SpringLayout.EAST, lblNewLabel_1);
        sl_contentPane.putConstraint(SpringLayout.SOUTH, passwordField, 48, SpringLayout.SOUTH, textField);
        sl_contentPane.putConstraint(SpringLayout.EAST, passwordField, 201, SpringLayout.EAST, lblNewLabel_1);
        contentPane.add(passwordField);
        
        JButton btnNewButton = new JButton("登入");
        sl_contentPane.putConstraint(SpringLayout.SOUTH, btnNewButton, 65, SpringLayout.SOUTH, passwordField);
        contentPane.add(btnNewButton);
        
        JButton button = new JButton("重置");
        sl_contentPane.putConstraint(SpringLayout.NORTH, btnNewButton, 2, SpringLayout.NORTH, button);
        sl_contentPane.putConstraint(SpringLayout.EAST, btnNewButton, -70, SpringLayout.WEST, button);
        sl_contentPane.putConstraint(SpringLayout.NORTH, button, 32, SpringLayout.SOUTH, passwordField);
        sl_contentPane.putConstraint(SpringLayout.WEST, button, 242, SpringLayout.WEST, contentPane);
        button.setFont(new Font("宋体", Font.PLAIN, 19));
        contentPane.add(button);
        
        Checkbox checkbox = new Checkbox("记住密码");
        sl_contentPane.putConstraint(SpringLayout.WEST, checkbox, 0, SpringLayout.WEST, textField);
        sl_contentPane.putConstraint(SpringLayout.SOUTH, checkbox, -6, SpringLayout.NORTH, btnNewButton);
        contentPane.add(checkbox);
        
        Checkbox checkbox_1 = new Checkbox("隐身登入");
        sl_contentPane.putConstraint(SpringLayout.WEST, checkbox_1, 35, SpringLayout.EAST, checkbox);
        sl_contentPane.putConstraint(SpringLayout.SOUTH, checkbox_1, 0, SpringLayout.SOUTH, checkbox);
        contentPane.add(checkbox_1);
    }
}


运行截图:

创建一个简单的Java Swing登入窗口