Java - 如何使一组JInternalFrame彼此独立?

时间:2010-03-20 01:22:21

标签: java mdi jinternalframe jdesktoppane

我正在编写一个简短的Paint程序,我正在尝试为它制作MDI架构。为了实现这一点,我在JDesktopPane中使用了JInternalFrame。 虽然我获得了多个帧,但实际上并没有正常工作。 基本上,如果我有2个JInternalFrame,我只能在最后一个上绘制。另一个似乎被禁用了。

这是一段简短的视频,展示了这个问题。 http://bit.ly/9ydiwM

以下是代码的一部分。

Panneau.java
public class Panneau extends JDesktopPane
{

    /** La liste de fenêtres ouvertes */
    private static ArrayList<Cadre> cadres;

    /** Le pannel comportant la liste des formes à dessiner*/
    private Pannel pannel;

    /** La barre d'outils */
    private ToolBox toolBox;

    public Panneau()
    {

        this.setLayout(new BorderLayout());

        // Initialisations des listes de cadres
        cadres = new ArrayList<Cadre>();

        // En haut ToolBox
        toolBox = new ToolBox();
        this.add(toolBox, BorderLayout.NORTH);

        **// Intialisation de la première internal frame
        Cadre cadre = new Cadre();
        this.add(cadre, BorderLayout.CENTER);**

        cadres.add(cadre);

        // Ajout du pannel à gauche
        pannel = new Pannel();

        this.add(pannel, BorderLayout.WEST);

    }

    /**
     * Crée une nouvelle fenêtre de dessin
     * 
     */
    **public void createNewInternalFrame()
    {
        Cadre cadre = new Cadre();
        this.add(cadre, BorderLayout.CENTER);

        cadres.add(cadre);
    }**
}

public class Cadre extends JInternalFrame
{
    /** Largeur par d'une fenêtre interne */
    private int width;

    /** Hauteur d'une fenêtre interne */
    private int height;

    /** Titre d'une fenêtre interne */
    private String title;

    /** Toile associée à la fenêtre interne */
    private Toile toile;

    public Cadre()
    {
        width = 400;
        height = 400;
        title = "Form";

        toile = new Toile();

        this.setTitle(title);

        this.setSize(width, height);

        this.setEnabled(true);

        this.setResizable(true);

        this.setAutoscrolls(true);

        this.setClosable(true);

        this.setIconifiable(true);

        this.setDoubleBuffered(true);

        this.setContentPane(toile);

        this.setVisible(true);

        this.pack();    
    }
}

基本上,Panneau是包含GUI的所有不同部分的主窗口。我可以创建我想要的JInternalFrame,使用:Panneau.createNewInternalFrame()。 厕所基本上是我绘制形状的地方。

有什么想法吗?

由于

3 个答案:

答案 0 :(得分:4)

您正在错误地使用JDesktopPane。桌面窗格“不”故意使用布局管理器。这允许您添加多个内部框架并单独拖动它们。

您的课程不应该扩展JDesktopPane,因为您没有向其添加任何新功能。

所以一般来说你所有的逻辑都应该处理JFrame。那就是:

a)创建工具栏并将其添加到内容窗格的NORTH。

b)您创建桌面窗格并将其添加到内容窗格的CENTER

c)您创建内部框架,并将它们创建到桌面窗格

阅读Swing教程,了解使用内部框架的示例。

答案 1 :(得分:2)

我认为问题在于你在BorderLayout中覆盖了CENTER方向。这样做的结果是两个盒子基本上是第二个添加的盒子,它对那些根本不是为它设计的组件造成严重破坏。因此,层次结构有两个不同的元素,布局管理器有第二个元素设置CENTER组件,布局管理器可能处理相当多的东西。

请注意BorderLayout中的以下代码(是的,它已被弃用,但无论如何都会被非弃用的方法调用):

/**
 * @deprecated  replaced by <code>addLayoutComponent(Component, Object)</code>.
 */
@Deprecated
public void addLayoutComponent(String name, Component comp) {
  synchronized (comp.getTreeLock()) {
    /* Special case:  treat null the same as "Center". */
    if (name == null) {
        name = "Center";
    }

    /* Assign the component to one of the known regions of the layout.
     */
    if ("Center".equals(name)) {
        center = comp;
    } else if ("North".equals(name)) {
        north = comp;
    } else if ("South".equals(name)) {
        south = comp;
    } else if ("East".equals(name)) {
        east = comp;
    } else if ("West".equals(name)) {
        west = comp;
    } else if (BEFORE_FIRST_LINE.equals(name)) {
        firstLine = comp;
    } else if (AFTER_LAST_LINE.equals(name)) {
        lastLine = comp;
    } else if (BEFORE_LINE_BEGINS.equals(name)) {
        firstItem = comp;
    } else if (AFTER_LINE_ENDS.equals(name)) {
        lastItem = comp;
    } else {
        throw new IllegalArgumentException("cannot add to layout: unknown constraint: " + name);
    }
  }
}

使用合适的布局管理器来完成这项工作会很酷,但它们并不适用于使用MDI窗口。

答案 2 :(得分:0)