2011-07-31 74 views
2

我有一個面板,其中我已指定網格佈局佈局。我基本上有1列和4行。標籤默認與中心對齊。我如何將它們對齊到左邊?將JLabel與網格包佈局中的面板左側對齊

private void addLabel(String name, int gridx, int gridy, int anchor){ 
      gbc.gridx=gridx; 
      gbc.gridy=gridy; 
      JLabel label=new JLabel(name); 
      gbc.fill=GridBagConstraints.HORIZONTAL; 
      gbag.setConstraints(label, gbc); 
      panel1.add(label); 

主叫線路有:

gbc=new GridBagConstraints(); 
     gbag=new GridBagLayout(); 
panlel1.setLayout(gbag); 
addLabel(" Exemption type", 0, 0,anchor); 
+0

此外設置gbc.anchor = GridBagConstraints.WEST爲我工作... – m13r

回答

1

嘗試

JLabel myLabel = new JLabel("Fubar", SwingConstants.LEFT); 

或者,你可以通過調用myLabel.setHorizontalAlignment(SwingConstants.LEFT);

編輯做一個已創建的JLabel相同:例如:

import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 

import javax.swing.*; 

public class GridBagExample { 
    private static void createAndShowUI() { 
     String[] data = {"one", "two", "three", "four"}; 

     JPanel panel = new JPanel(new GridBagLayout()); 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     gbc.gridwidth = 1; 
     gbc.gridheight = 1; 
     gbc.anchor = GridBagConstraints.WEST; 
     gbc.fill = GridBagConstraints.BOTH; 
     gbc.weightx = 1.0; // **** comment this line out to see effect **** 
     gbc.weighty = 1.0; // **** comment this line out to see effect **** 

     for (int i = 0; i < data.length; i++) { 
     JLabel label = new JLabel(data[i]); 
     gbc.gridy = i; 
     panel.add(label, gbc); 
     } 

     JFrame frame = new JFrame("GridBagExample"); 
     frame.getContentPane().add(panel); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowUI(); 
     } 
     }); 
    } 
} 
+0

我沒有工作。我認爲它必須使用Gridbag佈局 –

+0

@Kaushik:如果它不起作用,那麼我們需要更多的信息。考慮創建併發佈一個證明問題的最小可編譯代碼示例,一個[SSCCE](http://SSCCE.org)(檢查鏈接)。 –

+0

@Kaushik:另外,你是否正確設置了GridBagConstraints.fill,anchor和GridBagConstraints.weightx? –

0

閱讀API。我相信該方法是

setHorizontalAlignment(...); 
+0

這樣做不能在GridBagLayout容器中工作 – m13r

0

這是我如何解決這個問題。 最重要的部分是

gbc.anchor = GridBagConstraints.WEST; 

上面是叫你指定你想要的佈局單元格和你之前權組件添加到面板的後面。

的GridBagConstraints

當組件小於其顯示 面積小此字段用於。它確定在顯示區域內放置 組件的位置。 ...

下面是我如何實現它。

public class ControlPanel extends JPanel{...  
     ControlPanel(){ 
      this.setLayout(new GridBagLayout()); 
      //create components 
      ... 

      GridBagConstraints gbc = new GridBagConstraints(); 

      //space components out a little 
      gbc.insets = new Insets(5,5,5,5); 

      gbc.gridx = 0; 
      gbc.gridy = 0; 
      this.add(button_btn,gbc); 

      gbc.gridx = 1; 
      gbc.gridy = 0; 
      this.add(spinner1_pnl,gbc); 

      gbc.gridx = 2; 
      gbc.gridy = 0; 
      this.add(spinner2_pnl,gbc); 

      gbc.gridx = 3; 
      gbc.gridy = 0; 
      this.add(checkbox1_chk,gbc); 

      gbc.gridx = 4; 
      gbc.gridy = 0; 
      this.add(checkbox2_chk,gbc); 

      gbc.gridx = 0; 
      gbc.gridy = 1; 
      gbc.gridwidth = 3; //span 3 columns 
      gbc.anchor = GridBagConstraints.WEST; 
      this.add(results_lbl,gbc); 
     } 
    } 

在這種情況下,我把下面的一些其他組件的標籤,但最重要的是標籤左(西)其細胞內對齊。