简体   繁体   English

关于PropertyChangeListener的这种推理正确吗?

[英]Is this reasoning about PropertyChangeListener correct?

I have some doubts about the use of the PropertyChangeListener interface. 我对使用PropertyChangeListener接口有一些疑问。

I have a class named GUI that implements the PropertyChangeListener interface. 我有一个名为GUI的类,该类实现PropertyChangeListener接口。

In this class I have the following method that create and show a JFrame ( LoginFrame is a custom class that extends JFrame ): 在此类中,我具有以下创建和显示JFrameLoginFrame是扩展JFrame的自定义类):

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

So, on my LoginFrame object I add a PropertyChangeListener . 因此,在我的LoginFrame对象上,添加一个PropertyChangeListener So I think that I am adding a mechanism for which when change the value of a property in this object it notify this change that will be handle by the following method (declared in my GUI class): 因此,我认为我正在添加一种机制,当更改此对象中的属性值时,它将通知此更改,该更改将通过以下方法(在我的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();
    }

}

In the specific case in my LoginFrame class I have a JButton that if clicked fire the event that will be handled by the previous method, in this way: 在我的LoginFrame类中的特定情况下,我有一个JButton ,如果单击该按钮将触发将由先前方法处理的事件,方法是:

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    System.out.println("Button LogIn cliccked");

  //  this.addPropertyChangeListener(listener);         // I add the PropertyChange Listener to this LoginFrame object

    // I fire a PropertyChange: the event start and will be handled by the propper propertyChange() method definied in the listener class: 

    firePropertyChange("loginResult", false, loginResult);   

}

Is it my reasoning correct? 我的推理正确吗?

Thanks 谢谢

Andrea 安德里亚

  1. Instead of firing property change from Action performed function directly, better extends your target bean class, define a setXXX() method to change the xxx property. 与其直接从Action执行的函数中触发属性更改,不如更好地扩展您的目标bean类,而是定义一个setXXX()方法来更改xxx属性。 All Java beans is incorporating with getXXX() and setXXX() method to get and set their Property xxxx . 所有Java bean都与getXXX()setXXX()方法结合在一起,以获取和设置其Property xxxx The setXXX() is the one changing the property. setXXX()是更改属性的那个。 we should fire the property, After we change the property, in the context we are changing it, hence it is the setXXX() method. 我们应该触发该属性,在更改属性后,在上下文中对其进行更改,因此它是setXXX()方法。 Let us look into setPreferredSize(Dimesion) method source code of Component class : 让我们看一下Component类的setPreferredSize(Dimesion)方法源代码:

      public void setPreferredSize(Dimension preferredSize) { if (prefSizeSet) { old = this.prefSize; } else { old = null; } this.prefSize = preferredSize; prefSizeSet = (preferredSize != null); firePropertyChange("preferredSize", old, preferredSize); } 

    See, we are firing the property upon changes of the property with the corresponding property name. 看到,我们在更改具有相应属性名称的属性时触发该属性。 The advantage is that it adds better clarity and make the code structure more readable. 优点是它增加了清晰度,并使代码结构更具可读性。

  2. Rather than with conditional checking with each property while listening, i would like to use: addPropertyChangeListener("aProperty", PropertyChangeListener) method: which will listen to specific property changes as defined by in place of "aProperty" . 我不希望在监听时对每个属性进行条件检查,而是要使用: addPropertyChangeListener("aProperty", PropertyChangeListener)方法:该方法将侦听由代替"aProperty"定义的特定属性更改。

  3. As recommended by @Hovercraft below, the property name be a public String constant so as not to run into spelling or capitalization issues. 如下面的@Hovercraft所建议,属性名称是一个公共的String常量,以免遇到拼写或大小写问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM