2016-06-07 14 views
0

MigLayout (see the example here)을 사용하여 몇 개의 패널 위에 일부 구성 요소를 집중 시키려고했습니다. 모든 패널에서 같은 비율로 페이지를 분할하면 갑자기 효과가있었습니다.여러 패널에 구성 요소를 집중시키기 위해 MigLayout에서 "X : X"를 사용해야하는 이유는 무엇입니까?

그러나 각 grow 앞에 "0 : 0"매개 변수를 사용해야했는데 정확히 무슨 일이 일어나는지 이해하지 못합니다. 예를 들어이 거의에 필요하지 않기 때문에, 그러나 짧은 버전이

은 내가 MigLayout CheatSheet-PageQuickStart-guide에 갔다하지만 형식은 "최대 분 : 선호"값 > 0

와 매개 변수를 찾을 수 최대 크기를 지정하십시오. 단일 값 (예 : "10")은 기본 크기 만 설정하며 "null : 10 : null"및 ": 10 :"및 "n : 10 : n"과 정확하게 동일합니다.

두 값 (예, "10:20")과 바람직한 최소 크기를 의미하고, "10 : 20 : NULL"정확히 동일하고 "10시 20분"및 "N : 10 : 20"

그러나 0을 매개 변수로 사용하거나 X:X or X:Y을 사용해야하는 이유는 무엇입니까? 예를 들어 X:Y:Z or X:X:X이 아닌 이유는 무엇입니까?

그렇다면 여기에 X:X을 사용하여 구성 요소를 가운데에 배치해야하는 이유는 무엇입니까?

+0

Font Metrics에 의해 결정된다

작업 예
:Y를 사용? 어떤 경우에는 무슨 일이 벌어지고 있는지 알아내는 데 도움이 될 것입니다. 즉, '0 : 0'은 최소값이고 기본 크기는 0이므로 구성 요소가 기본적으로 보이지 않을 때까지 축소 할 수 있다고 가정합니다. 그러나 실제 크기는 레이아웃을 정의한 방법에 따라 그보다 더 클 것입니다.시도하고 도움을 청하려면 더 많은 정보가 필요합니다. – Thomas

+0

필자는 패널을''debug "'- 옵션으로 테스트했지만 차이점이 없으므로 생략했습니다. 각각의'JLabel'은 자체 적색 테두리를 가지고 있습니다. '0 : 0'을 사용하지 않으면 왼쪽이 조금 더 커지고, 왼쪽면은 조금 작아집니다. 여분의 세포가 없기 때문에 어떤 정보를 얻을 수 있는지 알지 못합니다. 또한 "0"매개 변수가 설명되지 않았기 때문에 내 질문에 MigLayout 설명서가 더 많이 중점을 둡니다. – hamena314

+0

'... he 매개 변수 "0"이 설명되지 않았습니다. - 음, 0이 실제로 추론 할 수있는 것 외에도 특별한 의미가 아니라면 특별히 문서에 언급하지 않았습니다. – Thomas

답변

1

여기서 중요한 비트가 기본 크기입니다. 귀하의 다른 질문에, 당신은 다음 코드를 사용하여 작동하도록했다고하셨습니다. 이 것

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]")); 

또한

locationPanel.setLayout(new MigLayout("gap rel 2", "[:0:, grow, center][grow, left]")); 
usagePanel.setLayout(new MigLayout("gap rel 2", "[:0:, grow, center][grow, left]")); 
structuralAspectsPanel.setLayout(new MigLayout("gap rel 2", "[:0:, grow, center][grow, left]")); 
//Obviously you could use any number not just 0 

옵션을 사용할 수를 쓴 한 경우 일 수 있습니다 (여기서 X < = Y < = Z)

X:Y:Z 
X:Y: 
:Y:Z 
:Y: 
:Y 

가 왜 그것을 않습니다 이 작업을 수행?
열이 같은 비율로 증가하고 세 개의 패널 모두에서 첫 번째 열의 기본 크기를 설정할 때 MigLayout은 초기 열 크기를 기본 크기로 설정하므로 초기 크기를 설정합니다.

essensce에서는 같은 크기의 열을 설정하고 같은 비율로 커지므로 같은 크기로 유지됩니다. 따라서 그들은 줄을 서 있습니다. 당신이 MigLayout이 UI에 시각적 레이아웃 정보를 렌더링하는 디버그 모드를 가지고 있다는 사실을 알고 계셨습니까 Y

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(); 
    } 
} 
+1

_ 에센셜에서 열을 같은 크기로 설정하면 같은 비율로 커지므로 서로 같은 크기로 유지됩니다. _ 이것은 무엇이 일어나고 있는지 이해하는 데 도움이되는 부분입니다. 감사! – hamena314

+0

다행히 도울 수있었습니다. – Dan