0

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

所以我有以下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()方法?

为什么?我错过了什么?

肿瘤坏死因子

安德烈亚

4

1 回答 1

1

当您firePropertyChangeMainFrame上下文调用时,它会触发MainFrame,它实际上可以监听它的属性更改事件。但相反,您login frame使用loginFrame.addPropertyChangeListener(this);添加了侦听器。loginframe如果更改事件由它自己的firePropertyChange函数触发,将侦听更改。但是你可以打电话loginFrame.firePropertyChange("buttonLogOffClicked", false, true);从Action的actionPerformed()功能,在类。actionLogOutMainFrame

编辑:

  1. 尝试将实例传递LoginFrameMainFrame实例构造函数,你已经创建了使用。

  2. 或者,在您的 GUI 类中声明一个static名为 like 的函数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);
          }
    
       }
    
于 2013-11-18T21:51:14.083 回答