java项目实例之实现登录界面设计

时间:2018-09-18
本文章向大家介绍java项目实例之实现登录界面设计,需要的朋友可以参考一下

此系列将记录本人从开始到结束做物料管理系统的过程

登录界面的设计

此博客将实现如下界面:

当用户名或密码没输入时将显示相应的提示信息,采用java swing实现

代码:

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import java.awt.Color;

public class login {

    private JFrame frame;
    private JTextField textField;
    private JPasswordField passwordField;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    login window = new login();
                    window.frame.setLocationRelativeTo(null);
                    window.frame.setVisible(true);
                    
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public login() {
        initialize();
    }

    private void initialize() {
        frame = new JFrame();
        frame.setTitle("u7269u6599u7BA1u7406u7CFBu7EDF");
        frame.setBounds(100, 100, 360, 260);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        
        JLabel usernameLabel = new JLabel("u7528u6237u540DuFF1A");
        usernameLabel.setFont(new Font("宋体", Font.PLAIN, 14));
        usernameLabel.setBounds(90, 66, 72, 28);
        frame.getContentPane().add(usernameLabel);
        
        JLabel passwordLabel = new JLabel("u5BC6  u7801uFF1A");
        passwordLabel.setFont(new Font("宋体", Font.PLAIN, 14));
        passwordLabel.setBounds(90, 104, 72, 33);
        frame.getContentPane().add(passwordLabel);
        
        textField = new JTextField();
        textField.setForeground(Color.BLACK);
        textField.setBounds(147, 70, 110, 21);
        frame.getContentPane().add(textField);
        textField.setColumns(10);
        textField.addActionListener(new action());
        
        passwordField = new JPasswordField();
        passwordField.setBounds(147, 110, 110, 21);
        frame.getContentPane().add(passwordField);
        passwordField.addActionListener(new action());
        
        JButton loginButton = new JButton("u767Bu5F55");
        loginButton.setBounds(90, 157, 72, 23);
        frame.getContentPane().add(loginButton);
        loginButton.addActionListener(new action());
        
        ButtonGroup group = new ButtonGroup();
        
        JRadioButton button1 = new JRadioButton("u7BA1u7406u54581");
        button1.setBounds(64, 18, 72, 23);
        frame.getContentPane().add(button1);
        group.add(button1);
        
        JRadioButton button2 = new JRadioButton("u7BA1u7406u54582");
        button2.setBounds(138, 18, 72, 23);
        frame.getContentPane().add(button2);
        group.add(button2);
        
        JRadioButton button3 = new JRadioButton("u7BA1u7406u54582");
        button3.setBounds(210, 18, 80, 23);
        frame.getContentPane().add(button3);
        group.add(button3);
        
        JLabel label = new JLabel("");
        label.setBounds(267, 73, 54, 15);
        frame.getContentPane().add(label);
        
        JLabel label_1 = new JLabel("");
        label_1.setBounds(267, 113, 54, 15);
        frame.getContentPane().add(label_1);
    }
    
    class action implements ActionListener{

        @SuppressWarnings("deprecation")
        public void actionPerformed(ActionEvent e) {
            String buttonName = e.getActionCommand();
            if(buttonName.equals("登录")) {
                if(textField.getText().isEmpty()) {
                    JOptionPane.showMessageDialog(null, "账号不能为空");  
                }else if(passwordField.getText().isEmpty()) {
                    JOptionPane.showMessageDialog(null, "密码不能为空");
                }
            }
        }    
    }
}