带有 GridBagLayout 的 java 侧边栏

发布于 2024-08-15 01:18:33 字数 1473 浏览 10 评论 0原文

我正在尝试为我正在从事的项目绘制侧边栏。我选择使用 GridBagLayout 是因为我对 BoxLayout 的局限性感到沮丧。有人可以帮助解释我做错了什么吗?我想要的是侧边栏包含两个 JPanel。我的代码将它们放置在侧边栏的中间位置而不是顶部。有人可以解释一下我在这里缺少什么吗?

    JPanel sideBar = new JPanel();
    sideBar.setBounds(0, 0, 180, (int)this.getBounds().getHeight());
    sideBar.setLayout(new GridBagLayout());


    JPanel optionBar = new JPanel();
    optionBar.setBorder(BorderFactory.createTitledBorder("Box1"));
    optionBar.setLayout(new GridBagLayout());


    JPanel buttonBar = new JPanel();
    buttonBar.setBorder(BorderFactory.createTitledBorder("Options"));
    buttonBar.setLayout(new GridBagLayout());


    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 0;
    c.ipady = 5;
    c.insets = new Insets(10,0,0,0);


    JButton simplify;
    simplify = new JButton("Open");
    simplify.addActionListener( this.listener );
    c.gridy = 0;
    buttonBar.add(simplify, c);

    JButton mergeButton;
    mergeButton = new JButton("Close");
    mergeButton.addActionListener( this.listener );
    c.gridy = 1;
    buttonBar.add(mergeButton, c);

    JButton splitButton;
    splitButton = new JButton("Merge");
    splitButton.addActionListener( this.listener );
    c.gridy = 2;
    buttonBar.add(splitButton, c);

    c.insets = new Insets(0,5,5,5);
    c.gridy = 0;
    sideBar.add(optionBar, c);

    c.gridy = 1;
    c.ipadx = 70;
    sideBar.add(buttonBar, c);

    return(sideBar);

I am attempting to draw a sidebar for a project that I am working on. I chose to use GridBagLayout because I became frustrated with the limitations of BoxLayout. Could someone help explain what I am doing wrong. What I want is for the side bar to contain two JPanels. The code that I have places them halfway down the sidebar instead of at the top. Could someone explain what I am missing here.

    JPanel sideBar = new JPanel();
    sideBar.setBounds(0, 0, 180, (int)this.getBounds().getHeight());
    sideBar.setLayout(new GridBagLayout());


    JPanel optionBar = new JPanel();
    optionBar.setBorder(BorderFactory.createTitledBorder("Box1"));
    optionBar.setLayout(new GridBagLayout());


    JPanel buttonBar = new JPanel();
    buttonBar.setBorder(BorderFactory.createTitledBorder("Options"));
    buttonBar.setLayout(new GridBagLayout());


    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 0;
    c.ipady = 5;
    c.insets = new Insets(10,0,0,0);


    JButton simplify;
    simplify = new JButton("Open");
    simplify.addActionListener( this.listener );
    c.gridy = 0;
    buttonBar.add(simplify, c);

    JButton mergeButton;
    mergeButton = new JButton("Close");
    mergeButton.addActionListener( this.listener );
    c.gridy = 1;
    buttonBar.add(mergeButton, c);

    JButton splitButton;
    splitButton = new JButton("Merge");
    splitButton.addActionListener( this.listener );
    c.gridy = 2;
    buttonBar.add(splitButton, c);

    c.insets = new Insets(0,5,5,5);
    c.gridy = 0;
    sideBar.add(optionBar, c);

    c.gridy = 1;
    c.ipadx = 70;
    sideBar.add(buttonBar, c);

    return(sideBar);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

会发光的星星闪亮亮i 2024-08-22 01:18:33

如果您熟悉/更熟悉 HTML,则可以使用 table2gridbag。它是一个小型控制台工具,它获取布局描述(HTML 表)并将其转换为用于配置 GridBagLayout 管理器的等效布局描述

If you are familiar/more comfortable with HTML, you could use table2gridbag. It's a small console tool that takes a layout description (HTML table) and translates that into an equivalent layout description for configuring the GridBagLayout manager

栀梦 2024-08-22 01:18:33

GridBagLayout 只会分配组件所需的足够垂直空间,其余部分留空。我希望您看到侧边栏组件垂直居中?

为了将组件“推出”,您需要设置垂直权重。如果您将最后一个组件的 weighty 约束设置为 1.0,它将占用该组件的所有剩余垂直空间,并将其余部分推到顶部。 (您可能还需要将最后一个面板锚定到 GridBagConstraints.NORTH )。

sideBar.add(buttonBar, c); 之前尝试 c.weighty = 1.0

GridBagLayout will only allocate enough vertical space that a component requires, leaving the rest blank. I expect you're seeing your side bar components centred vertically?

In order to "push" the components out, you need to set a vertical weight. If you set the weighty constraint to 1.0 on your last component, it will take up all remaining vertical space for that component and push the rest to the top. (You may also need to anchor that last panel to GridBagConstraints.NORTH).

Try c.weighty = 1.0 before sideBar.add(buttonBar, c);

冰葑 2024-08-22 01:18:33

我的代码放置了它们
位于侧边栏的中间而不是位于
顶部。

那么,您需要首先阅读 Swing 教程 来学习如何正确使用布局管理器。 BoxLayout 比 GridBagLayout 容易得多,因为您不必学习如何指定约束。但如果您想使用 GridBagLayout,请阅读“如何使用 GridBagLayout”部分。您可能需要重点关注处理“weightx 和weighty”约束的部分。根据我有限的知识,应该有助于解决问题。

另外,当使用布局管理器时,您不使用 setBounds() 或 setSize()。

The code that I have places them
halfway down the sidebar instead of at
the top.

Well you need to start by reading the Swing tutorial to learn how to use the layout managers properly. BoxLayout is far easier than GridBagLayout because you don't have to learn how to specify constraints. But if you want to use GridBagLayout, then read the section on "How to Use GridBagLayout". You may want to concentrate on the section dealing with the "weightx and weighty" constraints. Based on my limited knowledge that should help solve the problem.

Also, when using layout managers you don't use setBounds() or setSize().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文