2016-06-07 28 views
0

我有一個大面板,包含3個較小的面板(locationPanel,usagePanel,structuralAspectsPanel)。如何使用MigLayout將內容分佈在多個面板上?

每個較小的面板上都有一些JLabelsJCheckBoxes。我繼續前進並將每個面板上的組件集中在一起,但是如何在所有3個面板上完成它們的中心? (請參閱中心的黑線)

我試圖使用MigLayoutnew JLabel("Label here"), "cell 0 0"),但無法創建相同大小的動態間隙的單元佈局選項,以便所有組件均居中。 gap 200(180,300,...)將部件「推」到視覺中心似乎有效,但我不想使用絕對定位/間隙,因爲它們可能很容易破裂。我的問題的圖片: How to center the components over several panels?

這裏是我的源代碼:

public class RiskAssessmentPage extends JPanel { 
    JPanel riskAssessmentPanel = new JPanel(); 

    JPanel locationPanel = new JPanel(); 
    JPanel usagePanel = new JPanel(); 
    JPanel structuralAspectsPanel = new JPanel(); 

    public RiskAssessmentPage() { 
    setLayout(new MigLayout("")); 
    riskAssessmentPanel.setLayout(
     new MigLayout("wrap", "[grow, fill]", "[grow, fill, push][grow, fill, push][grow, fill, push]")); 

    locationPanel.setLayout(new MigLayout("gap rel 2", "[grow, center][grow, left]")); 
    locationPanel.setBorder(BorderFactory.createTitledBorder("Location")); 

    usagePanel.setLayout(new MigLayout("gap rel 2", "[grow, center][grow, left]")); 
    usagePanel.setBorder(BorderFactory.createTitledBorder("Usage")); 

    structuralAspectsPanel.setLayout(new MigLayout("gap rel 2", "[grow, center][grow, left]")); 
    structuralAspectsPanel.setBorder(BorderFactory.createTitledBorder("Structural Aspects")); 

    locationPanel.add(new JLabel("This is the first of all labels")); 
    locationPanel.add(new JCheckBox("Checkbox with Label"), "wrap"); 
    locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
    locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
    locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
    locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
    locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
    locationPanel.add(new JSeparator(), "growx, span"); 
    locationPanel.add(new JLabel("Second Label")); 
    locationPanel.add(new JCheckBox("Checkbox with Label"), "wrap"); 
    locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
    locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
    locationPanel.add(new JSeparator(), "growx, span"); 
    locationPanel.add(new JLabel("This Label is fairly large and long and pushes the text around")); 
    locationPanel.add(new JCheckBox("Checkbox with Label"), "wrap"); 
    locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
    locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 

    usagePanel.add(new JLabel("A label in the usage panel")); 
    usagePanel.add(new JCheckBox("Checkbox with Label"), "wrap"); 
    usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
    usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
    usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
    usagePanel.add(new JSeparator(), "growx, span"); 
    usagePanel.add(new JLabel("And another one and another one and another one")); 
    usagePanel.add(new JCheckBox("Checkbox with Label"), "wrap"); 
    usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
    usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
    usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
    usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
    usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
    usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 

    structuralAspectsPanel.add(new JLabel("Label here")); 
    structuralAspectsPanel.add(new JCheckBox("Checkbox with Label"), "wrap"); 
    structuralAspectsPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
    structuralAspectsPanel.add(new JSeparator(), "growx, span"); 
    structuralAspectsPanel.add(new JLabel("I am so uncreative with label names...")); 
    structuralAspectsPanel.add(new JCheckBox("Checkbox with Label"), "wrap"); 
    structuralAspectsPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
    structuralAspectsPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
    structuralAspectsPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
    structuralAspectsPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
    structuralAspectsPanel.add(new JSeparator(), "growx, span"); 
    structuralAspectsPanel.add(new JLabel("Thats it. I give up with naming them.")); 
    structuralAspectsPanel.add(new JCheckBox("Checkbox with Label"), "wrap"); 
    structuralAspectsPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
    structuralAspectsPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 

    riskAssessmentPanel.add(locationPanel); 
    riskAssessmentPanel.add(usagePanel); 
    riskAssessmentPanel.add(structuralAspectsPanel); 

    add(riskAssessmentPanel, "grow, push"); 
    } 
} 

回答

1

