有一个 bean 属性“可见”,它由class 中的 getterisVisible()
和 setter表示。setVisible()
Window
如何监听这个值?
我想用绑定库的复选框实现流行的“视图”菜单。不幸的是,我看不到如何绑定窗口的“可见”属性。即使我不会编写翻译器,因为我看不到任何预定义的方式来监听这个属性:
package tests;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowStateListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Try_Swing2 {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
final JFrame frame2 = new JFrame();
frame2.addWindowStateListener(new WindowStateListener() {
@Override
public void windowStateChanged(WindowEvent e) {
System.out.println("windowState.newState = " + e.getNewState());
}
});
frame2.addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
System.out.println("windowOpened");
}
@Override
public void windowIconified(WindowEvent e) {
System.out.println("windowIconified");
}
@Override
public void windowDeiconified(WindowEvent e) {
System.out.println("windowDeiconified");
}
@Override
public void windowDeactivated(WindowEvent e) {
System.out.println("windowDeactivated");
}
@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing");
}
@Override
public void windowClosed(WindowEvent e) {
System.out.println("windowClosed");
}
@Override
public void windowActivated(WindowEvent e) {
System.out.println("windowActivated");
}
});
frame2.addPropertyChangeListener("visible", new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
System.out.println("visible = " + evt.getNewValue());
}
});
frame2.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame2.setTitle("This window is controlled by another window");
frame2.setSize(800, 600);
frame2.setLocationRelativeTo(null);
frame2.setVisible(true);
AbstractAction toggleAction = new AbstractAction("Toggle another window visibility") {
@Override
public void actionPerformed(ActionEvent e) {
frame2.setVisible( !frame2.isVisible() );
}
};
JButton toggleButton = new JButton(toggleAction);
JFrame frame1 = new JFrame();
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setTitle("This windows controls");
frame1.setLayout(new FlowLayout());
frame1.add(toggleButton);
frame1.pack();
frame1.setLocation(0, 0);
frame1.setVisible(true);
}
});
}
}