【问题标题】:Java - How to make a set of JInternalFrame independant of each other?Java - 如何使一组 JInternalFrame 相互独立?
【发布时间】:2011-01-29 16:55:29
【问题描述】:

我正在编写一个简短的 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 基本上是我绘制形状的地方。

有什么想法吗?

谢谢

【问题讨论】:

    标签: java mdi jinternalframe jdesktoppane


    【解决方案1】:

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

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

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

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

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

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

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

    【讨论】:

    • 你是对的。我改变了构建 GUI 的方式。现在我有一个 JFrame 而不是 JPanel,它工作得很好!只有一个小问题:我无法调整任何 JInternalFrame(s) 的大小。当我尝试这样做时,他们只是将自己放在上角,并且似乎被禁用了。
    • 同样,Swing 教程有使用内部框架的工作示例,它们没有表现出这种行为。我不知道你可能会做不同的事情。将您的代码与教程代码进行比较,看看有什么不同。
    【解决方案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 窗口而设计的。

    【讨论】:

    • 将两个组件添加到同一个 BorderLayout 容器的 CENTER 区域只会导致第一个组件被第二个组件替换。所以,我认为使用 BorderLayout.CENTER 肯定会导致这里描述的问题。
    【解决方案3】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-22
      • 2021-10-28
      • 1970-01-01
      • 1970-01-01
      • 2018-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多