无法拦截扩展PropertyChangeListener的Main类中的PropertyChangeEvent,为什么?

时间:2013-11-14 16:29:10

标签: java swing user-interface propertychangelistener

我是Java Swing 发展的新手,我发现在GUI中使用 PropertyChangeListener 会遇到一些困难。

所以我有一个 Main 类来实现 PropertyChangeListener 接口:

package com.test.login4;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JFrame;


public class Main implements PropertyChangeListener {

    private static LoginFrame loginFrame;

    private static final GUI gui = new GUI();

    public static void main(String[] args) {
          System.out.println("Main ---> main()");   
          loginFrame = new LoginFrame();
          loginFrame.setVisible(true);


    }

    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        System.out.println("Main ---> actionPerformed()");

    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        // TODO Auto-generated method stub
        System.out.println("GUI ---> propertyChange()");

    }

}

然后我有 LoginFrame 类:

package com.test.login4;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;



public class LoginFrame extends JFrame implements ActionListener {

    private static final int FIXED_WIDTH = 550;
    private static final Dimension INITAL_SIZE = new Dimension(FIXED_WIDTH, 230);

    private boolean loginResult = true;

    public LoginFrame() {

        System.out.println("Inside LoginFrame ---> LoginFrame()");

        this.setTitle("XCloud Login");

        //this.setPreferredSize(INITAL_SIZE);
        this.setSize(INITAL_SIZE);
        this.setResizable(false);

        Container mainContainer = this.getContentPane(); // main Container into the main JFrame


        // JPanel creation and settings of the MigLayout on it:
        // JPanel externalPanel = new JPanel();
        JPanelWithBackground externalPanel = null;

        try {
            // externalPanel = new JPanelWithBackground("resources/logo.png");
            externalPanel = new JPanelWithBackground("src/com/test/resources/logo.png");

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        externalPanel.setLayout(new net.miginfocom.swing.MigLayout("fill"));

        externalPanel.add(new JLabel("Username"), "w 50%, wrap");
        JTextField userNameTextField = new JTextField(20);

        externalPanel.add(userNameTextField, "w 90%, wrap");

        externalPanel.add(new JLabel("Password"), "w 50%, wrap");
        JTextField pswdTextField = new JTextField(20);
        externalPanel.add(pswdTextField, "w 90%, wrap");

        JButton loginButton = new JButton("Login");
        loginButton.setActionCommand("loginAction");
        loginButton.addActionListener(this);

        externalPanel.add(loginButton, "w 25%, wrap");

        mainContainer.add(externalPanel);
        // mainFrame.add(mainContainer);
        // loginFrame.setVisible(true);
        this.setVisible(true);
    }

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

        firePropertyChange("loginButtonClicked", false, true); 


    }

}

正如您在 JButton 中所看到的那样,当您执行 actionPerformed 方法时,您可以看到 LoginPer 方法(并且它可以正常工作,因为我通过println看到它并且在这个方法中通过这一行执行 firePropertyChange()方法:

firePropertyChange("loginButtonClicked", false, true); 

然后在 Main 类中,我有 propertyChange()方法,必须拦截此事件,但这似乎不起作用,因为不要输入此方法,不要在我的控制台中打印“GUI ---> propertyChange()”

为什么呢?我错过了什么?

1 个答案:

答案 0 :(得分:2)

您必须使用addPropertyChangeListener注册听众,即:

Main listener = new Main();
loginFrame.addPropertyChangeListener(listener);

有关详细信息和示例,请参阅How to Write a Property Change Listener

编辑:

  

但我在哪里插入您的previuos 2行代码?在什么   类\方法

您没有在发布的示例中分配类Main的实例,因此我不确定它的用途是什么。发布的示例也无法编译。要查看输出,请添加以下行:

loginFrame.addPropertyChangeListener(new Main());

但它只是一个黑客。总而言之,您需要分配一个实现PropertyChangeListener的实例,并使用addPropertyChangeListener方法将其注册到面板。

编辑:

以下是演示基本监听器的示例:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class ListenerDemo {
    public static final String PROP_NAME = "loginButtonClicked";

    public ListenerDemo() {
        DemoPanel panel = new DemoPanel();

        panel.addPropertyChangeListener(PROP_NAME, new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                System.out.println(evt.getPropertyName());
            }
        });

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }

    class DemoPanel extends JPanel {
        public DemoPanel() {
            JButton button = new JButton("Test");
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    firePropertyChange(PROP_NAME, false, true);
                }
            });
            add(button);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new ListenerDemo();
            }
        });
    }
}