2017-01-29 25 views
0

我遇到了GridBagLayout()問題,並且列寬甚至相等。實質上,我使用GridBagLayout lahyout經理將三個按鈕添加到面板。我期望(想)按鈕的寬度是相同的,並填充它們所在的單元格。但是,由於按鈕的大小不同(取決於它們中的文本),我最終得到不同大小的按鈕。下面是代碼:使用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); 
     }  
    } 
} 

的「紅桃A」是比「鑽石2」尺寸和「俱樂部3」是明顯的變小一點不同。我希望他們都是相同的大小。任何幫助將不勝感激,謝謝!

+1

使用網格佈局 – Aubin

+1

的GridBagLayout將*從未*使所有列的寬度相同。 GridBagLayout單元格靈活;他們的大小總是取決於他們的內容。沒有GridBagConstraints值可以改變這種行爲。正如Aubin所說,使用GridLayout而不是GridBagLayout。或者,如果您想要挑戰,請使用SpringLayout。 – VGR

+0

按鈕數量變化 – Billy

回答

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; 
    } 
} 
+0

謝謝你。我最終只是用GridLayout替換了GridBagLayout。 – Billy