简体   繁体   中英

Adding components with gridbaglayout iteratively?

I'd like to lay out static uneditable grid of fixed size, using gridbaglayout inside a for loop. When I do so however, I get nothing. Here is my first attempt as an 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);
    }
}
}

Currently it just displays all the labels in a row, not one after the other. Why is this, how can I get the behaviour I want?

Set the Layoutmanager of battleBoardPane to GridbagLayout .
(The default is FlowLayout it doesn't "inherrit" it from the containing pane as far as I know.)

The first answer is right, but you make wrong code update.

Instead of this.setLayout(new GridBagLayout());

at first row of drawBattleBoard() method

you must write

battleBoardPane.setLayout(new GridBagLayout());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM