gpt4 book ai didi

java - 当主应用程序 JFrame 最小化时最小化辅助 JFrame

转载 作者:行者123 更新时间:2023-12-02 08:30:43 28 4
gpt4 key购买 nike

我正在开发的应用程序包含一个主 JFrame,用户最终可能会从中打开另一个补充框架。我正在尝试实现应用程序的这种行为,其中一旦主框架最小化,补充框架就会最小化(图标化)。

我正在考虑重写主框架的 setExtendedState 方法来捕获它最小化的时刻,然后从那里触发属性更改事件,以便补充框架可以对其采取行动。

然而,我发现不幸的是,重写的 setExtendedState 永远不会被调用。

我将非常感谢任何实现所需行为的想法。下面是我用于测试的代码...

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;


public class IconifySupplementaryFrameTest {

public static void main(String[] args) {
(new MainFrame()).setVisible(true);
}

}

class MainFrame extends JFrame {
public static final String EXTENDED_STATE_KEY = "extendedState";

MainFrame() {
super("Iconify test - main window");

setLayout(new FlowLayout(FlowLayout.LEADING));

setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 400);
setLocationByPlatform(true);

add(new JButton(new AbstractAction("Show supplementary frame") {
@Override
public void actionPerformed(ActionEvent e) {
SupplementaryFrame.doShow(MainFrame.this);
}
}));
}

@Override
public synchronized void setExtendedState(int state) {
// This overridden method is never called ???
int oldState = getExtendedState();
super.setExtendedState(state);
firePropertyChange(EXTENDED_STATE_KEY, oldState, state);
}
}


class SupplementaryFrame extends JFrame implements PropertyChangeListener {
private static SupplementaryFrame instance;

private SupplementaryFrame(final JFrame parentFrame) {
super("Iconify test - supplementary window");

setSize(300, 300);
setLocationRelativeTo(parentFrame);

parentFrame.addPropertyChangeListener(
MainFrame.EXTENDED_STATE_KEY, this);

addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
instance = null;
parentFrame.removePropertyChangeListener(
MainFrame.EXTENDED_STATE_KEY,
SupplementaryFrame.this);
SupplementaryFrame.this.dispose();
}
});
}


static void doShow(JFrame parentFrame) {
if(instance == null) {
instance = new SupplementaryFrame(parentFrame);
instance.setVisible(true);
}
else {
// omitted _ugly_ code to bring this window (instance) to front
}

}


@Override
public void propertyChange(PropertyChangeEvent evt) {
int state = this.getExtendedState();
int parentState = ((Integer)evt.getNewValue()).intValue();

if((parentState & ICONIFIED) == ICONIFIED)
this.setExtendedState(state | ICONIFIED);
}
}

最佳答案

只需将 WindowStateListener 添加到 MainFrame 即可使您的代码与属性更改监听器一起使用:

addWindowStateListener(new WindowStateListener() {

@Override
public void windowStateChanged(WindowEvent e) {
firePropertyChange(EXTENDED_STATE_KEY, e.getOldState(), e.getNewState());
}
});

关于java - 当主应用程序 JFrame 最小化时最小化辅助 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3449948/

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