java布局的问题 [英] Trouble with java layouts

查看:26
本文介绍了java布局的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经有一个带有 2 个组件的 jframe,每个组件都占据一半的宽度和所有的高度:

I used to have a jframe with 2 components in it, each of them taking half of the width and all of the height:

我使用 2 列 1 行的网格布局实现了这一点,效果很好.尽管现在我想在右侧的组件下方添加第三个组件:

I achieved this with a gridlayout with 2 columns and 1 row, which worked fine. Although now I'd like to add a third component beneath the one on the right:

有人知道我是如何做到这一点的吗?

Does anybody know how I could achieve this?

推荐答案

可以尝试嵌套一些JPanel,例如:

You can try nesting some JPanels, for example:

有一个带有 FlowLayout 的主窗格,用于保存左右窗格.

Have a main pane with a FlowLayout which will hold the left and right panes.

在右侧窗格中有一个 BoxLayoutGridLayout 来固定顶部和底部窗格.

On the right pane have a BoxLayout or GridLayout to hold the top and bottom pane.

在下面的示例代码中,它给出了以下输出

In the example code below which gives the following output

我添加了一些边框颜色,以便您可以看到它是如何工作的,我添加了一些标签,因为我懒得覆盖每个 JPanelgetPreferredSize(),但我认为这会让你知道如何从这里开始.

I added some border colors so you can see how it works, I added some labels because I'm too lazy to override the getPreferredSize() of each JPanel, but I think this will give you the idea on how to go on from here.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class LayoutExample {
    private JFrame frame;
    private JPanel pane;
    private JPanel leftPane;
    private JPanel rightPane;
    private JPanel topPane;
    private JPanel bottomPane;
    public static void main (String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new LayoutExample().createAndShowGui();
            }
        });
    }

    public void createAndShowGui() {
        frame = new JFrame("Layout Example");
        pane = new JPanel();
        leftPane = new JPanel() {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 300); 
            }
        };
        rightPane = new JPanel() {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 300);
            }
        };
        topPane = new JPanel();
        bottomPane = new JPanel();

        pane.setLayout(new FlowLayout());
        rightPane.setLayout(new GridLayout(2, 1, 5, 5));

        rightPane.add(topPane);
        rightPane.add(bottomPane);

        pane.add(leftPane);
        pane.add(rightPane);
        pane.setBorder(BorderFactory.createLineBorder(Color.green));
        leftPane.setBorder(BorderFactory.createLineBorder(Color.red));
        topPane.setBorder(BorderFactory.createLineBorder(Color.black));
        bottomPane.setBorder(BorderFactory.createLineBorder(Color.magenta));

        frame.setContentPane(pane);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

<小时>

注意:为了在上面的示例中在左右窗格上获得相同的尺寸,我不得不重写 getPreferredSize() 方法,那是因为我使用了 FlowLayout,在这种情况下,为了避免这样做,最好使用 @LukkasRotter 在下面的回答中给出的 2 个建议中的任何一个.


Note: In order to get the same dimension on both, left and right panes in my example above, I had to override the getPreferredSize() method, that's because I used a FlowLayout, in this case, to avoid doing this it's better to use any of the 2 suggestions given by @LukkasRotter in his answer below.

这篇关于java布局的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