Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.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 为什么这个propertyChange()方法可以';你不能处理这件事吗?_Java_Swing_Events_Event Handling_Propertychangelistener - Fatal编程技术网

Java 为什么这个propertyChange()方法可以';你不能处理这件事吗?

Java 为什么这个propertyChange()方法可以';你不能处理这件事吗?,java,swing,events,event-handling,propertychangelistener,Java,Swing,Events,Event Handling,Propertychangelistener,我是Swing开发的新手,在使用实现PropertyChangeListener接口的类时遇到以下问题 因此,我有以下GUI类(我只发布这个类中有趣的部分): 然后我有一个MainFram类,它扩展了一个JFrame,其中有一个JButton来执行注销操作,如下所示: public class MainFrame extends JFrame { private final Action actionLogOut = new AbstractAction() { {

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

因此,我有以下GUI类(我只发布这个类中有趣的部分):

然后我有一个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));
    ......................
    ......................
    ......................
    }
}
因此,当单击myJButton时,将执行以下方法:

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);
    }
};
事实上,当我单击控制台中的按钮时,我会看到输出:

“单击注销按钮!!!,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


Andrea

当您从
大型机
上下文调用
firePropertyChange
时,它会为
大型机
触发,该大型机实际上可以监听其属性更改事件。但是,您已经使用
loginFrame.addPropertyChangeListener(this)
,将侦听器添加到了
登录框架
<如果更改事件由自己的
firePropertyChange
函数触发,则code>loginframe将侦听更改。但是,您可以调用
loginFrame.firePropertyChange(“buttonLogOffClicked”,false,true)
;从
actionLogOut
操作的
actionPerformed()
函数,在
MainFrame
类中

编辑:

  • 尝试将
    LoginFrame
    的实例传递给
    MainFrame
    实例构造函数,该构造函数是您为使用而创建的

  • 或者,在GUI类中声明一个名为
    fireloginpropent
    static
    函数。您需要将
    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);
          }
    
       }
    
    公共类GUI扩展SingleFrameApplication实现PropertyChangeListener{
    私有大型机=null;
    私有静态LoginFrame LoginFrame=新LoginFrame();
    ///你的其他代码
    私有void showLoginFrame(){
    
    //loginFrame=new loginFrame();现在真的很新了,请问您是否发布了一个SSCCEIt seams,用于“符合JavaBeans的组件”™ 规格。”.So在
    JavaBean
    上下文中使用。@PM77-1,哪些东西是java bean?@Sage:
    JavaBean
    是一个遵循特定标准的java类。我相信博文中公认的答案很好地解释了这一点。你是说
    JFrame
    本身就是一个bean吗?是的,组件也是bean。嗯,似乎不起作用,because在这个类中我没有LoginFrame实例……我缺少什么?@AndreaNobili:正如Sage所说,你缺少的是你似乎将PropertyChangeListener添加到了错误的对象。你需要将它添加到正确的对象中,代码才有意义和工作。1+@AndreaNobili,你实际上已经理解了这一点,你需要
    MainFrame
    类中的
    LoginFrame
    的一个实例,用于从
    MainFrame
    触发
    LoginFrame
    的属性更改事件。在一个应用程序中使用多个帧时,您将面临这种类型的危险。我以前说过多次,现在告诉您,更改设计样式。使用卡l用一个框架进行布局和做事我无法更改布局样式…我正在处理一个旧的大型应用程序,如果它不太智能,我也必须保留原始布局:(@AndreaNobili,好的。看看我编辑的答案。你现在应该可以使用我提到的更改了
    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);
          }
    
       }