簡體   English   中英

使用GridBagLayout布局管理器無法獲得等寬的列

[英]Can't get equal width columns with GridBagLayout layout manager

我在GridBagLayout()上遇到問題,並且列寬均勻。 本質上,我將使用GridBagLayout布局管理器向面板添加三個按鈕。 我希望(希望)按鈕的寬度相同,並填充它們所在的單元格。但是,由於按鈕的大小不同(取決於它們中的文本),所以我最終得到了不同大小的按鈕。 下面是代碼:

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個”的大小明顯較小。 我希望它們大小相等。 任何幫助將不勝感激,謝謝!

這是我在評論中談論的方法的示例。 這應該可以幫助您將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;
    }
}

有點黑...將所有組件的首選大小設置為一些愚蠢的小尺寸,例如:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM