繁体   English   中英

使用GridBagLayout设置坐标

[英]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);
}
}

和图片: http : //img14.imageshack.us/img14/7243/oknouw.jpg

anchorfill如果您指定唯一的工作weighx和/或weighty与大于0的值。

现在的问题是如何分配额外的空间。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM