gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:50:29 27 4
gpt4 key购买 nike

我有一个框架(此处名为“MainApplication”),它主要有一个 JPanel 来根据上下文显示信息。

启动时,MainApplication 有一个空的 JPanel。

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

“LoginRequest”类实现了 ActionListener,因此当用户单击“登录”按钮时,它会检查登录名/密码是否正确,并且,如果用户被授予,我想卸载该表单,并在 MainApplication Frame 上显示主屏幕。

所以,为了做到这一点,我想出了这个:

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

编辑:

你的方式:

主框架类:

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

关于java - 如何将 ActionPerformed 从一个 ActionListener 发送到另一个 ActionListener?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14916936/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com