2014-11-29 4 views
0

GridBagConstraints과 함께 양식을 작성하는 데 어려움이 있습니다. gridy으로 설정하면 작동하지 않는 것 같습니다. 여기 내 코드는 다음과 같습니다.Java Gridbagconstraints gridy 문제

thisWindow = new GridBagConstraints(); 
thisWindow.insets = new Insets(5, 5, 5, 5); 
thisWindow.weightx = 1.0; 
thisWindow.weighty = 1.0; 
thisWindow.gridheight = 4; 
thisWindow.anchor = GridBagConstraints.NORTHWEST; 
//set elements 
thisWindow.gridx = 0; 
thisWindow.gridy = 0; 
charScreen.add(nameLbl, thisWindow); 
thisWindow.gridx = 1; 
thisWindow.gridy = 0; 
charScreen.add(charNameCmb, thisWindow); 
thisWindow.gridx = 2; 
thisWindow.gridy = 0; 
charScreen.add(raceLbl, thisWindow); 
thisWindow.gridx = 3; 
thisWindow.gridy = 0; 
charScreen.add(raceCmb, thisWindow); 
thisWindow.gridx = 4; 
thisWindow.gridy = 0; 
charScreen.add(genderLbl, thisWindow); 
thisWindow.gridx = 5; 
thisWindow.gridy = 0; 
charScreen.add(genderCmb, thisWindow); 
//This should be on a new line. 
thisWindow.gridx = 0; 
thisWindow.gridy = 1; 
charScreen.add(levelLbl, thisWindow); 
thisWindow.gridx = 1; 
thisWindow.gridy = 1; 
charScreen.add(levelSpn, thisWindow); 
thisWindow.gridx = 2; 
thisWindow.gridy = 1; 
charScreen.add(charClassLbl, thisWindow); 
thisWindow.gridx = 3; 
thisWindow.gridy = 1; 
charScreen.add(charClassCmb, thisWindow); 
thisWindow.gridx = 4; 
thisWindow.gridy = 1; 
charScreen.add(deityLbl, thisWindow); 
thisWindow.gridx = 5; 
thisWindow.gridy = 1; 
charScreen.add(deityCmb, thisWindow); 
thisWindow.gridx = 6; 
thisWindow.gridy = 1; 
charScreen.add(homelandLbl, thisWindow); 
thisWindow.gridx = 7; 
thisWindow.gridy = 1; 
charScreen.add(homelandTxt, thisWindow); 
//This should be on a third line. 
thisWindow.gridx = 0; 
thisWindow.gridy = 2; 
charScreen.add(sizeLbl, thisWindow); 
thisWindow.gridx = 1; 
thisWindow.gridy = 2; 
charScreen.add(sizeTxt, thisWindow); 
charScreen.setVisible(true); 

나는 다양한 앵커를 사용해 보았지만 아무런 도움이되지 않았습니다. 내가 가지고있는 문제는 두 번째 줄과 세 번째 줄에 있어야하는 두 부분이 모두 첫 줄과 겹쳐져 있다는 것입니다. 이것에 대한 조언은 크게 감사드립니다.

+1

가장 좋은 방법은 우리가 완벽하고 신속하게 이해하는 것이 얻으려면 문제는 당신이 작고 그러나 완전한 프로그램 인 [최소 예제 프로그램] (http://stackoverflow.com/help/mcve)을 작성하고 게시하는 것이라면 될 것입니다 문제를 설명하는 데 필요한 코드 만 필요하므로 수정없이 복사, 붙여 넣기, 컴파일 및 실행할 수 있습니다. –

답변

3

당신의 gridHeight가 당신을 망치고 있습니다.

변경 :

thisWindow.gridheight = 4; 

내가 이것을 테스트하기 위해 내 자신의 MCVE을 만들어

thisWindow.gridheight = 1; 

하려면 :

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

public class TestCharScreen { 

    private static GridBagConstraints thisWindow; 
    private static JPanel charScreen = new JPanel(new GridBagLayout()); 
    private static JLabel nameLbl = new JLabel("nameLbl"); 
    private static JLabel charNameCmb = new JLabel("charNameCmb"); 
    private static JLabel raceLbl = new JLabel("raceLbl"); 
    private static JLabel raceCmb = new JLabel("raceCmb"); 
    private static JLabel genderLbl = new JLabel("genderLbl"); 
    private static JLabel genderCmb = new JLabel("genderCmb"); 
    private static JLabel levelLbl = new JLabel("levelLbl"); 
    private static JLabel levelSpn = new JLabel("levelSpn"); 
    private static JLabel charClassLbl = new JLabel("charClassLbl"); 
    private static JLabel charClassCmb = new JLabel("charClassCmb"); 
    private static JLabel deityLbl = new JLabel("deityLbl"); 
    private static JLabel deityCmb = new JLabel("deityCmb"); 
    private static JLabel homelandLbl = new JLabel("homelandLbl"); 
    private static JLabel homelandTxt = new JLabel("homelandTxt"); 
    private static JLabel sizeLbl = new JLabel("sizeLbl"); 
    private static JLabel sizeTxt = new JLabel("sizeTxt"); 


    public static void main(String[] args) { 


     thisWindow = new GridBagConstraints(); 
     thisWindow.insets = new Insets(5, 5, 5, 5); 
     thisWindow.weightx = 1.0; 
     thisWindow.weighty = 1.0; 

     // ***** 
     thisWindow.gridheight = 4; // 4? ***** 

     thisWindow.anchor = GridBagConstraints.NORTHWEST; 
     //set elements 
     thisWindow.gridx = 0; 
     thisWindow.gridy = 0; 
     charScreen.add(nameLbl, thisWindow); 
     thisWindow.gridx = 1; 
     thisWindow.gridy = 0; 
     charScreen.add(charNameCmb, thisWindow); 
     thisWindow.gridx = 2; 
     thisWindow.gridy = 0; 
     charScreen.add(raceLbl, thisWindow); 
     thisWindow.gridx = 3; 
     thisWindow.gridy = 0; 
     charScreen.add(raceCmb, thisWindow); 
     thisWindow.gridx = 4; 
     thisWindow.gridy = 0; 
     charScreen.add(genderLbl, thisWindow); 
     thisWindow.gridx = 5; 
     thisWindow.gridy = 0; 
     charScreen.add(genderCmb, thisWindow); 
     //This should be on a new line. 
     thisWindow.gridx = 0; 
     thisWindow.gridy = 1; 
     charScreen.add(levelLbl , thisWindow); 
     thisWindow.gridx = 1; 
     thisWindow.gridy = 1; 
     charScreen.add(levelSpn, thisWindow); 
     thisWindow.gridx = 2; 
     thisWindow.gridy = 1; 
     charScreen.add(charClassLbl, thisWindow); 
     thisWindow.gridx = 3; 
     thisWindow.gridy = 1; 
     charScreen.add(charClassCmb, thisWindow); 
     thisWindow.gridx = 4; 
     thisWindow.gridy = 1; 
     charScreen.add(deityLbl, thisWindow); 
     thisWindow.gridx = 5; 
     thisWindow.gridy = 1; 
     charScreen.add(deityCmb, thisWindow); 
     thisWindow.gridx = 6; 
     thisWindow.gridy = 1; 
     charScreen.add(homelandLbl, thisWindow); 
     thisWindow.gridx = 7; 
     thisWindow.gridy = 1; 
     charScreen.add(homelandTxt, thisWindow); 
     //This should be on a third line. 
     thisWindow.gridx = 0; 
     thisWindow.gridy = 2; 
     charScreen.add(sizeLbl, thisWindow); 
     thisWindow.gridx = 1; 
     thisWindow.gridy = 2; 
     charScreen.add(sizeTxt, thisWindow); 

     charScreen.setBorder(BorderFactory.createTitledBorder("charScreen")); 
     // charScreen.setVisible(true); 

     JOptionPane.showMessageDialog(nameLbl, charScreen); 
    } 
} 
+0

Brilliant! 호버 크래프트에 감사드립니다! That works works : – Ryan

+1

@ Ryan : 이것을 테스트하기 위해 MCVE를 만들었습니다. 위의 코드를 참조하십시오. 앞으로 이와 같은 게시를 고려하십시오. JLabel을 사용했고 다른 구성 요소는 사용하지 않았다는 점에 유의하십시오. 문제를 설명하는 데 필요한 모든 것이었기 때문입니다. –

+0

@ Ryan 행복하다면 답을 받아 들여야 함을 기억하십시오. – QGA