Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将ActionPerformed从ActionListener发送到另一个ActionListener?_Java_Swing_Actionlistener - Fatal编程技术网

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

Java 如何将ActionPerformed从ActionListener发送到另一个ActionListener?,java,swing,actionlistener,Java,Swing,Actionlistener,我有一个框架(这里称为“MainApplication”),它主要有一个JPanel来显示信息,具体取决于上下文 启动时,Main应用程序有一个空的JPanel 然后,它创建一个“LoginRequest”类,该类创建一个简单的登录/密码表单,并将其发送回main应用程序,main应用程序将其显示在其JPanel中 “LoginRequest”类实现ActionListener,因此当用户单击“Login”按钮时,它会检查登录/密码是否正确,如果用户被授权,我希望卸载该表单,并在主应用程序框架上

我有一个框架(这里称为“MainApplication”),它主要有一个JPanel来显示信息,具体取决于上下文

启动时,Main应用程序有一个空的JPanel

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

“LoginRequest”类实现ActionListener,因此当用户单击“Login”按钮时,它会检查登录/密码是否正确,如果用户被授权,我希望卸载该表单,并在主应用程序框架上显示主屏幕

因此,为了做到这一点,我提出了以下建议:

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):

当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);
    }
}
您的登录表单应如下所示:

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);
    }
}
编辑:

你的方式:

大型机类:

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();
            }
        });
    }
}
登录请求类:

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

调用
LoginRequest
类中的
actionPerformed
方法中的
repaint
revalidate
方法。比如:owner_m.repaint();所有者重新验证();为什么不使用模式对话框来收集凭据?@brano88:我已经尝试过了,但它没有改变任何东西…:(@trashgood:是的,你是对的,我可以这么做,但我还是想知道为什么我写的代码不起作用:/谢谢你的完整回答。我会选择卡片布局,尽管有一些修改,因为我的面板“懒惰”(它们在“主应用程序”要求之前没有自行构建)。顺便说一句,我仍然不明白为什么我的第一个代码不起作用…删除/重新绘制/重新验证面板可能不是最好的方法,但它应该起作用…:/你正在尝试在MainApplication类中触发一个事件,但你正在
LoginRequest中的
actionPerformed
方法中传递
这个
类。尝试传递
owner\m
@adrien。痛苦:作为参考,这里有一个使用
validate()
的工作,但是
CardLayout
(+1)更好。@brano88:谢谢你的建议,但是我可以将“LoginRequest”对象传递给“main应用程序”,即使在执行的操作中也是如此。(我使用它来知道该操作的调用方是什么).它现在起作用了