当主应用程序JFrame最小化时,最小化补充JFrame

编程入门 行业动态 更新时间:2024-10-24 18:20:42
本文介绍了当主应用程序JFrame最小化时,最小化补充JFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在处理的应用程序包含一个主JFrame,用户最终可能会从该主JFrame打开另一个补充框架.我正在尝试实现这样一种应用程序的行为,即一旦主框架最小化,辅助框架就被最小化(图标化).

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.

我正在考虑重写主框架的setExtendedState方法以捕获最小化的瞬间,然后从那里触发属性更改事件,以便辅助框架可以对其执行操作.

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.

但是我发现,不幸的是,被覆盖的setExtendedState从未被调用.

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

推荐答案

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

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

更多推荐

当主应用程序JFrame最小化时,最小化补充JFrame

本文发布于:2023-11-04 05:22:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557060.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最小化   应用程序   JFrame

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!