JInternalFrame 最小化,同时保持当前位置 [英] JInternalFrame minimize while maintaining current location

查看:30
本文介绍了JInternalFrame 最小化,同时保持当前位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要 JInternalFrame 的可图标化/最小化功能来折叠框架(它确实如此),但还要保持 JInternalFrame 在其父组件中的位置.目前,当我按下 JInternalFrame 的最小化按钮时,java 会将组件放在其容器的底部.有没有办法在最小化的同时保持位置?如果没有明显的解决方案,我如何观察可图标的图标并删除默认侦听器?谢谢.

I need the iconable/minimize feature of JInternalFrame to collapse the frame (which it does), but also maintain the JInternalFrame's position within its parent component. Currently, when I press the minimize button of a JInternalFrame, java places the component at the bottom of its container. Is there a way to maintain the location whilst minimizing? If there is no obvious solution, how might I observe the iconable icon and remove the default listener? Thank you.

推荐答案

要修改此行为,您需要创建 javax.swing.DesktopManager 的实现.为了获得大多数已经可用的默认行为,我建议子类化 javax.swing.DefaultDesktopManager.

To modify this behavior you would want to create an implementation of javax.swing.DesktopManager. To get the majority of the default behavior already available I'd suggest subclassing javax.swing.DefaultDesktopManager.

在 DefaultDesktopManager 中,方法 iconifyFrame(JInternalFrame f) 控制整个行为,但在内部使用方法 protected Rectangle getBoundsForIconOf(JInternalFrame f) 来确定图标的边界框架被最小化.您可以在此处返回要使用的内部框架图标的边界.问题是这些值会被缓存,因此如果您希望它每次都移动,则需要执行以下操作.

In DefaultDesktopManager the method iconifyFrame(JInternalFrame f) controls the complete behavior but internally uses the method protected Rectangle getBoundsForIconOf(JInternalFrame f) to determine the bounds for the icon of the frame being minimized. Here you can return the bounds for the icon of the internal frame that you'd like to use. The problem is those values are cached so if you want it to move every time you would need to do something like the following.

import javax.swing.DefaultDesktopManager;
import javax.swing.DesktopManager;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import java.awt.Dimension;

public class CustomDesktopManager extends DefaultDesktopManager {
    @Override
    public void iconifyFrame(JInternalFrame f) {
        super.iconifyFrame(f);

        JInternalFrame.JDesktopIcon icon = f.getDesktopIcon();
        Dimension prefSize = icon.getPreferredSize();
        icon.setBounds(f.getX(), f.getY(), prefSize.width, prefSize.height);
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                JDesktopPane desktopPane = new JDesktopPane();
                DesktopManager dm = new CustomDesktopManager();
                desktopPane.setDesktopManager(dm);
                JInternalFrame internalFrame = new JInternalFrame("Test Internal Frame", true, false, true, true);
                internalFrame.setSize(200, 150);
                internalFrame.setVisible(true);
                desktopPane.add(internalFrame);

                frame.add(desktopPane, BorderLayout.CENTER);
                frame.setSize(800, 600);
                frame.setVisible(true);
            }
        });
    }
}

这篇关于JInternalFrame 最小化,同时保持当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