2013-08-26 4 views
0

다소 간단한 GUI가 있으며 창 왼쪽에 버튼과 컨트롤을 만들려고합니다. 오른쪽에는 결국 내용을 표시하는 텍스트 영역이 있습니다. 왼쪽에는 사용자가 조작 할 수있는 버튼과 컨트롤이 있습니다. 나는 지금 가지고있는 것을 만들기 위해 많은 레이아웃 관리자 (그리고 그들은 상당히 까다 롭다)를 사용했다.Java GUI의 크기를 조정할 때 세로 간격을 방지하십시오.

왼쪽 컨트롤의 컨테이너가 사용하는 BoxLayout에 대한 오라클의 문서를 살펴 보았습니다. 윈도우 크기를 조정할 때 버튼 간격이 벌어지는 것을 방지 할 수있는 방법이 없습니다. 나는 그 (것)들을 정상에 위로 박살 내고 다만 간격을두기없이 거기 체재 한 ㄴ다는 것을 싶을. BoxLayout의 '글루'기능은 실제로 당신이 생각하는대로하지 않습니다. 아마 고무 밴드라고해야합니다.

내 질문은 화면의 크기가 조정됨에 따라 왼쪽의 콘텐츠가 더 넓고 넓게 분리되는 것을 어떻게 유지합니까?

내 GUI가 :

당신이 상자 레이아웃을 사용하여 고정하는 경우
public class TestCode extends JFrame{ 

JTextArea textArea = new JTextArea(); 
JComboBox <String> typeComboBox; 
JTextField searchField; 
JTextField fileField; 

public TestCode() { 
    System.out.println ("In constructor"); 
    setTitle ("GUI Test"); 
    setSize (600, 300); 
    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 
    setVisible (true); 

    JScrollPane scrollPane = new JScrollPane(textArea); 
    add(scrollPane, BorderLayout.CENTER); 

    JButton readButton = new JButton("Read File"); 
    JButton displayButton = new JButton("Display"); 
    JButton searchButton = new JButton("Search"); 


    searchField = new JTextField(10); 
    fileField = new JTextField(15); 

    typeComboBox = new JComboBox <String>(); 
    typeComboBox.addItem("Index"); 
    typeComboBox.addItem("Type"); 
    typeComboBox.addItem("Name"); 

    JPanel container = new JPanel(); 
     container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS)); 
    JPanel filePanel = new JPanel(); 
     filePanel.setLayout(new FlowLayout(FlowLayout.LEFT)); 
     filePanel.add(new JLabel("Source file:", SwingConstants.RIGHT)); 
     filePanel.add(fileField); 
     filePanel.add(readButton); 
    JPanel displayPanel = new JPanel(); 
     displayPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); 
     displayPanel.add(new JLabel("Display data:", SwingConstants.RIGHT)); 
     displayPanel.add(displayButton); 
    JPanel searchPanel = new JPanel(); 
     searchPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); 
     searchPanel.add(new JLabel ("Search target")); 
     searchPanel.add(Box.createHorizontalBox()); 
     searchPanel.add(searchField); 
     searchPanel.add(typeComboBox); 
     searchPanel.add(Box.createHorizontalBox()); 
     searchPanel.add(searchButton); 

    container.add(filePanel); 
    container.add(displayPanel); 
    container.add(searchPanel); 
    add(container, BorderLayout.WEST); 

    validate(); 
} 
+0

하나의 레이아웃 대신 여러 레이아웃을 사용하려고합니다. –

+0

저는 박스 레이아웃과 레이아웃을 가지고 있습니다. 상자 레이아웃은 행이 분리되도록 허용합니다. – leigero

+0

BoxLayout에 익숙하지 않지만이 경우 "왼쪽"컨테이너에 AbsoluteLayout을 사용했을 것입니다. 어쩌면 이것은 선택 사항 일 수 있습니다. –

답변

1

는 BoxLayout에서는 레이아웃에 상기 preferredSize가뿐만 아니라 minumimu 최대 크기를 사용한다. 귀하의 경우 패널은 사용 가능한 공간이 많아짐에 따라 기본 크기에서 최대 크기까지 커집니다. 당신이 할 수있는 일이 발생하지 않도록하려면 더 나은 솔루션이 getPreferredSize()를 반환하는 각 패널의 getMaximumSize()을 무시하는 것입니다

