Anonymous
Застрял, пытаясь заставить мой графический интерфейс входа работать на Java
Сообщение
Anonymous » 01 май 2024, 17:26
Я новичок в программировании и начал разрабатывать или создавать графический интерфейс, чтобы проверить свои способности. Я попытался войти в систему и зарегистрироваться, используя только хеш-карту и массивы только для хранения значений. Каждый раз, когда я нажимаю кнопку «Зарегистрироваться» или войти в систему, мне не предлагается войти в класс, есть ли что-то, что я сделал неправильно? Это мне очень поможет.
Огромное спасибо
Код: Выделить всё
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 logininfo = new HashMap();
ArrayList registrationInfo = new ArrayList();
LoginPage(HashMap loginInfoOriginal, ArrayList 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 logininfo = new HashMap();
public IDandPasswords(){
logininfo.put("Bro","pizza");
logininfo.put("Brometheus","PASSWORD");
logininfo.put("BroCode","abc123");
}
public HashMap getLoginInfo(){
return logininfo;
}
}
Я попробовал нажать кнопку при регистрации и войти на страницу/класс, но это не привело меня на страницу.
Подробнее здесь:
https://stackoverflow.com/questions/784 ... rk-in-java
1714573588
Anonymous
Я новичок в программировании и начал разрабатывать или создавать графический интерфейс, чтобы проверить свои способности. Я попытался войти в систему и зарегистрироваться, используя только хеш-карту и массивы только для хранения значений. Каждый раз, когда я нажимаю кнопку «Зарегистрироваться» или войти в систему, мне не предлагается войти в класс, есть ли что-то, что я сделал неправильно? Это мне очень поможет. Огромное спасибо [code]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 logininfo = new HashMap(); ArrayList registrationInfo = new ArrayList(); LoginPage(HashMap loginInfoOriginal, ArrayList 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 logininfo = new HashMap(); public IDandPasswords(){ logininfo.put("Bro","pizza"); logininfo.put("Brometheus","PASSWORD"); logininfo.put("BroCode","abc123"); } public HashMap getLoginInfo(){ return logininfo; } } [/code] Я попробовал нажать кнопку при регистрации и войти на страницу/класс, но это не привело меня на страницу. Подробнее здесь: [url]https://stackoverflow.com/questions/78413277/stuck-on-making-my-login-gui-work-in-java[/url]