在 Java 中制作 GUI 遇到困难

问题描述 投票:0回答:1

我对编程相当陌生,我已经开始开发或制作 GUI 来测试我的能力。我尝试仅使用哈希映射和数组来登录和注册来存储值。每次我单击注册或登录注册时,它都不会提示我参加课程,我是否做错了什么?这对我会有很大帮助。

非常感谢你

import java.util.*;
 
 
 
 
 
    public static void main(String[] args) {
 
        IDandPasswords idandPasswords = new IDandPasswords();
        LoginPage LoginPage = new LoginPage(idandPasswords.getLoginInfo(), new ArrayList<>());
    }
---------------------------------------------------------------------------------------------------------------------------------------
 
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
 
public class LoginPage implements ActionListener {
 
    JFrame frame = new JFrame();
    JButton loginButton = new JButton("Login");
    JButton resetButton = new JButton("Reset");
    JButton registerButton = new JButton("Register");
    JTextField userIDField = new JTextField();
    JPasswordField userPasswordField = new JPasswordField();
    JLabel userIDLabel = new JLabel("userID:");
    JLabel userPasswordLabel = new JLabel("password:");
    JLabel messageLabel = new JLabel();
    JPanel loginPanel = new JPanel();
    JPanel registrationPanel = new JPanel();
    HashMap<String,String> logininfo = new HashMap<String,String>();
    ArrayList<String> registrationInfo = new ArrayList<String>();
 
    LoginPage(HashMap<String,String> loginInfoOriginal, ArrayList<String> registrationInfoOriginal){
 
        logininfo = loginInfoOriginal;
        registrationInfo = registrationInfoOriginal;
 
        userIDLabel.setBounds(50,100,75,25);
        userPasswordLabel.setBounds(50,150,75,25);
 
        messageLabel.setBounds(125,250,250,35);
        messageLabel.setFont(new Font(null,Font.ITALIC,25));
 
        userIDField.setBounds(125,100,200,25);
        userPasswordField.setBounds(125,150,200,25);
 
        loginButton.setBounds(125,200,100,25);
        loginButton.setFocusable(false);
        loginButton.addActionListener(this);
 
        resetButton.setBounds(225,200,100,25);
        resetButton.setFocusable(false);
        resetButton.addActionListener(this);
 
        registerButton.setBounds(125,300,200,25);
        registerButton.setFocusable(false);
        registerButton.addActionListener(this);
 
        loginPanel.add(userIDLabel);
        loginPanel.add(userPasswordLabel);
        loginPanel.add(messageLabel);
        loginPanel.add(userIDField);
        loginPanel.add(userPasswordField);
        loginPanel.add(loginButton);
        loginPanel.add(resetButton);
 
        registrationPanel.setLayout(null);
        registrationPanel.add(registerButton);
 
        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("Menu");
        JMenuItem loginMenuItem = new JMenuItem("Login");
        JMenuItem registerMenuItem = new JMenuItem("Register");
 
        loginMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                frame.remove(registrationPanel);
                frame.add(loginPanel);
                frame.repaint();
            }
        });
 
        registerMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                frame.remove(loginPanel);
                frame.add(registrationPanel);
                frame.repaint();
            }
        });
 
        menu.add(loginMenuItem);
        menu.add(registerMenuItem);
        menuBar.add(menu);
        frame.setJMenuBar(menuBar);
 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(420,420);
        frame.setLayout(null);
        frame.add(loginPanel);
        frame.setVisible(true);
    }
 
    @Override
    public void actionPerformed(ActionEvent e) {
 
        if (e.getSource() == resetButton) {
            userIDField.setText("");
            userPasswordField.setText("");
        }
 
        if (e.getSource() == loginButton) {
 
            String userID = userIDField.getText();
            String password = String.valueOf(userPasswordField.getPassword());
 
            if (logininfo.containsKey(userID)) {
                if (logininfo.get(userID).equals(password)) {
                    messageLabel.setForeground(Color.green);
                    messageLabel.setText("Login successful");
                    frame.dispose();
                    WelcomePage welcomePage = new WelcomePage(userID);
                } else {
                    messageLabel.setForeground(Color.red);
                    messageLabel.setText("Wrong password");
                }
 
            } else {
                messageLabel.setForeground(Color.red);
                messageLabel.setText("username not found");
            }
        }
 
        if (e.getSource() == registerButton) {
            String username = JOptionPane.showInputDialog("Enter Username:");
            String password = JOptionPane.showInputDialog("Enter Password:");
            logininfo.put(username, password);
            JOptionPane.showMessageDialog(null, "Registration successful!");
        }
    }
}
---------------------------------------------------------------------------------------------------------------------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
 
 
 
class WelcomePage {
 
    JFrame frame = new JFrame();
    JButton logoutButton = new JButton("Logout");
    JLabel welcomeLabel = new JLabel("Hello!");
 
