1

我是 Java Swing开发的新手,在我的 GUI 中使用PropertyChangeListener时发现了一些困难。

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

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


    }

}

正如您在此LoginFrame类中看到的,当用户单击JButton时,会执行actionPerformed方法(并且它有效,因为我通过 println 看到它)并在此方法中通过以下行执行firePropertyChange()方法:

firePropertyChange("loginButtonClicked", false, true); 

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

为什么?我错过了什么?

4

1 回答 1

2

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

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

有关详细信息和示例,请参阅如何编写属性更改侦听器。

编辑:

但是我在哪里插入你之前的 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();
            }
        });
    }
}
于 2013-11-14T16:38:40.680 回答