gpt4 book ai didi

java - 为什么这个 propertyChange() 方法无法处理这个事件?

转载 作者:行者123 更新时间:2023-12-01 13:49:42 27 4
gpt4 key购买 nike

我是 Swing 开发的新手,在使用实现 PropertyChangeListener 接口(interface)的类时遇到以下问题。

所以我有以下GUI类(我只发布该类中有趣的部分):

public class GUI extends SingleFrameApplication implements PropertyChangeListener {

private MainFrame mainFrame = null;
private static LoginFrame loginFrame;

@Override
protected void startup() {
boolean offLine = false;
showLoginFrame();

mainFrame = new MainFrame(settings, tasksSettings, logAppender);

if (OSUtils.isUbuntuPrecisePangolin() || OSUtils.isFedoraBeefyMiracle() || OSUtils.isFedoraSphericalCow()) {
File mountPointFolder = new File(System.getenv("HOME") + "/connect_drives");
if (!mountPointFolder.exists())
mountPointFolder.mkdir();

mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
mainFrame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
if (mainFrame.getState() == JFrame.ICONIFIED)
tryToExit();
else
mainFrame.setState(JFrame.ICONIFIED);
}
});
}
}

private void showLoginFrame() {
loginFrame = new LoginFrame();
loginFrame.setVisible(true);
loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Notify every change to every bound property for that object:
loginFrame.addPropertyChangeListener(this);

}

@Override
protected void shutdown() {
System.out.println("Entered into GUI ---> shutdown()");
logger.debug("Termino l'applicazione.");
ulogger.info(Constants.APP_TITLE + "|Arresto "+ Constants.APP_TITLE);
// FileUtils.saveGeneralLogFile(logAppender.getLogInFile());
logAppender.saveGeneralLogFile();
EventBusService.unsubscribe(this);
if (mainFrame != null)
mainFrame.setVisible(false);

}
public static void main(String[] args) {

launch(GUI.class, args);
}

@Override
public void propertyChange(PropertyChangeEvent arg0) {
System.out.println("GUI SingleFrameApplication ---> propertyChange(): " + arg0.getPropertyName());

if (arg0.getPropertyName().equals("buttonLogOffClicked")) {
//System.out.println("GUI SingleFrameApplication ---> richiamo exit");
//exit();

mainFrame.OnWindowClose();
mainFrame.dispose();
mainFrame = null;

showLoginFrame();
}

if (arg0.getPropertyName().equals("loginResult")) {
System.out.println("GUI SingleFrameApplication ---> richiamo MainFrame");
//loginFrame.setVisible(false);
loginFrame.dispose();
loginFrame = null;

showMainFrame();
}

}

private void showMainFrame() {

mainFrame = new MainFrame(settings, tasksSettings, logAppender);
// I add a PropertyChangeListener to the created MainFrame object:
mainFrame.addPropertyChangeListener(this);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

WindowListener exitListener = new WindowAdapter() {

@Override
public void windowClosing(WindowEvent e) {
System.out.println("GUI SingleFrameApplication --> windowClosing");
shutdown();
// mainFrame.setVisible(false);
/*int confirm = JOptionPane.showOptionDialog(frame,
"Are You Sure to Close this Application?",
"Exit Confirmation", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, null, null);
if (confirm == JOptionPane.YES_OPTION) {
System.exit(1);
}*/
}
};

mainFrame.addWindowListener(exitListener);
mainFrame.setVisible(true);
}

然后我有一个MainFram类,它扩展了一个JFrame,其中有一个JButton来执行注销操作,如下所示:

public class MainFrame extends JFrame {
private final Action actionLogOut = new AbstractAction() {
{
putValue(Action.NAME, _("log-out"));
}

@Override
public void actionPerformed(ActionEvent e) {
System.out.println("logOutButton clicked !!!, firePropertyChange() will start");
// System.exit(0);
firePropertyChange("buttonLogOffClicked", false, true);
}
};

public MainFrame(Settings settings, TasksSettings tasksSettings, LogAppender logAppender) {
super();
......................
......................
......................
header.add(new JButton(actionLogOut));
......................
......................
......................
}
}

因此,当单击我的 JButton 时,将执行此方法:

private final Action actionLogOut = new AbstractAction() {
{
putValue(Action.NAME, _("log-out"));
}

@Override
public void actionPerformed(ActionEvent e) {
System.out.println("logOutButton clicked !!!, firePropertyChange() will start");
// System.exit(0);
firePropertyChange("buttonLogOffClicked", false, true);
}
};

事实上,当我单击按钮时,在控制台中,会出现输出:

“点击了 logOutButton!!!,firePropertyChange() 将启动”

然后我执行 firePropertyChange() 方法,我希望该事件由 GUI 类的此方法处理:

@Override
public void propertyChange(PropertyChangeEvent arg0) {
System.out.println("GUI SingleFrameApplication ---> propertyChange(): " + arg0.getPropertyName());

if (arg0.getPropertyName().equals("buttonLogOffClicked")) {
//System.out.println("GUI SingleFrameApplication ---> richiamo exit");
//exit();

mainFrame.OnWindowClose();
mainFrame.dispose();
mainFrame = null;

showLoginFrame();
}

if (arg0.getPropertyName().equals("loginResult")) {
System.out.println("GUI SingleFrameApplication ---> richiamo MainFrame");
//loginFrame.setVisible(false);
loginFrame.dispose();
loginFrame = null;

showMainFrame();
}

}

但是不起作用并且似乎没有进入 firePropertyChange() 方法?

为什么?我错过了什么?

Tnx

安德里亚

最佳答案

当您从 MainFrame 上下文调用 firePropertyChange 时,它会为 MainFrame 触发,它实际上可以监听其属性更改事件。但您已使用 loginFrame.addPropertyChangeListener(this) 将监听器添加到登录框架;如果更改事件由其自己的 firePropertyChange 函数触发,则 loginframe 将监听更改。但是您可以调用 loginFrame.firePropertyChange("buttonLogOffClicked", false, true);来自 MainFrame 类中 actionLogOut Action 的 actionPerformed() 函数。

编辑:

  1. 尝试将 LoginFrame 实例传递给您已创建的用于使用的 MainFrame 实例构造函数。

  2. 或者,在 GUI 类中声明一个名为 fireLogInPropEvent 的静态函数。您需要将您的 LoginFrame 实例声明为静态。然后在这个函数中放置 loginFrame.firePropertyChange("buttonLogOffClicked", false, true) 来监听这个属性。

    public class GUI extends SingleFrameApplication implements PropertyChangeListener {


    private MainFrame mainFrame = null;
    private static LoginFrame loginFrame = new LoginFrame();

    /// your other code

    private void showLoginFrame() {
    // loginFrame = new LoginFrame(); <------- already created hence commenting out
    loginFrame.setVisible(true);
    loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Notify every change to every bound property for that object:
    loginFrame.addPropertyChangeListener(this);

    }

    public static void fireLogInPropEvent()
    {
    loginFrame.firePropertyChange("buttonLogOffClicked", false, true);
    }

    }

关于java - 为什么这个 propertyChange() 方法无法处理这个事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20058312/

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