爲了使您的面板列排隊,您需要將它們設置爲相同的大小。在MigLayout中,您可以通過將第一列的首選大小增長率設置爲與所有面板中的值相同。例如

panel1.new MigLayout("", //Unchanged Layout Constraints 
     "[:500, grow, center][grow, left]", //Column Constraints 
     ""); //Unchanged Row Constraints 

panel2.new MigLayout("", //Unchanged Layout Constraints 
     "[:500, grow, center][grow, left]", //Column Constraints 
     ""); //Unchanged Row Constraints 

在該示例中兩個panel1panel2的第一列的優選大小設置爲500以及生長被設定爲它的默認值,從而也相同。

所描述的內容的工作示例可以在這裏看到

import java.awt.*; 
import javax.swing.*; 
import net.miginfocom.swing.MigLayout; 

public class MigLay extends JFrame { 

    private MigLay() { 
     super("Button Layout"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLayout(new MigLayout("wrap", //Layout Constraints 
     "grow, fill", //Column Constraints 
     "grow, fill")); //Row Constraints 
     createPanels(); 
     pack(); 
     setMinimumSize(getSize()); //Sets minimum size to the preferred size. Remove or change this line if you do not want that to happen 
     setVisible(true); 
    } 

    private void createPanels() { 

     JPanel locationPanel = new JPanel(); 
     JPanel usagePanel = new JPanel(); 
     JPanel structuralAspectsPanel = new JPanel(); 

     //JLabels for font metrics 
     JLabel one = new JLabel("This is the first of all labels"); 
     JLabel two = new JLabel("Second Label"); 
     JLabel three = new JLabel("This Label is fairly large and long and pushes the text around"); 
     JLabel four = new JLabel("A label in the usage panel"); 
     JLabel five = new JLabel("And another one and another one and another one"); 

     //Font Metrics 
     FontMetrics metrics = three.getFontMetrics(three.getFont()); //Take longest label manually or dynamically (You will have to add that code) 
     int width = metrics.stringWidth(three.getText()); 

     locationPanel.setLayout(new MigLayout("gap rel 2", "[:" + width + ", grow, center][grow, left]")); 
     locationPanel.setBorder(BorderFactory.createTitledBorder("Location")); 

     usagePanel.setLayout(new MigLayout("gap rel 2", "[:" + width + ", grow, center][grow, left]")); 
     usagePanel.setBorder(BorderFactory.createTitledBorder("Usage")); 

     locationPanel.add(one); 
     locationPanel.add(new JCheckBox("Checkbox with Label"), "wrap"); 
     locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
     locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
     locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
     locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
     locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
     locationPanel.add(new JSeparator(), "growx, span"); 
     locationPanel.add(two); 
     locationPanel.add(new JCheckBox("Checkbox with Label"), "wrap"); 
     locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
     locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
     locationPanel.add(new JSeparator(), "growx, span"); 
     locationPanel.add(three); 
     locationPanel.add(new JCheckBox("Checkbox with Label"), "wrap"); 
     locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
     locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 

     usagePanel.add(four); 
     usagePanel.add(new JCheckBox("Checkbox with Label"), "wrap"); 
     usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
     usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
     usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
     usagePanel.add(new JSeparator(), "growx, span"); 
     usagePanel.add(five); 
     usagePanel.add(new JCheckBox("Checkbox with Label"), "wrap"); 
     usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
     usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
     usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
     usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
     usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 
     usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap"); 

     getContentPane().add(locationPanel); 
     getContentPane().add(usagePanel); 
    } 

    public static void main(String[] args) { 
     new MigLay(); 
    } 
} 
0

我已經想通了,它其實很簡單!

0:0, grow 60加到第一列,grow 40加到第二列。

grow 60/40 -part表示左列獲得可用寬度的60%,而另一列獲得40%。

0:0 -part是關於設置最小/首選/最大寬度(雖然我不得不承認我完全不理解它,但是歡迎解釋!)我爲此僅爲opened a question)。

由於所有面板的左右尺寸都相同,因此它們位於同一點。

locationPanel.setLayout(new MigLayout("gap rel 2", "[0:0, grow 60, center][grow 40, left]")); 

usagePanel.setLayout(new MigLayout("gap rel 2", "[0:0, grow 60, center][grow 40, left]")); 

structuralAspectsPanel.setLayout(new MigLayout("gap rel 2", "[0:0, grow 60, center][grow 40, left]")); 
相關問題