filePanel.setMaximumSize(filePanel.getPreferredSize()); 
... 
displayPanel.setMaximumSize(displayPanel.getPreferredSize()); 
... 
searchPanel.setMaximumSize(searchPanel.getPreferredSize()); 

있지만. 다른 LAF에서 신청서를 사용할 수있는 경우에는 각 패널의 선호 크기가 변경 될 수 있습니다.

+0

그것은 예기치 않은 바람직하지 않은 결과로 보입니다. filePanel은 '-'보다 크지 않은 작은 돌진으로 박살 났으며 searchPanel은 더 이상 한 줄에 들어 가지 않으며 줄 바꿈되는 단추는 JPanel 높이 아래에 숨겨져 있습니다. – leigero

+1

@leigero, 위 코드는 정상적으로 작동합니다. 나는 당신의 코드에서 그것을 테스트했다. 모든 구성 요소를 패널에 추가 한 후 최대 크기를 설정해야합니다. – camickr

+0

물론! 정말 고맙습니다. – leigero

0

, 당신은 빈 영역을 만들고 왼쪽 컨테이너에 추가합니다 Box.Filler를 사용할 수 있습니다. 오라클의 설명서 here의 머리글 Custom Box.Filler을 확인하십시오. 코드에서 filePanel, displayPanel, searchPanel을 추가 한 후 사용자가 지원하는 해상도보다 큰 기본 크기의 필러를 추가합니다. 아래에 필러가 추가 된 코드가 있으므로 크기를 조정할 때 콘텐츠 간격이 그대로 유지됩니다.

반면에 미그 레이아웃을 사용하고 "필기"를 사용하여 특수 필러 나 접착제없이 동일한 동작을 수행 할 수 있습니다.

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.FlowLayout; 
import javax.swing.*; 

public class TestCode extends JFrame{ 

    JTextArea textArea = new JTextArea(); 
    JComboBox typeComboBox; 
    JTextField searchField; 
    JTextField fileField; 

public TestCode() { 
    System.out.println ("In constructor"); 
    setTitle ("GUI Test"); 
    setSize (600, 300); 
    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 
    setVisible (true); 

    JScrollPane scrollPane = new JScrollPane(textArea); 
    add(scrollPane, BorderLayout.CENTER); 

    JButton readButton = new JButton("Read File"); 
    JButton displayButton = new JButton("Display"); 
    JButton searchButton = new JButton("Search"); 


    searchField = new JTextField(10); 
    fileField = new JTextField(15); 

    typeComboBox = new JComboBox(); 
    typeComboBox.addItem("Index"); 
    typeComboBox.addItem("Type"); 
    typeComboBox.addItem("Name"); 

    JPanel container = new JPanel(); 
     container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS)); 
    JPanel filePanel = new JPanel(); 
     filePanel.setLayout(new FlowLayout(FlowLayout.LEFT)); 
     filePanel.add(new JLabel("Source file:", SwingConstants.RIGHT)); 
     filePanel.add(fileField); 
     filePanel.add(readButton); 
    JPanel displayPanel = new JPanel(); 
     displayPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); 
     displayPanel.add(new JLabel("Display data:", SwingConstants.RIGHT)); 
     displayPanel.add(displayButton); 
    JPanel searchPanel = new JPanel(); 
     searchPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); 
     searchPanel.add(new JLabel ("Search target")); 
     searchPanel.add(Box.createHorizontalBox()); 
     searchPanel.add(searchField); 
     searchPanel.add(typeComboBox); 
     searchPanel.add(Box.createHorizontalBox()); 
     searchPanel.add(searchButton); 

     container.add(filePanel); 
     container.add(displayPanel); 
     container.add(searchPanel); 
     container.add(new Box.Filler(new Dimension(0,100), new Dimension(0,1000), new Dimension(0,1000))); 
     add(container, BorderLayout.WEST); 

    validate(); 
    } 

    public static void main(String[] args) { 
     TestCode code = new TestCode(); 
     code.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     code.setVisible(true); 
    } 
} 
관련 문제