    WelcomePage(String userID){
 
        welcomeLabel.setBounds(0,0,200,35);
        welcomeLabel.setFont(new Font(null,Font.PLAIN,25));
        welcomeLabel.setText("Hello "+userID);
 
        logoutButton.setBounds(300, 10, 100, 25);
        logoutButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
                LoginPage loginPage = new LoginPage(new HashMap<>(), new ArrayList<>());
            }
        });
 
        frame.add(welcomeLabel);
        frame.add(logoutButton);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(420, 420);
        frame.setLayout(null);
        frame.setVisible(true);
    }
}
---------------------------------------------------------------------------------------------------------------------------------------
import java.util.HashMap;
 
public class IDandPasswords {
 
    private HashMap<String,String> logininfo = new HashMap<String,String>();
 
    public IDandPasswords(){
        logininfo.put("Bro","pizza");
        logininfo.put("Brometheus","PASSWORD");
        logininfo.put("BroCode","abc123");
    }
 
    public HashMap<String,String> getLoginInfo(){
        return logininfo;
    }
}

我尝试提示按钮进入注册,并登录页面/课程,但它不会将我带到该页面。

java
1个回答
0
投票

由于以下行(在类

LoginPage
的构造函数中),您还需要为
loginPanel
registrationPanel
设置边界。

frame.setLayout(null);

您还忘记将

loginPanel
的布局管理器设置为 null。这是经过更正的代码。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
 
public class LoginPage implements ActionListener {
 
    JFrame frame = new JFrame();
    JButton loginButton = new JButton("Login");
    JButton resetButton = new JButton("Reset");
    JButton registerButton = new JButton("Register");
    JTextField userIDField = new JTextField();
    JPasswordField userPasswordField = new JPasswordField();
    JLabel userIDLabel = new JLabel("userID:");
    JLabel userPasswordLabel = new JLabel("password:");
    JLabel messageLabel = new JLabel();
    JPanel loginPanel = new JPanel();
    JPanel registrationPanel = new JPanel();
    HashMap<String,String> logininfo = new HashMap<String,String>();
    ArrayList<String> registrationInfo = new ArrayList<String>();
 
    LoginPage(HashMap<String,String> loginInfoOriginal, ArrayList<String> registrationInfoOriginal){
 
        logininfo = loginInfoOriginal;
        registrationInfo = registrationInfoOriginal;
 
        userIDLabel.setBounds(50,100,75,25);
        userPasswordLabel.setBounds(50,150,75,25);
 
        messageLabel.setBounds(125,250,250,35);
        messageLabel.setFont(new Font(null,Font.ITALIC,25));
 
        userIDField.setBounds(125,100,200,25);
        userPasswordField.setBounds(125,150,200,25);
 
        loginButton.setBounds(125,200,100,25);
        loginButton.setFocusable(false);
        loginButton.addActionListener(this);
 
        resetButton.setBounds(225,200,100,25);
        resetButton.setFocusable(false);
        resetButton.addActionListener(this);
 
        registerButton.setBounds(125,300,200,25);
        registerButton.setFocusable(false);
        registerButton.addActionListener(this);
 
        loginPanel.add(userIDLabel);
        loginPanel.add(userPasswordLabel);
        loginPanel.add(messageLabel);
        loginPanel.add(userIDField);
        loginPanel.add(userPasswordField);
        loginPanel.add(loginButton);
        loginPanel.add(resetButton);
        loginPanel.setBounds(0, 0, 400, 400);
        loginPanel.setLayout(null);
 
        registrationPanel.setLayout(null);
        registrationPanel.add(registerButton);
        registrationPanel.setBounds(0, 0, 400, 400);
 
        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("Menu");
        JMenuItem loginMenuItem = new JMenuItem("Login");
        JMenuItem registerMenuItem = new JMenuItem("Register");
 
        loginMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("loginMenuItem");
                frame.remove(registrationPanel);
                frame.add(loginPanel);
                frame.repaint();
            }
        });
 
        registerMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("registerMenuItem");
                frame.remove(loginPanel);
                frame.add(registrationPanel);
                frame.repaint();
            }
        });
 
        menu.add(loginMenuItem);
        menu.add(registerMenuItem);
        menuBar.add(menu);
        frame.setJMenuBar(menuBar);
 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(420,420);
        frame.setLayout(null);
        frame.add(loginPanel);
        frame.setVisible(true);
    }
 
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("actionPerformed");
        if (e.getSource() == resetButton) {
            userIDField.setText("");
            userPasswordField.setText("");
        }
 
        if (e.getSource() == loginButton) {
 
            String userID = userIDField.getText();
            String password = String.valueOf(userPasswordField.getPassword());
 
            if (logininfo.containsKey(userID)) {
                if (logininfo.get(userID).equals(password)) {
                    messageLabel.setForeground(Color.green);
                    messageLabel.setText("Login successful");
                    frame.dispose();
                    WelcomePage welcomePage = new WelcomePage(userID);
                } else {
                    messageLabel.setForeground(Color.red);
                    messageLabel.setText("Wrong password");
                }
 
            } else {
                messageLabel.setForeground(Color.red);
                messageLabel.setText("username not found");
            }
        }
 
        if (e.getSource() == registerButton) {
            String username = JOptionPane.showInputDialog("Enter Username:");
            String password = JOptionPane.showInputDialog("Enter Password:");
            logininfo.put(username, password);
            JOptionPane.showMessageDialog(null, "Registration successful!");
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.