繁体   English   中英

如何将一个ActionPerformed从一个ActionListener发送到另一个ActionListener?

[英]How to send an ActionPerformed from an ActionListener to another ActionListener?

我有一个Frame(在这里命名为“ MainApplication”),它主要具有一个JPanel来显示信息,具体取决于上下文。

启动时,MainApplication的空JPanel。

然后,它创建一个“ LoginRequest”类,该类创建一个简单的登录名/密码表单,然后将其发送回MainApplication,该应用程序将其显示在其JPanel中。

“ LoginRequest”类实现了ActionListener,因此,当用户单击“ Login”按钮时,它会检查登录名/密码是否正确,如果授予了用户权限,我要卸载该表格并显示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();
    }
}

当ActionEvent从“ LoginRequest”类发送到“ MainApplication”类时,它执行代码,但是最后,什么都没有发生,就好像未重新绘制JFrame一样。

有任何想法吗 ?

谢谢,

最好的方法是使用JDialog (主框架JFrame是父组件)登录表单,并使用CardLayout在面板之间切换(因此无需删除,重新绘制和重新验证):

您的主要形式应如下所示:

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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM