在进行级联时包含JPanels的Swing JPanels(使用RelativeLayout构建)意外削减

时间:2016-08-22 18:25:36

标签: java swing relativelayout

我有一个带有JTabbedPane(Name MainTabbedPane )版本的JFrame JPanel(名称 MajorTabPaneContainer ),它从另外两个JPanel构建两列(两者都是Class IncludedColumnPane )。对于JPanels布局" RelativeLayout"使用。

因此,两列比JPanels小。但我没有说明这是从哪里来的。我忙了一段时间,现在已经创建了一个小样本,希望看到发生了什么 - 但我不知道这里发生了什么。我想问一下有人可以看看这里。

Main-Class(MainTabbedPane):

import java.awt.BorderLayout;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.JFrame;
import javax.swing.JTabbedPane;

import de.gombers.myidtravel.tools.gui.main.GUImain;
import de.gombers.myidtravel.tools.gui.main.MenuBar;
import de.gombers.myidtravel.tools.gui.main.MyIDtravelProperties;
import de.gombers.myidtravel.tools.gui.subversion.SvnMaintenance;

public class MainTabbedPane extends JFrame {

    protected JTabbedPane theTabbedPane = new JTabbedPane();

    public MainTabbedPane() {
        super("My Sample Tabbed Pane"); 

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(800, 500);

        MajorTabPaneContainer myPane = new MajorTabPaneContainer();
        theTabbedPane.addTab("A Tab", null, myPane.getPane()); 

        getContentPane().add(theTabbedPane, BorderLayout.CENTER);
    }

    public static void main(String[] args) throws Exception {
        MainTabbedPane menu = new MainTabbedPane();
        menu.setVisible(true);
    }
}

制表选项卡并构建列(MajorTabPaneContainer)

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.brunchboy.util.swing.relativelayout.AttributeConstraint;
import com.brunchboy.util.swing.relativelayout.AttributeType;
import com.brunchboy.util.swing.relativelayout.DependencyManager;
import com.brunchboy.util.swing.relativelayout.RelativeLayout;

public class MajorTabPaneContainer {

    private static final long serialVersionUID = 1L;
    private RelativeLayout relativeLayout = new RelativeLayout();
    private RelativeLayout relativeSubLayout = new RelativeLayout();
    private JPanel thePanel = new JPanel();
    private JPanel theSubPanel = new JPanel();

    public MajorTabPaneContainer() {
        buildPanel();
    }

    public void buildPanel() {
        String thisItem;
        String aboveItem; 
        String prevItem;
        thePanel.setLayout(relativeLayout);

        //   First row
        JButton firstButton = new JButton("Button");
        thisItem="button";
        relativeLayout.addConstraint(thisItem, AttributeType.TOP,
                new AttributeConstraint(DependencyManager.ROOT_NAME, AttributeType.TOP, 10));
        relativeLayout.addConstraint(thisItem, AttributeType.LEFT,
                new AttributeConstraint(DependencyManager.ROOT_NAME, AttributeType.LEFT, 10));
        relativeLayout.addConstraint(thisItem, AttributeType.RIGHT,
                new AttributeConstraint(DependencyManager.ROOT_NAME, AttributeType.LEFT, 100));
        thePanel.add(firstButton, thisItem);
        prevItem=thisItem;
        aboveItem=thisItem;

        buildSubPanel();
        thisItem="SubPanel";
        relativeLayout.addConstraint(thisItem, AttributeType.TOP,
                new AttributeConstraint(aboveItem, AttributeType.BOTTOM, 20));
        relativeLayout.addConstraint(thisItem, AttributeType.LEFT,
                new AttributeConstraint(DependencyManager.ROOT_NAME, AttributeType.LEFT));
//      relativeLayout.addConstraint(thisItem, AttributeType.RIGHT,
//              new AttributeConstraint(DependencyManager.ROOT_NAME, AttributeType.RIGHT));
        thePanel.add(theSubPanel, thisItem);
    }

    public JPanel getPane() {
        return thePanel;
    }

    public void buildSubPanel() {
        int nextIx=-1;
        int boundary=10;
        int gap=10;
        int width=120;

        theSubPanel.setLayout(relativeSubLayout);

        String thisItem="";
        String prevItem=DependencyManager.ROOT_NAME; 
        String aboveItem=DependencyManager.ROOT_NAME; 

        JPanel firstPanel=new IncludedColumnPane("FirstPanel").buildPanel();
        JPanel secondPanel=new IncludedColumnPane("SecondPanel").buildPanel();

        // First Column CheckOut
        nextIx++;
        thisItem=String.format("dummy%s", nextIx);
        relativeSubLayout.addConstraint(thisItem, AttributeType.TOP,
                new AttributeConstraint(DependencyManager.ROOT_NAME, AttributeType.TOP));
        relativeSubLayout.addConstraint(thisItem, AttributeType.LEFT,
                new AttributeConstraint(DependencyManager.ROOT_NAME, AttributeType.LEFT, boundary));
        relativeSubLayout.addConstraint(thisItem, AttributeType.RIGHT,
                new AttributeConstraint(DependencyManager.ROOT_NAME, AttributeType.LEFT, boundary+width));
        theSubPanel.add(firstPanel, thisItem);
        prevItem=thisItem;
        aboveItem=thisItem;

        // Second Column SVN Update
        nextIx++;
        thisItem=String.format("dummy%s", nextIx);
        relativeSubLayout.addConstraint(thisItem, AttributeType.TOP,
                new AttributeConstraint(prevItem, AttributeType.TOP));
        relativeSubLayout.addConstraint(thisItem, AttributeType.LEFT,
                new AttributeConstraint(prevItem, AttributeType.RIGHT, gap));
        relativeSubLayout.addConstraint(thisItem, AttributeType.RIGHT,
                new AttributeConstraint(thisItem, AttributeType.LEFT, width));
        theSubPanel.add(secondPanel, thisItem);
        prevItem=thisItem;

    }
}

该类表示列(IncludedColumnPane)

import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.brunchboy.util.swing.relativelayout.AttributeConstraint;
import com.brunchboy.util.swing.relativelayout.AttributeType;
import com.brunchboy.util.swing.relativelayout.DependencyManager;
import com.brunchboy.util.swing.relativelayout.RelativeLayout;

import de.gombers.myidtravel.tools.gui.objects.ManagedWorkspace;
import de.gombers.myidtravel.tools.gui.subversion.ProjectBean;

public class IncludedColumnPane {
    protected JPanel theColumn = new JPanel(); 
    private final String text;

    public IncludedColumnPane(String text) {
        this.text=text;
    }


    public JPanel buildPanel() {
        RelativeLayout relativeLayout = new RelativeLayout();
        theColumn.setLayout(relativeLayout);
        String thisItem;
        String prevItem;
        String aboveItem = DependencyManager.ROOT_NAME;

        int margin=5; 
        int linespace=0;


        JButton button = new JButton("Button "+text);
        thisItem="button1";
        relativeLayout.addConstraint(thisItem, AttributeType.TOP,
                new AttributeConstraint(DependencyManager.ROOT_NAME, AttributeType.TOP));
        relativeLayout.addConstraint(thisItem, AttributeType.LEFT,
                new AttributeConstraint(DependencyManager.ROOT_NAME, AttributeType.LEFT));
        relativeLayout.addConstraint(thisItem, AttributeType.RIGHT,
                new AttributeConstraint(thisItem, AttributeType.LEFT, 200));
        theColumn.add(button, thisItem);
        prevItem=thisItem;
        aboveItem=thisItem;

        JLabel label = new JLabel("Label "+text);
        thisItem="Label";
        relativeLayout.addConstraint(thisItem, AttributeType.TOP,
                new AttributeConstraint(prevItem, AttributeType.BOTTOM, 20));
        relativeLayout.addConstraint(thisItem, AttributeType.LEFT,
                new AttributeConstraint(prevItem, AttributeType.LEFT));
        relativeLayout.addConstraint(thisItem, AttributeType.RIGHT,
                new AttributeConstraint(thisItem, AttributeType.LEFT, 200));
        theColumn.add(label, thisItem);
        prevItem=thisItem;
        aboveItem=thisItem;

        return theColumn;
    }
}

enter image description here结果如下:

0 个答案:

没有答案