2013-01-22 43 views
0

我對座標x和y有問題。我使用GridBagLayout gridx = 0,gridy = 0,但所有組件都對齊全中心。我需要設置對齊左上角0,0,但我的解決方案使用GridBagLayout不起作用。使用GridBagLayout設置座標

現在我顯示代碼和圖片。需要對齊左側。默認有中心對齊,但需要爲組件設置左上對齊,因爲我需要繪製兩個圖片底部的JFrame。

import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 
import javax.swing.*; 
public class Zobrazenie extends JPanel { 
private static final long serialVersionUID = 1L; 
    JButton b1,b2,b3,b4,b5; 
    GridBagConstraints gbc = new GridBagConstraints(); 
    Zobrazenie() 
    { 
     setLayout(new GridBagLayout()); 
     gbc.insets=new Insets(10,10,10,10); 

     gbc.gridx=0; 
     gbc.gridy=0; 
     gbc.anchor=GridBagConstraints.WEST; 
     b1=new JButton("Button 1"); 
     add(b1,gbc); 

     gbc.gridx=0; 
     gbc.gridy=1; 
     b2=new JButton("Button 2"); 
     add(b2,gbc); 

     gbc.gridx=0; 
     gbc.gridy=2; 
     b3=new JButton("Button 3"); 
     add(b3,gbc); 

     gbc.gridx=0; 
     gbc.gridy=3; 
     b4=new JButton("Button 4"); 
     add(b4,gbc); 

     gbc.gridx=0; 
     gbc.gridy=4; 
     b5=new JButton("Button 5"); 
     add(b5,gbc); 
    } 
public static void main(String[] args) 
{ 
    JFrame jf = new JFrame(); 
    Zobrazenie zb = new Zobrazenie(); 
    jf.setSize(400,300); 
    jf.setTitle("Okno"); 
    jf.setLocationRelativeTo(null); 
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    jf.add(zb); 
    jf.setVisible(true); 
} 
} 

而且圖片:http://img14.imageshack.us/img14/7243/oknouw.jpg

+0

歡迎來到StackOverflow!作爲一個經驗法則,當您提供迄今爲止嘗試過的代碼時,社區總是會更容易幫助您。 –

回答

1

anchorfill只有在指定weighx和/或weighty與工作價值大於0

現在的問題是你怎麼想額外的空間分發。