2012-07-31 1 views
1

for 루프 내에서 gridbaglayout을 사용하여 고정 크기의 정적 uneditable 그리드를 배치하고 싶습니다. 그러나 그렇게 할 때 나는 아무것도 얻지 못합니다. 반복적으로 gridbaglayout을 사용하여 구성 요소를 추가 하시겠습니까?

import java.awt.Color; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 

import javax.swing.BorderFactory; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

class ViewManager extends JFrame{ 

private static final long serialVersionUID = 1L; 
private NewCharacterVitalsPane myNewCharacterVitalsPane = null; 

public ViewManager(){ 
    super("Tony Larp DB Manager"); 
    this.setVisible(true); 
    this.setDefaultCloseOperation(3); 
    myNewCharacterVitalsPane = new NewCharacterVitalsPane(); 
    this.getContentPane().add(myNewCharacterVitalsPane); 
    this.pack();   
} 
static class Util { 
    static private ViewManager viewManager = null; 
    static public synchronized ViewManager getInstance() { 
     if (viewManager == null) { 
      viewManager = new ViewManager(); 
     } 
     return viewManager; 
    } 
} 
} 

public class Launcher { 

public static void main(String[] args) 
{ 
    SwingUtilities.invokeLater(new Runnable(){ 
     public void run(){ 
      ViewManager.Util.getInstance(); 
     } 
    }); 
} 
} 

class NewCharacterVitalsPane extends JPanel{ 

private static final long serialVersionUID = 1962802485168533544L; 
String locations[] = {"H","Ch","Ra","La","Rl","Ll"}; 
int armour[] = {0,0,0,0,10,0}; 
int life = 30; 

/*subPanes*/ 
JPanel battleBoardPane = new JPanel(); 


public NewCharacterVitalsPane(){ 
    addSubPanes(); 
    drawBattleBoard(); 
} 
private void addSubPanes() { 
    this.setLayout(new GridBagLayout()); 
    GridBagConstraints SPgbc = new GridBagConstraints(); 
    SPgbc.fill = GridBagConstraints.BOTH; 

    SPgbc.gridx = 0; 
    SPgbc.gridy = 1; 
    SPgbc.gridheight = 2; 
    SPgbc.ipadx = 100; 
    SPgbc.ipady = 180; 
    battleBoardPane.setBorder(BorderFactory.createLineBorder(Color.black)); 
    add(battleBoardPane,SPgbc); 
} 

private void drawBattleBoard(){ 
    this.setLayout(new GridBagLayout()); 
    GridBagConstraints BBgbc = new GridBagConstraints(); 
    BBgbc.gridx=0;BBgbc.gridy=0; 
    for (String location:locations){ 
     JLabel locLabel = new JLabel(location+": "); 
     BBgbc.gridy++; battleBoardPane.add(locLabel,BBgbc); 
    } 
} 
} 

은 현재 단지, 행의 모든 ​​레이블을 표시하는 다른 후하지 하나를 여기에 SSCEE로 내 첫 번째 시도이다. 왜 그런가요? 내가 원하는 행동을 얻으려면 어떻게해야합니까?

답변

1

첫 번째 대답은 잘하지만, 당신이 잘못된 코드 업데이트를 확인합니다. 내 코드를 업데이트합니다하지만

대신 this.setLayout(new GridBagLayout());

drawBattleBoard() 방법의 첫 번째 행에서

당신은

battleBoardPane.setLayout(new GridBagLayout());

+0

이것은 작동하는 것 같습니다! – Pureferret

2

battleBoardPane의 Layoutmanager를 GridbagLayout으로 설정하십시오.
이 (기본값은 FlowLayout 그것을하지 않습니다 "inherrit"그것은 내가 아는 포함하는 창에서.)

+0

이 작동하지 않는 작성해야합니다. – Pureferret