1

在此处输入图像描述

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

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

我正在尝试的是:

  • 顶部的这 4 个按钮作为细条
  • 里面的滚动窗格JPanels将包含所有视频转换任务。这占用了最大的空间
  • AJPanel在底部包含 aJProgressBar作为细条。
  • 但似乎有些地方搞砸了。请帮我解决这个问题

    SSCCE

    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);
        }
    }
    
    4

    2 回答 2

    2

    为什么不简单地将三个JPanels 放在一个as的顶部,其中所有JPanel具有各自大小的自定义面板的中间都可以容纳在 a 内,如下例所示:BorderLayoutLayout ManagerJPanelJScrollPane

    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));
        }
    }
    

    输出 :

    播放器布局

    于 2013-05-17T14:56:29.373 回答
    2

    用这个

    gbc.weightx = 1;
    gbc.weighty = 1;
    gbc.fill = GridbagConstraints.BOTH
    
    于 2013-05-17T12:01:57.893 回答