Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java GridBagLayout控制Swing中的对齐_Java_Swing_Gridbaglayout - Fatal编程技术网

Java GridBagLayout控制Swing中的对齐

Java GridBagLayout控制Swing中的对齐,java,swing,gridbaglayout,Java,Swing,Gridbaglayout,我有一个几乎到处都是JScrollPane的窗口。我正在向它添加大按钮(带有图像),我希望它们继续向下添加,这就是为什么我使用GridBagLayout,每次添加按钮时我都设置gbc.gridx,gbc.gridy 滚动功能工作正常: 但问题是,当我添加少于4个按钮时,它们会对齐到行的中心,而不是我想要的左侧。像这样 你知道我怎样才能把它们对准左边吗?我试过gbc.anchor和gbc.fill,但运气不好。谢谢 这是我的JScrollPane private JPanel imagesPa

我有一个几乎到处都是
JScrollPane
的窗口。我正在向它添加大按钮(带有图像),我希望它们继续向下添加,这就是为什么我使用
GridBagLayout
,每次添加按钮时我都设置
gbc.gridx,gbc.gridy

滚动功能工作正常:

但问题是,当我添加少于4个按钮时,它们会对齐到行的中心,而不是我想要的左侧。像这样

你知道我怎样才能把它们对准左边吗?我试过
gbc.anchor
gbc.fill
,但运气不好。谢谢

这是我的
JScrollPane

private JPanel imagesPanel() {
        JPanel panel = new JPanel(new GridBagLayout());

        int x = 0;
        int y = 0;

        GridBagConstraints c = new GridBagConstraints();
        //c.anchor = GridBagConstraints.NORTHWEST;

        //add all images as buttons
        List<PuzzleImage> imageList = _imageLoader.getImageCollection().getAllImages();
        for(PuzzleImage puzzleImage : imageList) {
            for(int i = 0; i < 1; i++) { //ignore this, just for debugging. i add 3 images
                ImageIcon imageIcon = new ImageIcon(puzzleImage.getLowScaleImage());

                c.gridx = x;
                c.gridy = y;
                c.weightx = 1.0;

                JButton btn = new JButton(imageIcon);
                btn.setPreferredSize(new Dimension(ImageLoader.LOW_SCALE_IMAGE_SIZE, ImageLoader.LOW_SCALE_IMAGE_SIZE));
                panel.add(btn, c);

                x++;
                if(x>=4) {
                    x = 0;
                    y++;
                }
            }
        }

        return panel;
    }
private JPanel imagesPanel(){
JPanel panel=newjpanel(newgridbagloayout());
int x=0;
int y=0;
GridBagConstraints c=新的GridBagConstraints();
//c、 锚点=GridBagConstraints.NORTHWEST;
//将所有图像添加为按钮
List imageList=_imageLoader.getImageCollection().getAllImages();
用于(拼图图像拼图图像:图像列表){
对于(inti=0;i<1;i++){//忽略这个,只是为了调试
ImageIcon ImageIcon=新的ImageIcon(puzzImage.getLowScaleImage());
c、 gridx=x;
c、 gridy=y;
c、 权重x=1.0;
JButton btn=新JButton(图像图标);
btn.setPreferredSize(新维度(ImageLoader.LOW_SCALE_IMAGE_SIZE,ImageLoader.LOW_SCALE_IMAGE_SIZE));
面板。添加(btn,c);
x++;
如果(x>=4){
x=0;
y++;
}
}
}
返回面板;
}

没有您的代码很难给出准确的答案,但这里有一个可能的解决方案:

您可以将包含所有图片的JPanel放入另一个JPanel中,使其固定在左侧,如下所示:

JPanel pictures = /* already defined */;
JPanel container = new JPanel(new BorderLayout());

container.add(pictures, BorderLayout.WEST);
//add container to your scroll container
但问题是,当我添加少于4个按钮时,它们会对齐到行的中心,而不是我想要的左侧

至少一个组件需要具有大于0的
weightx
约束

阅读上的Swing教程中的部分。解释约束如何工作的部分将提供更多信息

你为什么还要使用
GridBagLayout
?在您的最后一个问题中,我为您提供了一个简单的解决方案:。然后你甚至不需要担心约束

或者您可以使用
GridLayout
您只需定义您想要4列和组件自动换行。您可能需要嵌套面板,以便根据框架的大小,组件不会收缩/增长

基本代码是:

JPanel wrapper = new JPanel();
JPanel buttons = new JPanel( new GridLayout(0, 4) );

for (...)
{
    JButton button = new JButton(...);
    buttons.add( button );
}

frame.add(wrapper);

这个布局是关于一个不同的案例。此外,它不会将图像添加到右侧,仅添加到底部。我想在两个方向都添加图像建议不要使用FlowLayout。我猜您没有阅读提供给布局管理器使用的链接<代码>它不会将图像添加到右侧,只会添加到底部。-不知道这意味着什么。组件被添加到最上面一行,然后添加到右边,当行满时,组件被包装,这正是您的代码现在正在做的,除了您的硬代码和逻辑。