簡體   English   中英

Java-如何使一組JInternalFrame彼此獨立?

[英]Java - How to make a set of JInternalFrame independant of each other?

我正在編寫一個簡短的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是我繪制形狀的地方。

任何想法 ?

謝謝

您使用的JDesktopPane錯誤。 桌面窗格“不”故意使用布局管理器。 這使您可以添加多個內部框架並分別拖動它們。

您的類不應擴展JDesktopPane,因為您沒有向其添加任何新功能。

因此,一般而言,您的所有邏輯仍應處理JFrame。 那是:

a)創建工具欄並將其添加到內容窗格的NORTH。

b)創建桌面窗格並將其添加到內容窗格的CENTER

c)創建您的內部框架,並將它們放入桌面窗格

閱讀Swing教程以獲取使用內部框架的示例。

我認為問題在於您正在覆蓋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窗口而設計的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM