我正在为一篇课程写一个假期推荐系统。其GUI使用CardLayout。在主类中,创建一个用户对象,其默认名称和访问级别在其构造函数中定义。此对象从main传递到UserCard面板,该面板将其传递给Login并登录。 如果用户成功登录,则cardpanel从Login转换为登录,并且应该通过调用user.getUsername()来显示登录用户的用户名;方法
因此我的问题。由于卡布局的工作方式,具有用户名显示的面板已经在UserCards的构造函数中创建,其默认值从那时起首先创建了用户对象。我需要找到一种方法来强制此面板在cardlayout对象上调用show方法后重新绘制。以下是有问题的3个类的代码。 (我已将代码粘贴限制在相关方法中。)
//the usercards panel
public UserCards(User u)
{
CardLayout cl = new CardLayout();
this.setLayout(cl);
UserOptionsPanel options_card = new UserOptionsPanel(cl, this);
RegisterPanel register_card = new RegisterPanel(cl, this);
LoggedInPanel loggedin_card = new LoggedInPanel(cl, this, u);
LoginPanel login_card = new LoginPanel(cl, this, u, loggedin_card);
this.add(options_card, options);
this.add(login_card, login);
this.add(register_card, register);
this.add(loggedin_card, loggedin);
}
//the Loggin action listener user is passed in as a reference to the user object created in //main. the createUser(); method is a badly named method that simply calls setter methods on //the user object's fields
@Override
public void actionPerformed(ActionEvent e)
{
String[] vals = packData();
try
{
DBConnection d = new DBConnection();
Connection conn = d.getConnection();
Validation v = new Validation(vals, user);
v.getActual(conn);
if(v.validate())
{
user = v.createUser();
System.out.println(user.getUserName());
l.revalidate();
cl.show(pane, "loggedin");
}
else
{
lbl_statusmsg.setText("Password Incorrect");
lbl_statusmsg.repaint();
}
}
catch(ClassNotFoundException | SQLException ex)
{
ex.printStackTrace();
}
}
//the loggedin constructor
public class LoggedInPanel extends JPanel
{
private User user;
private JLabel lbl_details;
public LoggedInPanel(CardLayout cl, Container pane, User u)
{
super();
user = u;
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
lbl_details = new JLabel();
lbl_details.setText("Welcome "+user.getUserName());
this.add(lbl_details);
}
}
道歉,如果我没有过分清楚我不会被要求帮助:)
答案 0 :(得分:0)
你的意思是什么?
CardLayout cl = new CardLayout() {
@Override
public void show(java.awt.Container parent, String name) {
super.show(parent, name);
// your code here
}
};