3

我正在做一个项目,大型机中有 JInternalFrames。现在,我们需要让它们成为 JFrame。我正在考虑使用 JFrame 来保留 JInternalFrame。问题是内部框架的标题栏在那里,用户可以拖动它。

有没有办法让内部框架像 JFrame 中的窗格一样工作?在互联网上搜索后,我发现有人删除了标题栏。

你对此有什么好主意吗?

谢谢!

更新:也许我走错了路。真正的问题是 JInternal 框架无法脱离主框架,或者有什么办法让它看起来像是在框架之外?

4

2 回答 2

4

有什么方法可以使内部框架像 JFrame 中的窗格一样工作

我不确定您所说的窗格是什么意思,但我想像一个JPanel?你当然可以,但为什么,这是我的问题,除非你想要某种快速浮动面板,但你说你不想让它可拖动?所以我有点不确定你的动机,让我厌倦了回答......

问题是Internalframe的标题栏在那里

好吧,这是删除标题窗格的代码此处找到):

//remove title pane http://www.coderanch.com/t/505683/GUI/java/JInternalframe-decoration
BasicInternalFrameTitlePane titlePane =(BasicInternalFrameTitlePane)((BasicInternalFrameUI)jInternalFrame.getUI()).getNorthPane();
jInternalFrame.remove(titlePane);

用户可以拖动它。

我发现JInternalFrame通过删除使其可移动的MouseListeners 使其不可移动,但重要的是要注意它没有必要删除MouseListeners 因为用于使其不可拖动的方法将删除添加NorthPane的sMouseListener因此它对我们来说是不必要的自己删除它。:

//remove the listeners from UI which make the frame move
BasicInternalFrameUI basicInternalFrameUI = ((javax.swing.plaf.basic.BasicInternalFrameUI) jInternalFrame.getUI());
for (MouseListener listener : basicInternalFrameUI.getNorthPane().getMouseListeners()) {
    basicInternalFrameUI.getNorthPane().removeMouseListener(listener);
}

根据您的标题:

如何JInternalFrame填充容器

只需使用s和(将通过覆盖)setSize(int width,int height)JInternalFrame参数调用。JDesktopPanewidthheightJDesktopPanegetPreferredSize()

这会给我们这个:

在此处输入图像描述

import java.awt.Dimension;
import java.awt.HeadlessException;
import java.awt.event.MouseListener;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.SwingUtilities;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane;
import javax.swing.plaf.basic.BasicInternalFrameUI;

/**
 *
 * @author David
 */
public class Test {

    public Test() {
        createAndShowGUI();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test();

            }
        });
    }

    private void createAndShowGUI() throws HeadlessException {
        JFrame frame = new JFrame();
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        final JDesktopPane jdp = new JDesktopPane() {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(300, 300);
            }
        };

        frame.setContentPane(jdp);
        frame.pack();

        createAndAddInternalFrame(jdp);

        frame.setVisible(true);
    }

    private void createAndAddInternalFrame(final JDesktopPane jdp) {
        JInternalFrame jInternalFrame = new JInternalFrame("Test", false, false, false, false);
        jInternalFrame.setLocation(0, 0);
        jInternalFrame.setSize(jdp.getWidth(), jdp.getHeight());

        //remove title pane http://www.coderanch.com/t/505683/GUI/java/JInternalframe-decoration
        BasicInternalFrameTitlePane titlePane = (BasicInternalFrameTitlePane) ((BasicInternalFrameUI) jInternalFrame.getUI()).getNorthPane();
        jInternalFrame.remove(titlePane);

        /*
         //remove the listeners from UI which make the frame move
         BasicInternalFrameUI basicInternalFrameUI = ((javax.swing.plaf.basic.BasicInternalFrameUI) jInternalFrame.getUI());
         for (MouseListener listener : basicInternalFrameUI.getNorthPane().getMouseListeners()) {
         basicInternalFrameUI.getNorthPane().removeMouseListener(listener);
         }
         */
        jInternalFrame.setVisible(true);

        jdp.add(jInternalFrame);
    }
}
于 2013-01-30T11:50:14.437 回答
2

JPanel鉴于您的要求,我建议您在JFrame内容窗格中使用简单的。

于 2013-01-30T09:16:57.530 回答