gpt4 book ai didi

java - 当 JInternalFrame 最大化/最小化时触发的事件

转载 作者:行者123 更新时间:2023-11-30 03:21:57 25 4
gpt4 key购买 nike

我编写了以下代码来处理 Jinternalframe 最小化或最大化时的事件。

 jif.addComponentListener(new ComponentListener() {
public void componentResized(ComponentEvent e) {
Boolean isMax = ((JInternalFrame)e.getComponent()).isMaximum();

if(isMax == false)
{
// TODO Auto-generated method stub
}
else if(isMax == true )
{
// TODO Auto-generated method stub
}
}

public void componentMoved(ComponentEvent e) {
// TODO Auto-generated method stub

}

public void componentShown(ComponentEvent e) {
// TODO Auto-generated method stub

}

public void componentHidden(ComponentEvent e) {
// TODO Auto-generated method stub

}
}
);

问题在于 isMax 部分在框架加载时被触发。我希望这个事件仅在 Jinternalframe 完全加载后发生。我还注意到当我最小化容器小程序时会触发此事件。

有什么解决办法吗?

最佳答案

您也许可以使用 PropertyChangeListenerInternalFrameListener:

import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import javax.swing.*;
import javax.swing.event.*;

public class InternalFrameEventTest {
public JComponent makeUI() {
final JDesktopPane desktop = new JDesktopPane();
JPanel p = new JPanel(new BorderLayout());
p.add(desktop);
p.add(new JButton(new AbstractAction("New") {
@Override public void actionPerformed(ActionEvent e) {
JInternalFrame f = new JInternalFrame("title", true, true, true, true);
InternalFrameHandler handler = new InternalFrameHandler();
f.addInternalFrameListener(handler);
f.addPropertyChangeListener(handler);
f.setSize(240, 120);
f.setVisible(true);
desktop.add(f);
}
}), BorderLayout.NORTH);
return p;
}
public static void main(String... args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new InternalFrameEventTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}

class InternalFrameHandler implements PropertyChangeListener, InternalFrameListener {
//PropertyChangeListener
@Override public void propertyChange(PropertyChangeEvent e) {
if (JInternalFrame.IS_MAXIMUM_PROPERTY.equals(e.getPropertyName())) {
System.out.println("isMaximum: " + e.getNewValue());
}
}
//InternalFrameListener
@Override public void internalFrameClosing(InternalFrameEvent e) {
System.out.println("internalFrameClosing");
}
@Override public void internalFrameClosed(InternalFrameEvent e) {
System.out.println("internalFrameClosed");
}
@Override public void internalFrameOpened(InternalFrameEvent e) {
System.out.println("internalFrameOpened");
}
@Override public void internalFrameIconified(InternalFrameEvent e) {
System.out.println("internalFrameIconified");
}
@Override public void internalFrameDeiconified(InternalFrameEvent e) {
System.out.println("internalFrameDeiconified");
if (e.getInternalFrame().isMaximum()) {
System.out.println("isMaximum: " + e.getInternalFrame().isMaximum());
}
}
@Override public void internalFrameActivated(InternalFrameEvent e) {
System.out.println("internalFrameActivated");
}
@Override public void internalFrameDeactivated(InternalFrameEvent e) {
System.out.println("internalFrameDeactivated");
}
}

关于java - 当 JInternalFrame 最大化/最小化时触发的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31133370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com