Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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提供奇怪的结果?_Java_Swing_Jpanel_Grid Layout_Gridbaglayout - Fatal编程技术网

Java 为什么GridBagLayout提供奇怪的结果?

Java 为什么GridBagLayout提供奇怪的结果?,java,swing,jpanel,grid-layout,gridbaglayout,Java,Swing,Jpanel,Grid Layout,Gridbaglayout,我希望各种组件展开并填满整个窗口 你还试过别的吗?是的,我试过GridLayout但是按钮看起来很大。我还尝试了pack(),这使窗口变小了。窗口应为750x750:) 我尝试的是: 顶部的这4个按钮是一条细条 包含JPanels的滚动窗格,其中将包含所有视频转换任务。这占用了最大的空间 底部的JPanel,包含一条JProgressBar薄条 但似乎有什么地方出了问题请帮我解决这个问题 SSCCE import java.awt.*; 导入javax.swing.*; 导入com.explod

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

你还试过别的吗?是的,我试过
GridLayout
但是按钮看起来很大。我还尝试了
pack()
,这使窗口变小了。窗口应为750x750:)

我尝试的是:
  • 顶部的这4个按钮是一条细条
  • 包含
    JPanels
    的滚动窗格,其中将包含所有视频转换任务。这占用了最大的空间
  • 底部的
    JPanel
    ,包含一条
    JProgressBar
    薄条

    但似乎有什么地方出了问题请帮我解决这个问题

    SSCCE
    import java.awt.*;
    导入javax.swing.*;
    导入com.explodingpixels.macwidgets.*;
    公开课考试{
    公共静态void main(字符串[]args){
    hud窗口hud=新的hud窗口(“窗口”);
    getJDialog().setSize(750750);
    getJDialog().setLocationRelativeTo(null);
    getJDialog().setDefaultCloseOperation(JFrame.DISPOSE\u ON\u CLOSE);
    JPanel buttonPanel=newjpanel(newflowlayout());
    JButton addVideo=HudWidgetFactory.createHudButton(“添加新视频”);
    JButton removeVideo=HudWidgetFactory.createHudButton(“删除视频”);
    JButton startAll=HudWidgetFactory.createHudButton(“启动所有任务”);
    JButton stopAll=HudWidgetFactory.createHudButton(“停止所有任务”);
    按钮面板。添加(添加视频);
    按钮面板。添加(startAll);
    按钮面板。添加(删除视频);
    按钮面板。添加(停止全部);
    JPanel taskPanel=newjpanel(newgridlayout(0,1));
    JScrollPane taskScrollPane=新的JScrollPane(taskPanel);
    makeIAppScrollPane(taskScrollPane);
    对于(int i=0;i使用此

    gbc.weightx = 1;
    gbc.weighty = 1;
    gbc.fill = GridbagConstraints.BOTH
    
    用这个

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

    为什么不简单地将三个
    JPanel
    放在一个
    JPanel
    上,使用
    BorderLayout
    作为
    Layout Manager
    ,其中中间的
    JPanel
    和所有自定义面板及其各自的大小可以容纳在
    JScrollPane
    中,如下示例所示:

    import javax.swing.*;
    import java.awt.*;
    import java.util.Random;
    
    /**
     * Created with IntelliJ IDEA.
     * User: Gagandeep Bali
     * Date: 5/17/13
     * Time: 6:09 PM
     * To change this template use File | Settings | File Templates.
     */
    public class PlayerBase
    {
        private JPanel contentPane;
        private JPanel buttonPanel;
        private JPanel centerPanel;
        private CustomPanel[] colourPanel;
        private JPanel progressPanel;
    
        private JButton addVideoButton;
        private JButton removeVideoButton;
        private JButton startAllButton;
        private JButton stopAllButton;
    
        private JProgressBar progressBar;
    
        private Random random;
    
        public PlayerBase()
        {
            colourPanel = new CustomPanel[10];
    
            random = new Random();
        }
    
        private void displayGUI()
        {
            JFrame playerWindow = new JFrame("Player Window");
            playerWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
            contentPane = new JPanel(new BorderLayout(5, 5));
            contentPane.setBorder(
                    BorderFactory.createEmptyBorder(5, 5, 5, 5));
            buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));
            addVideoButton = new JButton("Add New Video");
            removeVideoButton = new JButton("Remove Video");
            startAllButton = new JButton("Start all tasks");
            stopAllButton = new JButton("Stop all tasks");
    
            buttonPanel.add(addVideoButton);
            buttonPanel.add(removeVideoButton);
            buttonPanel.add(startAllButton);
            buttonPanel.add(stopAllButton);
    
            contentPane.add(buttonPanel, BorderLayout.PAGE_START);
    
            JScrollPane scroller = new JScrollPane();
            centerPanel = new JPanel(new GridLayout(0, 1, 2, 2));
            for (int i = 0; i < colourPanel.length; i++)
            {
                colourPanel[i] = new CustomPanel(new Color(
                        random.nextInt(255), random.nextInt(255)
                        , random.nextInt(255)));
                centerPanel.add(colourPanel[i]);
            }
            scroller.setViewportView(centerPanel);
    
            contentPane.add(scroller, BorderLayout.CENTER);
    
            progressPanel = new JPanel(new BorderLayout(5, 5));
            progressBar = new JProgressBar(SwingConstants.HORIZONTAL);
            progressPanel.add(progressBar);
    
            contentPane.add(progressPanel, BorderLayout.PAGE_END);
    
            playerWindow.setContentPane(contentPane);
            playerWindow.pack();
            //playerWindow.setSize(750, 750);
            playerWindow.setLocationByPlatform(true);
            playerWindow.setVisible(true);
        }
    
        public static void main(String[] args)
        {
            Runnable runnable = new Runnable()
            {
                @Override
                public void run()
                {
                    new PlayerBase().displayGUI();
                }
            };
            EventQueue.invokeLater(runnable);
        }
    }
    
    class CustomPanel extends JPanel
    {
        public CustomPanel(Color backGroundColour)
        {
            setOpaque(true);
            setBackground(backGroundColour);
        }
    
        @Override
        public Dimension getPreferredSize()
        {
            return (new Dimension(750, 100));
        }
    }
    
    import javax.swing.*;
    导入java.awt.*;
    导入java.util.Random;
    /**
    *使用IntelliJ IDEA创建。
    *用户:Gagandeep Bali
    *日期:2013年5月17日
    *时间:下午6:09
    *要更改此模板,请使用文件|设置|文件模板。
    */
    公共类玩家库
    {
    私有JPanel内容窗格;
    私人JPanel按钮面板;
    私人JPanel中心面板;
    私人定制面板[]彩色面板;
    私人JPanel progressPanel;
    私有JButton addVideoButton;
    私人按钮移除视频按钮;
    私人JButton startAllButton;
    私有JButton stopAllButton;
    私人JProgressBar progressBar;
    私有随机;
    公共播放数据库()
    {
    ColorPanel=新定制面板[10];
    随机=新随机();
    }
    私有void displayGUI()
    {
    JFrame playerWindow=新JFrame(“播放器窗口”);
    playerWindow.setDefaultCloseOperation(JFrame.DISPOSE\u ON\u CLOSE);
    contentPane=newjpanel(新的BorderLayout(5,5));
    contentPane.setOrder(
    createEmptyByOrder(5,5,5,5));
    buttonPanel=新的JPanel(新的FlowLayout(FlowLayout.CENTER,5,5));
    addVideoButton=新JButton(“添加新视频”);
    removeVideoButton=新的JButton(“删除视频”);
    startAllButton=newjbutton(“启动所有任务”);
    stopAllButton=新JButton(“停止所有任务”);
    按钮面板。添加(添加视频按钮);
    按钮面板。添加(删除视频按钮);
    按钮面板。添加(开始按钮);
    buttonPanel.add(停止所有按钮);
    contentPane.add(按钮面板,边框布局,页面开始);
    JScrollPane scroller=新的JScrollPane();
    centerPanel=新的JPanel(新的网格布局(0,1,2,2));
    对于(int i=0;i
    输出:


    为什么不简单地将三个
    JPanel
    放在一个
    JPanel
    上,使用
    BorderLayout
    作为
    Layout Manager
    ,其中中间的
    JPanel
    和所有自定义面板及其各自大小可以容纳在
    JScrollPane
    中,如下示例所示:

    import javax.swing.*;
    import java.awt.*;
    import java.util.Random;
    
    /**
     * Created with IntelliJ IDEA.
     * User: Gagandeep Bali
     * Date: 5/17/13
     * Time: 6:09 PM
     * To change this template use File | Settings | File Templates.
     */
    public class PlayerBase
    {
        private JPanel contentPane;
        private JPanel buttonPanel;
        private JPanel centerPanel;
        private CustomPanel[] colourPanel;
        private JPanel progressPanel;
    
        private JButton addVideoButton;
        private JButton removeVideoButton;
        private JButton startAllButton;
        private JButton stopAllButton;
    
        private JProgressBar progressBar;
    
        private Random random;
    
        public PlayerBase()
        {
            colourPanel = new CustomPanel[10];
    
            random = new Random();
        }
    
        private void displayGUI()
        {
            JFrame playerWindow = new JFrame("Player Window");
            playerWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
            contentPane = new JPanel(new BorderLayout(5, 5));
            contentPane.setBorder(
                    BorderFactory.createEmptyBorder(5, 5, 5, 5));
            buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));
            addVideoButton = new JButton("Add New Video");
            removeVideoButton = new JButton("Remove Video");
            startAllButton = new JButton("Start all tasks");
            stopAllButton = new JButton("Stop all tasks");
    
            buttonPanel.add(addVideoButton);
            buttonPanel.add(removeVideoButton);
            buttonPanel.add(startAllButton);
            buttonPanel.add(stopAllButton);
    
            contentPane.add(buttonPanel, BorderLayout.PAGE_START);
    
            JScrollPane scroller = new JScrollPane();
            centerPanel = new JPanel(new GridLayout(0, 1, 2, 2));
            for (int i = 0; i < colourPanel.length; i++)
            {
                colourPanel[i] = new CustomPanel(new Color(
                        random.nextInt(255), random.nextInt(255)
                        , random.nextInt(255)));
                centerPanel.add(colourPanel[i]);
            }
            scroller.setViewportView(centerPanel);
    
            contentPane.add(scroller, BorderLayout.CENTER);
    
            progressPanel = new JPanel(new BorderLayout(5, 5));
            progressBar = new JProgressBar(SwingConstants.HORIZONTAL);
            progressPanel.add(progressBar);
    
            contentPane.add(progressPanel, BorderLayout.PAGE_END);
    
            playerWindow.setContentPane(contentPane);
            playerWindow.pack();
            //playerWindow.setSize(750, 750);
            playerWindow.setLocationByPlatform(true);
            playerWindow.setVisible(true);
        }
    
        public static void main(String[] args)
        {
            Runnable runnable = new Runnable()
            {
                @Override
                public void run()
                {
                    new PlayerBase().displayGUI();
                }
            };
            EventQueue.invokeLater(runnable);
        }
    }
    
    class CustomPanel extends JPanel
    {
        public CustomPanel(Color backGroundColour)
        {
            setOpaque(true);
            setBackground(backGroundColour);
        }
    
        @Override
        public Dimension getPreferredSize()
        {
            return (new Dimension(750, 100));
        }
    }
    
    import javax.swing.*;
    导入java.awt.*;
    导入java.util.Random;
    /**
    *使用IntelliJ IDEA创建。
    *用户:Gagandeep Bali
    *日期:2013年5月17日
    *时间:下午6:09
    *要更改此模板,请使用文件|设置|文件模板。
    */
    公共类玩家库
    {
    P