2012-10-01 5 views
2

자, 이제는 패널에 3 개의 구성 요소 (JScrollpane, JPanel 및 JTabbedPane)가 설치되었습니다 : .GridBagLayout 및 ScrollPane

this.plan = new JPanel(); 
this.plan.setLayout(new GridBagLayout()); 
GridBagConstraints c = new GridBagConstraints(); 

//The ScrollPane 
this.SrsDocument = new JTextArea(); 
this.SrsDocument.setEditable(false); 
this.SrsDocumentScroll = new JScrollPane(this.SrsDocument); 
c.fill = GridBagConstraints.BOTH; 
c.weightx = 0.33; 
c.weighty = 1.0; 
c.gridx = 0; 
this.plan.add(this.SrsDocumentScroll, c); 
... 
//The Panel 
this.TreePanel = new JPanel(); 
this.TreePanel.setLayout(new BoxLayout(this.TreePanel, BoxLayout.Y_AXIS)); 
this.Tree = new JTree(); 
((DefaultTreeModel)(this.Tree.getModel())).setRoot(null); 
this.Tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); 
this.TreeScroll = new JScrollPane(this.Tree); 
this.TreePanel.add(this.TreeScroll); 
c.gridx = 1; 
c.weightx = 0.33; 
this.plan.add(this.TreePanel, c); 
... 
//The TabbedPane 
this.currentFunction = new JTabbedPane(); 
c.gridx = 2; 
c.weightx = 0.33; 
this.plan.add(this.currentFunction, c); 

이 레이아웃에는 3 개의 열이 있고 모두 너비가 있어야합니다. 초기 디스플레이에는 this.SrsDocumentScroll의 폭이 훨씬 좁은 다음 다른 2가 있습니다. 또한 창 크기를 조정할 때 this.SrsDocumentScroll이 소비되는 반면 다른 하나는 this.SrsDocumentScroll이 완전히 소모 될 때까지 크기가 조정되지 않습니다. 나는 this.plan이 크기를 조정함에 따라 세 가지 모두가 똑같이 크기를 조정할 것으로 예상했다. weightx는 내가 가정 한 것처럼 크기를 조정할 때 공간을 분배하는 방법을 결정하지 않습니까?

3 개의 내용은 textArea가 텍스트로 채워지고 tree는 루트로만 표시되며 탭이있는 창에는 탭이 하나만 추가되어야합니다. 모든 내용이 동적으로 변경되지만 크기 조정은 문제가있는 동작이 아니며 창의 크기 조정과 크기 조정은 왼쪽에서 오른쪽으로 똑같이 소모됩니다.

EDIT : 이것은 더 많은 테스트가 될 것입니다. 그 이유는 제가 weightx에 0.33을 사용했기 때문입니다. 최종 결과는 tabbedpane에 가중치를 부여한 후 다른 탭필에 가중치를 부여하는 것입니다. 더 많이 (0.25,0.25,0.5) 같았지만, 당신이 나를 폴로한다면 그 값만 다루면된다.

답변

2

이 레이아웃에는 3 개의 열이 있고 모두 짝수 너비입니다. ... 나는 세 가지 모두 똑같이 크기를 조정할 것으로 예상 했었습니다 ...

GridBagLayout에 추가 된 구성 요소의 preferredSize, maximumSize 및 minimumSize에 따라 달라질 수 있습니다. 3 개의 열에 같은 크기의 구성 요소가 필요한 경우 GridBagLayout을 사용하지 말고 GridLayout(1, 3) (1 행 3 열) 또는 GridLayout(0, 3) (3 열 및 가변 행 수)을 사용하십시오.

은 주석 또는이 두 줄의 주석, 무슨 뜻인지 보여 아래의 코드를 실행하려면 :

 // setSizes(btn, scrollpane); 
     // setSizes(label, scrollpane); 

GridBagTest.java에게 응답을

import java.awt.*; 
import javax.swing.*; 

public class GridBagTest { 
    public static void main(String[] args) { 
     JPanel panel = new JPanel(new GridBagLayout()); 

     JTextArea textarea = new JTextArea(5, 30); 
     JScrollPane scrollpane = new JScrollPane(textarea); 

     JButton btn = new JButton("Foo"); 

     JLabel label = new JLabel("Bar"); 
     label.setBorder(BorderFactory.createTitledBorder("Bar")); 

     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.fill = GridBagConstraints.BOTH; 
     gbc.gridheight = 1; 
     gbc.gridwidth = 1; 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     gbc.weightx = 1.0; 
     gbc.weighty = 1.0; 

     panel.add(scrollpane, gbc); 

     gbc.gridx = 1; 
     panel.add(btn, gbc); 

     gbc.gridx = 2; 
     panel.add(label, gbc); 

     // **** comment or uncomment the lines below 
     // setSizes(btn, scrollpane); 
     // setSizes(label, scrollpane); 

     JFrame frame = new JFrame("GBC Test"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(panel); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 

    } 

    private static void setSizes(Component changeComponent, 
     Component templateComponent) { 
     changeComponent.setPreferredSize(templateComponent.getPreferredSize()); 
     changeComponent.setMaximumSize(templateComponent.getMaximumSize()); 
     changeComponent.setMinimumSize(templateComponent.getMinimumSize()); 
    } 
} 
+0

감사합니다. 이것은 시험의 더 많은 것 인 ment이다, thats는 나의 weightx 's를 위해 0.33를 사용했다. 최종 결과는 tabbedpane에 가중치를 부여한 후 다른 탭필에 가중치를 부여하는 것입니다. 당신이 나를 접한다면 더 많이 (0.25,0.25,0.5). 나는 아마 그것을 언급해야했다. 감사합니다 –

+0

@ Billium813 : 그리고 GridBagLayout는 그 테스트에 실패합니다. 다시 말하지만 컨테이너에 추가되는 구성 요소의 preferredSize 및 기타 특성을 고려하지 않았습니다. –

+0

후속 질문으로 왜이 매개 변수를 사용하는 그리드 레이 아웃처럼 작동하지 않습니까? 이제는 당신이 언급 했으니 까,이 숫자는 그 행동을 복사해서는 안되니? –