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

发布于 2024-09-13 12:03:58 字数 3023 浏览 6 评论 0原文

我正在开发的应用程序包含一个主 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);
    }
}

The application I'm working on contains a main JFrame, from which users might eventually open another supplementary frame. I am trying to implement such a behavior of the app where the supplementary frame is minimized (iconified) as soon as the main frame gets minimized.

I was thinking of overriding the setExtendedState method of the main frame to capture the moment when it gets minimised, and then fire property change event from there so that the supplementary frame may act upon to it.

I discovered however, that unfortunately the overridden setExtendedState never gets called.

I would greatly appreciate any ideas of achieving the desired behavior. Below is the code I used for testing...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水晶透心 2024-09-20 12:04:06

我正在开发的应用程序
包含一个主 JFrame,从中
用户最终可能会打开另一个
补充框架

应用程序通常设计为使用单个 JFrame。补充窗口是使用 JDialog 创建的。创建对话框时,请确保将框架指定为对话框的所有者。现在,当您图标化/取消图标化框架时,对话框会自动同步图标化。

The application I'm working on
contains a main JFrame, from which
users might eventually open another
supplementary frame

Applications are generally designed to use a single JFrame. Supplementary windows are created by using a JDialog. When you create the dialog make sure you specify the frame as the owner of the dialog. Now when you iconify/deiconify the frame, the dialog is iconified in sync automatically.

做个ˇ局外人 2024-09-20 12:04:05

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

addWindowStateListener(new WindowStateListener() {

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

Just add WindowStateListener to MainFrame to make your code work with property change listener:

addWindowStateListener(new WindowStateListener() {

        @Override
        public void windowStateChanged(WindowEvent e) {
                firePropertyChange(EXTENDED_STATE_KEY, e.getOldState(), e.getNewState());
        }
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文