0

コンテキストに応じて情報を表示するための 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 が再描画されなかったかのように、最後には何も起こりません。

何か案は ?

ありがとう、

4

1 に答える 1

1

最善の方法は、ログインフォームに使用しJDialog(メインフレームは親コンポーネントになります)、パネルを切り替えることです(したがって、削除、再描画、および再検証の必要はありません)。JFrameCardLayout

メインフォームは次のようになります。

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MainFrame{
    JFrame frame = new JFrame("Main frame");

    JPanel welcomePanel = new JPanel();
    JPanel workspacePanel = new JPanel();
    JPanel cardPanel = new JPanel();

    JButton btnLogin = new JButton("Login");
    JLabel lblWelcome = new JLabel("Welcome to workspace");

    CardLayout cl = new CardLayout();

    LoginRequest lr = new LoginRequest(this);

    public MainFrame() {
        welcomePanel.add(btnLogin);
        btnLogin.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                lr.setVisible(true);
            }
        });

        workspacePanel.add(lblWelcome);

        cardPanel.setLayout(cl);
        cardPanel.add(welcomePanel, "1");
        cardPanel.add(workspacePanel, "2");
        cl.show(cardPanel,"1");

        frame.getContentPane().add(cardPanel);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setPreferredSize(new Dimension(320,240));
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String [] args){
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MainFrame();
            }
        });
    }
}

ログインフォームは次のようになります。

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginRequest extends JDialog{
    /**You can add, JTextFields, JLabel, JPasswordField..**/
    JPanel panel = new JPanel();
    JButton btnLogin = new JButton("Login");

    public LoginRequest(final MainFrame mf) {
        setTitle("Login");
        panel.add(btnLogin);
        btnLogin.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //Put some login logic here
                mf.cl.show(mf.cardPanel,"2");
                dispose();
            }
        });
        add(panel, BorderLayout.CENTER);
        setModalityType(ModalityType.APPLICATION_MODAL);
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        pack();
        setLocationByPlatform(true);
    }
}

編集:

あなたのやり方で:

MainFrameクラス:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MainFrame{
    JFrame frame = new JFrame("Main frame");

    JPanel welcomePanel = new JPanel();
    JPanel workspacePanel = new JPanel();
    JPanel cardPanel = new JPanel();

    JButton btnLogin = new JButton("Login");
    JLabel lblWelcome = new JLabel("Welcome");

    LoginRequest lr = new LoginRequest(this);

    public MainFrame() {
        welcomePanel.add(btnLogin);
        btnLogin.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                lr.setVisible(true);
            }
        });

        workspacePanel.add(lblWelcome);

        frame.getContentPane().add(welcomePanel);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setPreferredSize(new Dimension(320,240));
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String [] args){
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MainFrame();
            }
        });
    }
}

LoginRequestクラス:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginRequest extends JDialog{
    /**You can add, JTextFields, JLabel, JPasswordField..**/
    JPanel panel = new JPanel();
    JButton btnLogin = new JButton("Login");

    public LoginRequest(final MainFrame mf) {
        setTitle("Login");
        panel.add(btnLogin);
        btnLogin.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //Put some login logic here
                mf.frame.getContentPane().removeAll();
                mf.frame.add(mf.workspacePanel);
                mf.frame.repaint();
                mf.frame.revalidate();
                dispose();
            }
        });
        add(panel, BorderLayout.CENTER);
        setModalityType(ModalityType.APPLICATION_MODAL);
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        pack();
        setLocationByPlatform(true);
    }
}
于 2013-02-17T13:15:59.850 に答える