コンテキストに応じて情報を表示するための JPanel を主に持つフレーム (ここでは「MainApplication」という名前) を取得しました。
起動時に、MainApplication には空の JPanel があります。
次に、単純なログイン/パスワード フォームを作成する「LoginRequest」クラスを作成し、それを MainApplication に送り返します。MainApplication はそれを JPanel に表示します。
「LoginRequest」クラスはActionListenerを実装しているので、ユーザーが「ログイン」ボタンをクリックすると、ログイン/パスワードが正しいかどうかをチェックし、ユーザーが許可されている場合は、そのフォームをアンロードして表示したいMainApplication フレームのメイン画面。
だから、それをするために、私はこれを思いついた:
public class LoginRequest implements ActionListener {
protected MainApplication owner_m = null;
public LoginRequest(MainApplication owner_p) {
owner_m = owner_p;
}
@Override
public void actionPerformed(ActionEvent event_p) {
// the user just clicked the "Login" button
if (event_p.getActionCommand().equals("RequestLogin")) {
// check if login/password are correct
if (getParameters().isUserGranted(login_l, password_l)) {
// send an ActionEvent to the "MainApplication", so as it will
// be notified to display the next screen
this.owner_m.actionPerformed(
new java.awt.event.ActionEvent(this, 0, "ShowSummary")
);
} else {
messageLabel_m.setForeground(Color.RED);
messageLabel_m.setText("Incorrect user or password");
}
}
}
}
次に、「MainApplication」クラス (JFrame を拡張) :
public class MainApplication extends JFrame implements ActionListener {
protected void load() {
// create the panel to display information
mainPanel_m = new JPanel();
// on startup, populate the panel with a login/password form
mainPanel_m.add(new LoginRequest(this).getLoginForm());
this.add(mainPanel_m);
}
@Override
public void actionPerformed(ActionEvent event_p) {
// show summary on request
if (event_p.getActionCommand().equals("ShowSummary")) {
// remove the previous information on the panel
// (which displayed the login form on this example)
mainPanel_m.removeAll();
// and populate the panel with other informations, from another class
mainPanel_m.add(...);
...
...
}
// and then refresh GUI
this.validate();
this.repaint();
this.pack();
}
}
「LoginRequest」クラスから「MainApplication」クラスに ActionEvent が送信されると、コードが実行されますが、JFrame が再描画されなかったかのように、最後には何も起こりません。
何か案は ?
ありがとう、