1

forループ内でgridbaglayoutを使用して、固定サイズの静的な編集不可能なグリッドをレイアウトしたいと思います。しかし、そうしても何も得られません。これがSSCEEとしての私の最初の試みです:

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);
    }
}
}

現在、すべてのラベルが次々に表示されるのではなく、行に表示されるだけです。これはなぜですか、どうすれば私が望む動作を得ることができますか?

4

2 に答える 2

2

のレイアウトマネージャーをに設定battleBoardPaneGridbagLayoutます。
(デフォルトではFlowLayout、私が知る限り、包含ペインから「継承」しません。)

于 2012-07-31T11:30:59.723 に答える
1

最初の答えは正しいですが、間違ったコード更新を行います。

それ以外のthis.setLayout(new GridBagLayout());

drawBattleBoard()メソッドの最初の行

あなたは書く必要があります

battleBoardPane.setLayout(new GridBagLayout());

于 2012-08-01T09:41:46.520 に答える