2012-10-06 3 views
3

GUI에 GridBagLayout을 사용하고 있으며 크기가 제대로 조정되지 않는 문제가 있습니다. 누구든지 대체 레이아웃을 제안하기 전에 GridBagLayout을 사용하고자합니다. 나는 그것을위한 많은 수의 컴포넌트를 가지게 될 것이고, 그것들을 적절하게 레이아웃하고 싶습니다, 현재 코드/이미지는 현재 가지고있는 것들만 표시합니다.패널에서 Java의 GridBagLayout으로 크기가 올바르게 조정되지 않았습니다.

아무튼 문제는 자체 크기가 적절하게 조정되지 않는다는 것입니다. 크기를 조정할 때 현재의 가로 세로 비율을 유지하고 싶습니다. 어떤 패널에는 크기 조정시 우선 순위가 주어 지므로 크기를 조정할 때 종횡비가 흐트러집니다. 기본적으로, 나는 다른 것보다 더 큰 것을 같은 일부 패널을 가지고 있지만, 크기를 조정 한 후, 그렇게 같은, 다른 것보다 작아 질 : 그들이 이미 제시된 비율을 유지하는

http://i.imgur.com/LWhHm.png

지금, 나는 실제로 원하는 것입니다. 보시다시피 원래 GUI처럼 왼쪽 패널 중 하나의 GUI보다 채팅 부분이 작기를 바랍니다. 내가에있어 동안

jpPack = new JPanel(); 
    jpCards = new JPanel(); 
    jpInfo = new JPanel(); 
    jpChat = new JPanel(); 


    jpPack.setBackground(Color.red); 
    jpCards.setBackground(Color.blue); 
    jpInfo.setBackground(Color.green); 

    //create the Constraints and set some that will apply to each panel 
    GridBagConstraints c = new GridBagConstraints(); 
    c.fill = GridBagConstraints.BOTH; 

    //set the number of columns/rows to use 
    c.gridwidth = 1; 
    c.gridheight = 1; 
    //set the x and y position 
    c.gridx = 0; 
    c.gridy = 0; 
    //set the weight properties 
    c.weightx = 1.0; 
    c.weighty = 1.0; 

    getContentPane().add(jpCards, c); 

    //set the number of columns/rows to use 
    c.gridwidth = 1; 
    c.gridheight = 1; 
    //set the x and y position 
    c.gridx = 0; 
    c.gridy = 1; 
    //set the weight properties 
    c.weightx = 1.0; 
    c.weighty = 1.0; 

    getContentPane().add(jpPack, c); 

    //set the number of columns/rows to use 
    c.gridwidth = 1; 
    c.gridheight = 1; 
    //set the x and y position 
    c.gridx = 1; 
    c.gridy = 0; 
    //set the weight properties 
    c.weightx = 0.2; 
    c.weighty = 0.2; 

    getContentPane().add(jpInfo, c); 

    //set the number of columns/rows to use 
    c.gridwidth = 1; 
    c.gridheight = 1; 
    //set the x and y position 
    c.gridx = 1; 
    c.gridy = 1; 
    //set the weight properties 
    c.weightx = 0.2; 
    c.weighty = 0.2; 

    getContentPane().add(jpChat, c); 
    jpChat.setLayout(new GridLayout()); 
    jpChat.add(client.gui.getContentPane(), c); 

    setVisible(true); 

가, 나뿐만 아니라 내가 가지고있는 다른 질문을 할 수 있습니다 : 아래

내가이 생산하는 지금 가지고있는 코드입니다. GUI 전체와 채팅 패널에 최소 크기 제한을 설정하고 싶습니다. 먼저 사용자가 특정 x 및 특정 y보다 작게 크기를 조정할 수 없도록하려고합니다. 후자의 경우 필자가 원하는 가로 세로 비율을 유지하고 두 오른쪽 패널의 최소값을 기록한 후 왼쪽 패널의 크기를 조정하기를 원합니다. 즉, 크기가 될 때까지 각 패널의 크기를 조정 한 다음 전체 GUI의 크기를 계속 조정하지만 왼쪽 패널의 크기를 조정하여 오른쪽 패널을 최소 크기로 유지합니다.

나를 도와 주셔서 감사합니다. 이것은 실제로 나를 괴롭 히고 있습니다, 나는 이것을 시도하고 GridBagLayoutConstraints를 어지럽히고 아무데도 가지 않는데 몇 시간을 보냈습니다. 이것이 내가 얻은 최고입니다.

+0

동일한 인스턴스를 재사용하지 않고 하나 이상의 'GridBagConstraints' 객체를 사용 해본 적이 있습니까? – gobernador

+0

@gobernador'GridBagLayout'에 대한 자바 튜토리얼은 제약 조건을 재사용합니다. 이전 사용에서 잘못 설정하지 않으면 모든 것이 잘되어야합니다. –

+2

당신 둘다. 하지만 상황이 잘못 될 때 제약 조건을 다시 설정하면 문제가 발생할 수 있습니다. – MadProgrammer

답변

3

귀하의 질문에 약간의 노력을 기울였습니다. 간단한 이유가있어서 그로 인해 걱정이 될 수 있습니다. ChatPanel에서 사용중인 JTextField을 초기화하는 동안 해당 열을 지정해야합니다. 그렇게하면 이러한 효과가 발생할 수 있습니다. 위 코드를 작성한 이래로, 어떤 의미에서는 이상한 행동을하지 않습니다. 두 번째 질문과 관련하여, @ kleopatra는 약간의 통찰력을주었습니다. 다음은 내가 사용한 코드입니다 :

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

public class ClientGrid 
{ 
    private void displayGUI() 
    { 
     JFrame frame = new JFrame("Client Grid"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JPanel contentPane = new JPanel(); 
     contentPane.setLayout(new GridBagLayout()); 

     JPanel bluePanel = new JPanel(); 
     bluePanel.setOpaque(true); 
     bluePanel.setBackground(Color.BLUE); 

     JPanel greenPanel = new JPanel(); 
     greenPanel.setOpaque(true); 
     greenPanel.setBackground(Color.GREEN); 

     JPanel redPanel = new JPanel(); 
     redPanel.setOpaque(true); 
     redPanel.setBackground(Color.RED); 

     JPanel chatPanel = new JPanel(); 
     JTextField chatField = new JTextField(); 
     chatPanel.setLayout(new BorderLayout(5, 5)); 
     chatPanel.add(chatField, BorderLayout.PAGE_START); 

     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.anchor = GridBagConstraints.FIRST_LINE_START; 
     gbc.fill = GridBagConstraints.BOTH; 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     gbc.weightx = 0.7; 
     gbc.weighty = 0.3; 

     contentPane.add(bluePanel, gbc); 

     gbc.gridx = 1; 
     gbc.weightx = 0.3; 

     contentPane.add(greenPanel, gbc); 

     gbc.gridx = 0; 
     gbc.gridy = 1; 
     gbc.weightx = 0.7; 
     gbc.weighty = 0.7; 

     contentPane.add(redPanel, gbc); 

     gbc.gridx = 1; 
     gbc.weightx = 0.3; 

     contentPane.add(chatPanel, gbc); 

     frame.setContentPane(contentPane); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String... args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       new ClientGrid().displayGUI(); 
      } 
     }); 
    } 
}