为什么GridBagLayout提供奇怪的结果

为什么GridBagLayout提供奇怪的结果

我希望各种组件散布开来并填满整个窗口。

你有尝试过其他吗?是的,我尝试了GridLayout,但是按钮看上去很大。我还尝试了pack()来缩小窗口。窗口应为750x750 :)

我正在尝试的是这样的:
 顶部的这4个按钮是细条
 里面带有JPanels的滚动窗格将包含所有视频转换任务。这会占用最大空间
 底部的JPanel包含JProgressBar细条。

但是某处似乎有些混乱。请帮我解决这个问题

 社会科学中心

import java.awt.*;
import javax.swing.*;
import com.explodingpixels.macwidgets.*;

public class HudTest {
    public static void main(String[] args) {
        HudWindow hud = new HudWindow("Window");
        hud.getJDialog().setSize(750, 750);
        hud.getJDialog().setLocationRelativeTo(null);
        hud.getJDialog().setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel buttonPanel = new JPanel(new FlowLayout());


        JButton addVideo = HudWidgetFactory.createHudButton("Add New Video");
        JButton removeVideo = HudWidgetFactory.createHudButton("Remove Video");
        JButton startAll = HudWidgetFactory.createHudButton("Start All Tasks");
        JButton stopAll = HudWidgetFactory.createHudButton("Stop All Tasks");

        buttonPanel.add(addVideo);
        buttonPanel.add(startAll);
        buttonPanel.add(removeVideo);
        buttonPanel.add(stopAll);

        JPanel taskPanel = new JPanel(new GridLayout(0,1));
        JScrollPane taskScrollPane = new JScrollPane(taskPanel);
        IAppWidgetFactory.makeIAppScrollPane(taskScrollPane);
        for(int i=0;i<10;i++){
            ColorPanel c = new ColorPanel();
            c.setPreferredSize(new Dimension(750,100));
            taskPanel.add(c);
        }

        JPanel progressBarPanel = new JPanel();

        JComponent component = (JComponent) hud.getContentPane();
        component.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        Insets in = new Insets(2,2,2,2);

        gbc.insets = in;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 10;
        gbc.gridheight = 1;
        gbc.fill = GridBagConstraints.BOTH;
        component.add(buttonPanel,gbc);

        gbc.gridy += 1;
        gbc.gridheight = 17;
        component.add(taskScrollPane,gbc);

        gbc.gridy += 17;
        gbc.gridheight = 2;
        component.add(progressBarPanel,gbc);

        hud.getJDialog().setVisible(true);
    }
}

最佳答案

用这个

gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridbagConstraints.BOTH

关于java - 为什么GridBagLayout提供奇怪的结果?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16607724/

10-09 15:18