gpt4 book ai didi

java - 为什么 GridBagLayout 会提供奇怪的结果?

转载 作者:行者123 更新时间:2023-11-29 03:33:44 24 4
gpt4 key购买 nike

enter image description here

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

您尝试过其他方法吗? 是的,我尝试过 GridLayout,但按钮看起来很大。我还尝试了 pack() ,它使窗口变小了。窗口应为 750x750 :)

我正在尝试的是:

  • 这4个按钮在上面呈细条状
  • 带有 JPanels 的滚动 Pane 将包含所有视频转换任务。这样占用空间最大
  • 底部的 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/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com