使用GridBagLayout布局管理器无法获得相等宽度的列

时间:2017-01-29 17:17:12

标签: java gridbaglayout

我遇到GridBagLayout()的问题,甚至列宽。基本上,我正在使用GridBagLayout lahyout管理器向面板添加三个按钮。我希望(想要)按钮的宽度相同,并填充它们所在的单元格。但是,因为按钮大小不同(取决于它们中的文本),我最终得到了不同大小的按钮。以下是代码:

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;


public class ShitHeadGUI extends JPanel {

    public String[] s = new String[] {"Ace of Hearts", "2 of Diamonds", "3 of Clubs"};


    //JPanel pile = new JPanel();
    List<JPanel> hands = new ArrayList<JPanel>();
    GridBagConstraints cons = new GridBagConstraints();
    JFrame frame = new JFrame("Shithead");

    public ShitHeadGUI() {
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        this.setPreferredSize(new Dimension(300,300));

        cons.gridx = 0;
        cons.gridy = 0;
        cons.weightx = 1;
        cons.fill = GridBagConstraints.HORIZONTAL;

        for (int i = 0; i < 2; i++) {
            hands.add(new JPanel());
            hands.get(i).setLayout(new GridBagLayout());
            this.add(hands.get(i));
            cons.gridy++;
        }

        frame.setVisible(true);
        frame.add(this);
        frame.pack();   

        addCards(null);
    }

    public void addCards(Hand h) {

        List<JButton> btnCard = new ArrayList<JButton>();

        for (int i = 0; i < 3; i++) {//h.size(); i++) {
            cons.gridx = i;
            //btnCard.add(new JButton(h.getCard(i).toString()));
            btnCard.add(new JButton(s[i]));
            hands.get(0).add(btnCard.get(i), cons);
        }       
    }
}

“心之王”与“钻石2”大小略有不同,“3个俱乐部”明显更小。我希望他们都大小相同。任何帮助将不胜感激,谢谢!!

2 个答案:

答案 0 :(得分:1)

以下是我在评论中讨论的方法示例。这应该可以帮助您使用GridBagLayout与完全相同长度的按钮。

您可以解决的问题是,如果长度的差异是奇数,则文本永远不会完全居中。那么右边总会得到比左边更多的空格......

public class StringExtender {

    static final int maxCardNameLength = "Queen of Diamonds".length();


    public static void main (String args[]) {

        System.out.println(StringExtender.extend("2 of Spades").length());
        System.out.println(StringExtender.extend("Jack of Clubs").length());
        System.out.println(StringExtender.extend("King of Diamonds").length());
        System.out.println(StringExtender.extend("Queen of Diamonds").length());

    }

    // 
    static String extend(String cardName) {

        int diff = maxCardNameLength - cardName.length();
        int diffHalf = 0;
        String result = cardName;

        // is the String shorter than maximum?
        if(diff > 0) {
            // is the difference greater than 1?
            // if true, then always pad left and right
            if(diff > 1 ){
                diffHalf = diff/2;

                // add padding left
                String paddingLeft = "%" + (result.length() + diffHalf) + "s";
                result = String.format(paddingLeft, result);
                System.out.println("Result: |" + result + "|");
            }

            // add padding right
            String paddingRight = "%-" + (maxCardNameLength) + "s";
            result = String.format(paddingRight, result);
            System.out.println("Result: |" + result + "|");
        }

        return result;
    }
}

答案 1 :(得分:1)

有一个小技巧……将所有组件的首选大小设置为一些愚蠢的小尺寸,例如:

setPreferredSize(new Dimension(2, 2));

并添加以下内容:

c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0; // fill all available space horizontally
c.weighty = 1.0; // fill all available space vertically