1

我正在编写一个简短的 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 的所有不同部分的主窗口。我可以使用 Panneau.createNewInternalFrame() 创建所需的 JInternalFrame。Toile 基本上是我绘制形状的地方。

任何的想法 ?

谢谢

4

3 回答 3

4

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

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

所以总的来说,你所有的逻辑仍然应该处理 JFrame。那是:

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

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

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

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

于 2010-03-20T03:25:21.907 回答
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 窗口而设计的。

于 2010-03-20T01:39:56.387 回答
0

请参阅如何使用内部框架InternalFrameDemo.

于 2010-03-20T05:11:48.667 回答