21xrx.com
2024-11-05 12:17:29 Tuesday
登录
文章检索 我的文章 写文章
Java实现登录界面身份选择的代码
2023-06-15 18:37:31 深夜i     --     --
Java Swing

在Java程序设计中,实现登录界面身份选择的功能是一项基本的任务。这样的功能往往是在一个系统的登录页面实现的,用户可以选择不同的身份来登录系统。这篇文章将介绍如何使用Java语言来实现这样的功能。首先,我们需要一个登录页面,可以用Java Swing库来实现,代码如下:


import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class LoginFrame extends JFrame implements ActionListener {

  JLabel user_label, password_label, message;

  JTextField userName_text;

  JPasswordField password_text;

  JButton submit, cancel;

  LoginFrame() {

    user_label = new JLabel();

    user_label.setText("用户名 :");

    userName_text = new JTextField();

    password_label = new JLabel();

    password_label.setText("密码 :");

    password_text = new JPasswordField();

    submit = new JButton("登录");

    cancel = new JButton("取消");

    submit.addActionListener(this);

    cancel.addActionListener(this);

    JPanel panel = new JPanel(new GridLayout(3, 1));

    panel.add(user_label);

    panel.add(userName_text);

    panel.add(password_label);

    panel.add(password_text);

    message = new JLabel();

    panel.add(message);

    panel.add(submit);

    panel.add(cancel);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    add(panel, BorderLayout.CENTER);

    setTitle("请登录");

    setSize(300, 150);

    setVisible(true);

  }

  public static void main(String[] args) {

    new LoginFrame();

  }

  public void actionPerformed(ActionEvent ae) {

    String userName = userName_text.getText();

    String password = password_text.getText();

    if (ae.getSource() == submit) {

      if (userName.trim().isEmpty() || password.trim().isEmpty()) {

        message.setText("用户名和密码不能为空");

        JOptionPane.showMessageDialog(null, "请输入用户名和密码", "警告", JOptionPane.WARNING_MESSAGE);

      } else {

        JFrame frame = new JFrame("选择身份登录");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setSize(350, 200);

        frame.setLocationRelativeTo(null);

        JPanel panel = new JPanel();

        panel.setLayout(new GridLayout(3, 1));

        JLabel message = new JLabel("请选择登录角色");

        message.setHorizontalAlignment(JLabel.CENTER);

        panel.add(message);

        JButton studentButton = new JButton("学生");

        studentButton.addActionListener(new ActionListener() {

          public void actionPerformed(ActionEvent e) {

            JOptionPane.showMessageDialog(null, "您选择了学生身份", "消息", JOptionPane.INFORMATION_MESSAGE);

            frame.dispose();

          }

        });

        JButton teacherButton = new JButton("教师");

        teacherButton.addActionListener(new ActionListener() {

          public void actionPerformed(ActionEvent e) {

            JOptionPane.showMessageDialog(null, "您选择了教师身份", "消息", JOptionPane.INFORMATION_MESSAGE);

            frame.dispose();

          }

        });

        JButton adminButton = new JButton("管理员");

        adminButton.addActionListener(new ActionListener() {

          public void actionPerformed(ActionEvent e) {

            JOptionPane.showMessageDialog(null, "您选择了管理员身份", "消息", JOptionPane.INFORMATION_MESSAGE);

            frame.dispose();

          }

        });

        panel.add(studentButton);

        panel.add(teacherButton);

        panel.add(adminButton);

        frame.add(panel);

        frame.setVisible(true);

      }

    } else {

      userName_text.setText("");

      password_text.setText("");

    }

  }

}

上面的代码实现了一个简单的登录页面,并在用户点击“登录”按钮后弹出一个选择身份的窗口。当用户选择身份后,显示消息框告知用户选择的身份。

实现该功能需要的 、JButton、ActionListener

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复