[英]Set Coordinates using GridBagLayout
我的坐標x和y有問題。 我使用GridBagLayout
gridx = 0,gridy = 0,但是所有組件都在整個中心對齊。 我需要將align設置為左上角0,0,但是使用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);
}
}
anchor
和fill
如果您指定唯一的工作weighx
和/或weighty
與大於0的值。
現在的問題是如何分配額外的空間。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.