我是 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()”我的控制台
为什么?我错过了什么?