How to achieve redirection in a Java login interface?

To achieve the redirection of the Java login interface, you can use Swing’s JFrame and JPanel. Below is a simple example code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginFrame extends JFrame {

    private JPanel loginPanel;
    private JPanel homePanel;

    public LoginFrame() {
        // 设置窗口标题
        super("登录界面");

        // 创建登录面板
        loginPanel = new JPanel();
        loginPanel.setLayout(new FlowLayout());

        // 创建用户名和密码输入框
        JTextField usernameField = new JTextField(20);
        JPasswordField passwordField = new JPasswordField(20);
        JButton loginButton = new JButton("登录");

        // 添加登录按钮的点击事件监听器
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 模拟登录成功
                boolean loginSuccess = true;

                if (loginSuccess) {
                    // 创建首页面板
                    homePanel = new JPanel();
                    homePanel.setLayout(new FlowLayout());

                    // 创建欢迎标签
                    JLabel welcomeLabel = new JLabel("欢迎登录!");
                    homePanel.add(welcomeLabel);

                    // 移除登录面板,添加首页面板
                    remove(loginPanel);
                    add(homePanel);
                    revalidate();  // 重新布局
                    repaint();  // 重绘界面
                } else {
                    JOptionPane.showMessageDialog(null, "登录失败!", "提示", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        // 将用户名、密码输入框和登录按钮添加到登录面板
        loginPanel.add(new JLabel("用户名:"));
        loginPanel.add(usernameField);
        loginPanel.add(new JLabel("密码:"));
        loginPanel.add(passwordField);
        loginPanel.add(loginButton);

        // 将登录面板添加到窗口
        add(loginPanel);

        // 设置窗口大小和关闭操作
        setSize(300, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        new LoginFrame();
    }
}

In this example code, the LoginFrame class extends JFrame, representing the entire login interface window. In the constructor method, a login panel loginPanel is first created, with username, password input fields, and a login button, and an event listener is added to the login button. When the login button is clicked, if the login is successful, the home panel homePanel is created, the login panel is removed using the remove() method, the home panel is then added to the window using the add() method, and finally, the interface is reconfigured and redrawn using the revalidate() and repaint() methods.

A LoginFrame object is created in the main() method to launch the login interface window.

To navigate to other pages, you can create the corresponding panels as needed and switch between them using the remove() and add() methods.

bannerAds