我正在爲一門課程寫一個假期推薦系統。其中的GUI使用CardLayout。在主類中,用默認名稱和訪問級別在其構造函數中定義用戶對象。此對象從主傳遞到UserCard面板,該面板將其傳遞到Login和登錄。 如果用戶成功登錄,則CardPanel從Login登錄到登錄,並且應該通過調用登錄用戶的用戶名來顯示登錄用戶的用戶名user.getUsername();方法。在調用CardLayout的show方法後強制JPanel重繪
我的問題是這樣的。由於卡片佈局的工作方式,具有用戶名顯示的面板已經在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);
}
}
道歉,如果我沒有過於明確的,我不給尋求幫助:)
爲更好地幫助更快張貼[SSCCE(http://sscce.org/),短,可運行,編譯, – mKorbel 2013-03-18 12:58:44