2012-10-20 6 views
0

모든 조합을 시도하고 모든 문서를 살펴 봤지만 GridBagLayout 사용법을 알 수 없습니다.GridBagLayout 지원

JPanel의 LayoutManager 인 GridBagLayout과 3 개의 구성 요소에서 GridBagConstraints를 사용하는 JPanel에는 3 개의 구성 요소가 있습니다.

아래 코드와 같이 3 개의 요소가 올바르게 표시됩니다. 문제는 첫 번째 구성 요소가 때로는 꽤 긴 JLabel이며,이 경우 JLabel은 확장되어 다른 두 구성 요소를 더 작게 만듭니다.

내 목표는 첫 번째 요소가 첫 번째 2 개 열을 차지하고 나머지 두 요소가 나머지 2 개를 차지하는 GridBagLayout이 1 행 4 열인 JPanel을 갖는 것입니다. 그들의 칼럼 밖으로 확장하십시오.

private static void setConstraints(GridBagConstraints constraints, int gridx, int gridy, int weightx, Insets insets) { 
    constraints.gridx = gridx; 
    constraints.weightx = weightx; 
    constraints.insets = insets; 
} 

gridBagLayout = new GridBagLayout(); 
constraints = new GridBagConstraints(); 
centerPanel = new JPanel(gridBagLayout); 

constraints.fill = GridBagConstraints.HORIZONTAL; 
fileNameLabel = new JLabel("Resolving: '" + EngineHelpers.trimStringToFitPanel(urlTextField.getText(), 70) + "'"); 
setConstraints(constraints, 0, 0, 2, new Insets(0, 5, 5, 0)); 
gridBagLayout.setConstraints(fileNameLabel, constraints); 
progressBar = new JProgressBar(); 
progressBar.setStringPainted(true); 
setConstraints(constraints, 1, 0, 1, new Insets(0, 5, 5, 0)); 
gridBagLayout.setConstraints(progressBar, constraints); 
cancelDownloadButton = new JButton("Cancel"); 
setConstraints(constraints, 2, 0, 1, new Insets(0, 0, 0, 0)); 
gridBagLayout.setConstraints(cancelDownloadButton, constraints); 

centerPanel.add(fileNameLabel); 
centerPanel.add(progressBar); 
centerPanel.add(cancelDownloadButton); 

미리 감사드립니다. 바보 같은 질문에 사과드립니다.

답변

2

버튼과 진행률 표시 줄 모두에 대해 weightx을 0으로 설정해야 할 수 있습니다. 레이블의 경우, 가중치를 1로 설정하십시오. 그러면 단추와 진행 막대가 전혀 확장되지 않고 진행 막대가 모든 여분의 공간을 차지하게됩니다.

진도 표시 줄 제약 조건의 gridwidth도 2로 설정되어 실제로 2 개의 열을 사용합니다.

마지막으로, centerPanel의 위치에 따라 컨테이너를 채우기 위해 실제로 확장되지 않을 수 있습니다. centerPanel이 BorderLayout이있는 부모에 배치되면 확장되어야합니다. 디버깅을 위해 centerPanel.setBorder(BorderFactory.createLineBorder(Color.RED))으로 경계를 추가 할 수 있습니다. 버튼, 레이블 및 진행률 막대에서도 유용 할 수 있습니다.

+0

지금 사용해 보겠습니다. 감사합니다. – Cristian

+0

지금은 감사합니다. – Cristian

관련 문제