繁体   English   中英

如何使 JPanel 在 SplitPane 中采用 100% 的宽度和高度

[英]How to make JPanel take 100% width and height inside SplitPane

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

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

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

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

为什么你有这个问题?

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

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

现在,考虑这个面板宽度为 102。gridlayout 将如何平均共享它? 它不能。 所以它会让左边的 1 个像素为空,右边的为 1 个像素。 这正是你所面临的。 由于 GridLayout 不能平等地共享宽度(和高度),它将使其为空。 如果您将 window 做得更大,那么在某一时刻,空间将足以容纳它们:

看到这个.gif:

gif

在宽度等于795-801的时候,网格布局不能将空间平均分配给一行拥有的20个组件。 当它到达 802 时,让您烦恼的余量 go 消失了。 这是因为行中有 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) {
    }

}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM