我遇到了一个问题。我在java中用3个按钮创建了一个注册表单。我有setEnabled(False)的LogIn,SignUp和Place命令。我希望在包含id和psw的loginframe之后用户为系统提供正确的id和psw,启用PlaceOrder按钮。如何管理它;
主要框架类
public class MainFrame extends JFrame{
private JButton loginButton;
private JButton signUpButton;
JButton placeOrderButton;
private boolean q;
private UserManager userManager;
public MainFrame(){
super("Main Panel");
userManager = new UserManager();
loginButton = new JButton("Login");
loginButton.addActionListener(new LoginButtonListener());
placeOrderButton = new JButton("Place Order");
placeOrderButton.addActionListener(new PlaceOrderButtonListener());
placeOrderButton.setEnabled(false);
signUpButton = new JButton("Sign Up");
signUpButton.addActionListener(new SignUpButtonListener());
this.getContentPane().add(loginButton, BorderLayout.LINE_START);
this.getContentPane().add(signUpButton, BorderLayout.CENTER);
this.getContentPane().add(placeOrderButton, BorderLayout.LINE_END);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
class LoginButtonListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
new LoginFrame(userManager);
}
}
class SignUpButtonListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
new SignUpFrame(userManager);
}
}
class PlaceOrderButtonListener implements ActionListener {
public void actionPerformed(ActionEvent argO){
}
}
LoginFrame类
public class LoginFrame extends JFrame{
private JButton loginButton;
private JTextField usernameField;
private JPasswordField password;
private UserManager userManager;
public LoginFrame(UserManager userManager){
super("Enter your credentials to login");
this.userManager = userManager;
loginButton = new JButton("Login");
loginButton.addActionListener(new LoginButtonListener());
usernameField = new JTextField(15);
password = new JPasswordField(15);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(3,2));
mainPanel.add(new JLabel("Username:"));
mainPanel.add(usernameField);
mainPanel.add(new JLabel("Password:"));
mainPanel.add(password);
mainPanel.add(loginButton);
this.setContentPane(mainPanel);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
class LoginButtonListener implements ActionListener{
public void actionPerformed(ActionEvent arg0) {
String givenID = usernameField.getText();
if(userManager.isUserInList(givenID)){
String givenPass = password.getText();
User theUser = userManager.getUserWithID(givenID);
if(givenPass.equals(theUser.getPassword())){
dispose();
JOptionPane.showMessageDialog(null, "Access granted!");
}
else{
JOptionPane.showMessageDialog(null,
"Wrong Password",
"Error!",
JOptionPane.ERROR_MESSAGE);
}
}
else{
JOptionPane.showMessageDialog(null,
"Wrong username",
"Error!",
JOptionPane.ERROR_MESSAGE);
}
}
}
类UserManager
import java.util.ArrayList;
public class UserManager {
private ArrayList<User> userList;
public UserManager(){
userList = new ArrayList<User>();
User u1 = new User("Teo Haik", "teo@uom.gr","teohaik","teopass");
userList.add(u1);
}
public boolean isUserInList(String ID){
for(User u: userList){
if(u.getUserID().equals(ID)){
return true;
}
}
return false;
}
public User getUserWithID(String ID){
for(User u: userList){
if(u.getUserID().equals(ID)){
return u;
}
}
return null;
}
public void addUser(User p){
if(!userList.contains(p)){
userList.add(p);
}
}
}
答案 0 :(得分:0)
我无法理解你的代码,这里是完整的工作清洁代码:希望它有所帮助
用作指南
package stackoverflow;
import java.awt.EventQueue;
public class Login {
private JFrame frame;
private JTextField txtusername;
private JPasswordField txtPassword;
private JButton btnPlaceorder;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Login window = new Login();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Login() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 341, 240);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblUsername = new JLabel("Username:");
lblUsername.setBounds(21, 56, 52, 14);
frame.getContentPane().add(lblUsername);
JLabel lblPassword = new JLabel("Password");
lblPassword.setBounds(21, 94, 52, 14);
frame.getContentPane().add(lblPassword);
txtusername = new JTextField();
txtusername.setBounds(83, 53, 86, 20);
frame.getContentPane().add(txtusername);
txtusername.setColumns(10);
txtPassword = new JPasswordField();
txtPassword.setColumns(10);
txtPassword.setBounds(83, 91, 86, 20);
frame.getContentPane().add(txtPassword);
JButton btnNewButton = new JButton("Login");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (txtusername.getText().equals("admin") && new String(txtPassword.getPassword()).equals("admin")) {
btnPlaceorder.setEnabled(true);
} else {
btnPlaceorder.setEnabled(false);
}
}
});
btnNewButton.setBounds(10, 138, 89, 23);
frame.getContentPane().add(btnNewButton);
btnPlaceorder = new JButton("PlaceOrder");
btnPlaceorder.setEnabled(false);
btnPlaceorder.setBounds(105, 138, 89, 23);
frame.getContentPane().add(btnPlaceorder);
}
}
答案 1 :(得分:0)
现在这是完全正常的代码:
我已经修复但你的代码写得不好。我已经更新了一些像密码检查这样的东西来测试。
主要框架类
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class MainFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JButton loginButton;
private JButton signUpButton;
private JButton placeOrderButton;
private boolean q;
private UserManager userManager;
private LoginFrame loginFrame;
public MainFrame() {
super("Main Panel");
userManager = new UserManager();
loginButton = new JButton("Login");
loginButton.addActionListener(new LoginButtonListener());
placeOrderButton = new JButton("Place Order");
placeOrderButton.addActionListener(new PlaceOrderButtonListener());
placeOrderButton.setEnabled(false);
signUpButton = new JButton("Sign Up");
signUpButton.addActionListener(new SignUpButtonListener());
this.getContentPane().add(loginButton, BorderLayout.LINE_START);
this.getContentPane().add(signUpButton, BorderLayout.CENTER);
this.getContentPane().add(placeOrderButton, BorderLayout.LINE_END);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loginFrame = new LoginFrame(this);
this.setVisible(true);
}
public void enableOrder() {
this.placeOrderButton.setEnabled(true);
}
class LoginButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
loginFrame.init(userManager);
}
}
class SignUpButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
// new SignUpFrame(userManager);
}
}
class PlaceOrderButtonListener implements ActionListener {
public void actionPerformed(ActionEvent argO) {
}
}
}
登录框
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class LoginFrame extends JFrame {
private JButton loginButton;
private JTextField usernameField;
private JPasswordField password;
private UserManager userManager;
private MainFrame mainFrame;
public LoginFrame(MainFrame mainFrame) {
super("Enter your credentials to login");
this.mainFrame = mainFrame;
}
public void init(UserManager userManager) {
loginButton = new JButton("Login");
loginButton.addActionListener(new LoginButtonListener());
usernameField = new JTextField(15);
password = new JPasswordField(15);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(3, 2));
mainPanel.add(new JLabel("Username:"));
mainPanel.add(usernameField);
mainPanel.add(new JLabel("Password:"));
mainPanel.add(password);
mainPanel.add(loginButton);
this.userManager = userManager;
this.setContentPane(mainPanel);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
class LoginButtonListener implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
String givenID = usernameField.getText();
if (userManager.isUserInList(givenID)) {
String givenPass = new String(password.getPassword());
User theUser = userManager.getUserWithID(givenID);
if (givenPass.equals("password")) {
dispose();
JOptionPane.showMessageDialog(null, "Access granted!");
mainFrame.enableOrder();
}
else {
JOptionPane.showMessageDialog(null, "Wrong Password", "Error!", JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(null, "Wrong username", "Error!", JOptionPane.ERROR_MESSAGE);
}
}
}
}