我正在使用netbeans GUI Builder! 我已经创建了一个JFrame,并在其中放入了一个带有CardLayout的JPanel,以便在JPanel之间切换。 JFrame还附带一个菜单,附带不同的选项。现在我希望选项在用户登录之前显示为灰色。根据登录用户的提示,可以使用选项(参见图片)。
问题是我不知道如何在切换面板时获取用户登录的内容。登录面板知道在字段中输入的内容并知道登录是否正确。我尝试使用.getParent()函数来更改Jframe中的变量,但这似乎不起作用。但是我如何从JPanel更改JMenu项目呢? (儿童父母关系见这张图片)
答案 0 :(得分:0)
您好我已经开发了这个快速示例。 (请关注mantain的方式并分享登录信息)。
正如@Jan建议的那样,这是MVC模式的粗略实现。你试图分开: 模型:您正在使用的数据,以及用于服务和数据转换的组件层。 视图:从用户接收数据的组件 控制器:将视图与模型连接的方式 您可以在此处获取更多信息的链接:The MVC pattern and SWING
用于获取登录的类:
package com.stackoverflow.login;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
public class LoginDialog extends JDialog {
private static final long serialVersionUID = 1L;
// This is the class who will mantain the logged used information
private User user;
private JTextField tfUsername;
private JPasswordField pfPassword;
private JLabel lbUsername;
private JLabel lbPassword;
private JButton btnLogin;
private JButton btnCancel;
private boolean succeeded;
public LoginDialog(Frame parent) {
super(parent, "Login", true);
//
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints cs = new GridBagConstraints();
cs.fill = GridBagConstraints.HORIZONTAL;
lbUsername = new JLabel("Username: ");
cs.gridx = 0;
cs.gridy = 0;
cs.gridwidth = 1;
panel.add(lbUsername, cs);
tfUsername = new JTextField(20);
cs.gridx = 1;
cs.gridy = 0;
cs.gridwidth = 2;
panel.add(tfUsername, cs);
lbPassword = new JLabel("Password: ");
cs.gridx = 0;
cs.gridy = 1;
cs.gridwidth = 1;
panel.add(lbPassword, cs);
pfPassword = new JPasswordField(20);
cs.gridx = 1;
cs.gridy = 1;
cs.gridwidth = 2;
panel.add(pfPassword, cs);
panel.setBorder(new LineBorder(Color.GRAY));
btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (Login.authenticate(getUsername(), getPassword())) {
JOptionPane.showMessageDialog(LoginDialog.this,
"Hi " + getUsername() + "! You have successfully logged in.", "Login",
JOptionPane.INFORMATION_MESSAGE);
succeeded = true;
user = new User();
user.setPassword(getPassword());
user.setUsername(getUsername());
dispose();
} else {
JOptionPane.showMessageDialog(LoginDialog.this, "Invalid username or password", "Login",
JOptionPane.ERROR_MESSAGE);
// reset username and password
tfUsername.setText("");
pfPassword.setText("");
succeeded = false;
}
}
});
btnCancel = new JButton("Cancel");
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
JPanel bp = new JPanel();
bp.add(btnLogin);
bp.add(btnCancel);
getContentPane().add(panel, BorderLayout.CENTER);
getContentPane().add(bp, BorderLayout.PAGE_END);
pack();
setResizable(false);
setLocationRelativeTo(parent);
}
public String getUsername() {
return tfUsername.getText().trim();
}
public String getPassword() {
return new String(pfPassword.getPassword());
}
public boolean isSucceeded() {
return succeeded;
}
public User getUser() {
return user;
}
}
登录服务
package com.stackoverflow.login;
public class Login {
public static boolean authenticate(String username, String password) {
// hardcoded username and password
if (username.equals("bob") && password.equals("secret")) {
return true;
}
return false;
}
}
保留用户信息的类: package com.stackoverflow.login;
/**
* Class used to mantain the login information, it's just for educational
* purpouses, for prod environment you must implement it in a secure way!!
*/
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
运行程序的类
package com.stackoverflow.login;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
/**
* Run the program
*/
public class Main {
public static void main(String[] args) {
final JFrame frame = new JFrame("JDialog Demo");
final JButton btnLogin = new JButton("Click to login");
final JButton btnPanel = new JButton();
LoginDialog loginDlg = new LoginDialog(frame);
loginDlg.setVisible(true);
// if logon successfully
if (loginDlg.isSucceeded()) {
btnLogin.setVisible(false);
btnPanel.setText("Hi " + loginDlg.getUsername() + "! click to open new frame");
frame.getContentPane().add(btnPanel);
}
btnPanel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JLabel label = new JLabel("Logged user is: " + loginDlg.getUser().getUsername());
JDialog mydialog = new JDialog();
mydialog.setSize(new Dimension(400, 100));
mydialog.setTitle("Another frame");
mydialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
mydialog.add(label);
mydialog.setVisible(true);
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 100);
frame.setLayout(new FlowLayout());
frame.getContentPane().add(btnLogin);
frame.setVisible(true);
}
}