【问题标题】:How to make JPanel take 100% width and height inside SplitPane如何使 JPanel 在 SplitPane 中采用 100% 的宽度和高度
【发布时间】:2019-11-16 19:45:53
【问题描述】:

所以我有一个带有 2 个侧面的 splitPane,其中包含 2 个面板。其中一个面板是经过检查的(分成正方形),但它周围有意想不到的边距。这是我的意思http://prntscr.com/pxwfsk 的屏幕截图。我怎样才能摆脱这些边距,因为对于我的程序来说它是至关重要的。预先感谢。下面是负责创建 splitPane 和 JPanels 的部分代码:

        public static void workingFrame() throws InterruptedException {

            String frameName = "Bot World";

            World world = new World(); // creating right side with world for bots

            WorkFrame workF = new WorkFrame(0, 0, frameName);
            wfFrame = workF.newFrame();
            wfFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
            wfFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JSplitPane splitPane = new JSplitPane();
            splitPane.setSize(width, height);
//            splitPane.setDividerSize(1);
//            splitPane.setDividerLocation(149);
            splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);

            JPanel panelLeft = createLftPanel();
            JPanel panelRight = world.createRightPanel();

            splitPane.setLeftComponent(panelLeft);
            splitPane.setRightComponent(panelRight);
            wfFrame.add(splitPane);

            wfFrame.revalidate();
            wfFrame.setVisible(true);

            // WE NEED THIS HERE because otherwise PC is not fast enough to establish all the JPanles inside the main panel
            // and thus later on we cannot use method GetComponent();
            Thread.sleep(1000);
            // create bots on random location on panels
            for (int i = 0; i < 1; i++) {
                world.createBots();
                // add counter to start counting bots on a map
            }

            for (int i = 0; i < 1; i++) {
                // create food in this world
                world.createFood();
            }

            wfFrame.revalidate();

            while (world.bots.size() > 0) {
                for (Bot bot : world.bots) {
                    if (bot.isAlive) {
                        bot.seePathInDirection(panelRight);
                        Thread.sleep(500);
                        wfFrame.revalidate();
                    }
                    Thread.sleep(500);
                    wfFrame.revalidate();
                }
            }
        }
    }

和 createRightPanel 方法:

    public static JPanel createRightPanel() {
        JPanel panel = new JPanel();

        panel.setLayout(new GridLayout(ROWS, COLS));
        for (int i = 0; i < ROWS; i++) {
            for (int j = 0; j < COLS; j++) {
                JPanel pane = new JPanel();
                pane.setBackground(Color.WHITE);
                pane.setBorder(BorderFactory.createLineBorder(Color.black));
                panel.add(pane);
            }
        }
        botWorld = panel;
        return panel;
    }

【问题讨论】:

    标签: java swing jpanel jsplitpane


    【解决方案1】:

    您的代码存在一些“问题”。首先,不要使用组件的setSize(也不要setPreferredSize)方法。让布局管理器计算它的大小和位置。

    不要在Event Dispatch Thread 中使用Thread.sleep()。它将冻结整个 GUI。由于线程休眠,事件无法发生。考虑using a Swing TimerSwing Worker

    为什么会出现这个问题?

    因为网格布局将赋予其所有组件相同的大小。确实有点难理解,所以我会试着用一个例子来解释一下。

    考虑一个只有 1 行 10 列的网格布局的面板。您尝试向其中添加 10 个组件。此面板的宽度等于 100。网格布局将为每个组件提供 10 的宽度,因此 10x10 = 100。

    现在,考虑这个面板宽度为 102。gridlayout 将如何平均分配它?它不能。所以它会让左边的 1 个像素为空,右边的为 1 个像素。这正是你所面临的。由于 GridLayout 不能平等地共享宽度(和高度),它将使其为空。如果你把窗户开大一点,空间就足以让他们平等地容纳:

    查看此 .gif:

    在宽度等于795-801的时候,网格布局不能将空间平均分配给一行拥有的20个组件。当它变为 802 时,让您烦恼的边距消失了。这是因为行中有 20 个组件 / 800 = 每个组件有 40 个宽度。 +2 左右边框(绿色)。

    产生这种行为的代码:

    public class Example extends JFrame implements ComponentListener {
        private static final int ROWS = 20;
        private static final int COLUMNS = 20;
        private JLabel widthLabel;
        private JPanel greenPanel;
    
        public Example() {
            super("test");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setLayout(new BorderLayout());
    
            JPanel redPanel = new JPanel();
            redPanel.setBorder(BorderFactory.createLineBorder(Color.RED, 1));
    
            widthLabel = new JLabel();
            redPanel.add(widthLabel);
    
            greenPanel = new JPanel(new GridLayout(ROWS, COLUMNS));
            greenPanel.setBorder(BorderFactory.createLineBorder(Color.GREEN, 1));
            greenPanel.addComponentListener(this);
            for (int i = 0; i < ROWS * COLUMNS; i++) {
                JPanel panel = new JPanel(new BorderLayout());
                panel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
    
                greenPanel.add(panel);
            }
    
            JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, redPanel, greenPanel);
    
            add(splitPane);
            setLocationByPlatform(true);
            pack();
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> {
                new Example().setVisible(true);
            });
        }
    
        @Override
        public void componentResized(ComponentEvent e) {
            widthLabel.setText("Green panel's width: " + greenPanel.getWidth());
        }
    
        @Override
        public void componentMoved(ComponentEvent e) {
        }
    
        @Override
        public void componentShown(ComponentEvent e) {
        }
    
        @Override
        public void componentHidden(ComponentEvent e) {
        }
    
    }
    

    为了回答您的评论,不。网格布局中的组件类型无关紧要。所以 JPanel 与否,它没有任何作用。

    【讨论】:

    • 我明白了,感谢您的评论,这真的很有帮助。我摆脱了 setSize 属性并将边框设置到右侧面板以查看它的结束位置。显然问题不在于父面板本身,而在于它如何创建内部网格视图。在这里检查我的意思:prntscr.com/pxx5vo 所以可能的问题是 createRightPanel 方法中的代码。我猜 GridLayout 不能与 JPanel 一起正常工作。你怎么看?
    • (1+) 要保持面板与顶部/左侧同步,您可以使用“包装器”面板。 1) 创建一个 JPanel,其 FlowLayout 与水平/垂直间隙设置为 0 左对齐。 2) 将 greenPanel 添加到包装器中。 3) 将包装器添加到 splitPane。额外的空间将出现在拆分窗格的右侧/左侧。
    • 我明白你的意思。所以宽度和高度应该是整数(比如 1600 x 2800),所以 gridLayout 可以毫无问题地拆分它,这应该来自 JPanel 大小不接受双数的事实(否则它会拆分它没有问题)。无论如何,非常感谢,乔治。你给了我很好的教训。
    • @camickr 我不认为它会解决我的问题,因为父面板的大小仍然是 splitPane 的大小
    • 您也可以尝试GridBagLayout,其中列和行具有不同的调整大小权重。这将导致额外的像素在权重较高的行/列之间共享。这种效果肉眼几乎看不到。
    猜你喜欢
    • 2011-08-20
    • 1970-01-01
    • 1970-01-01
    • 2019-03-08
    • 2016-08-09
    • 2015-02-04
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    相关资源
    最近更新 更多