Java - MigLayout 设置边界

发布于 2024-12-23 03:06:54 字数 2009 浏览 0 评论 0原文

所以我有一个无法解决的问题,我希望我的 GUI 应用程序分为 3 个 JPanel(左、中、右)。我希望左面板和右面板具有固定尺寸,并且中心是流动的。这意味着当 JFrame 展开时,侧面板仅垂直展开,而中心面板则水平和垂直展开。

我已将所有面板的最小尺寸设置为高度 600,但它们只是保持在最小尺寸,并且不会随着 JForm 的增加而扩展,我不知道如何设置 JFrame 边框的边界,以便它们随其扩展。

package ppe.view;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import net.miginfocom.swing.MigLayout;

public class UI_View extends JFrame
{
    private JList       browse  = new JList();
    private JScrollPane rightX   = new JScrollPane();
    private JButton     btn1    = new JButton("Button 1");
    private JButton     btn2    = new JButton("Button 2");
    private JButton     btn3    = new JButton("Button 3");
    private JButton     btn4    = new JButton("Button 4");


    public UI_View()
    {
        this.setTitle("Prototype MVC Arhitecture");
        this.setMinimumSize(new Dimension(800, 600));
        this.setExtendedState(this.MAXIMIZED_BOTH);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new MigLayout());

        JPanel content = new JPanel(new MigLayout());
        content.setBackground(Color.black);

        JPanel right  = new JPanel(new MigLayout());
        JPanel center = new JPanel(new MigLayout());
        JPanel left   = new JPanel(new MigLayout());

        right.setBackground(Color.red);
        right.setMinimumSize(new Dimension(200, 600));
        right.setMaximumSize(new Dimension(200, 37500));

        center.setBackground(Color.green);
        center.setMinimumSize(new Dimension(400, 600));

        left.setBackground(Color.blue);
        left.setMinimumSize(new Dimension(200, 600));
        left.setMaximumSize(new Dimension(200, 37500));

        content.add(left);
        content.add(center);
        content.add(right);

        this.setContentPane(content);
    }

    public static void main(String[] args)
    {
        new UI_View().setVisible(true);
    }

}

我尝试将它们绑定到另一个内容面板,并将该面板作为 ContentPane 添加到 JFrame,自动将其绑定到 JFrame 边框,但问题仍然基本修复。

so i have a problem i cant solve i want my GUI app to be split between 3 JPanels (left, center, right). I want left panel and right panel to be of fixed size and center to be fluid. Meaning side panels expand only vertically as JFrame is expanded and center panel expands bot horizontally and vertically.

I have set minimal size for all panels to be height of 600 but they just stay in the minimal size and dont expand as JForm increases i dont know how to set bounds to JFrame borders so they expand whit it.

package ppe.view;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import net.miginfocom.swing.MigLayout;

public class UI_View extends JFrame
{
    private JList       browse  = new JList();
    private JScrollPane rightX   = new JScrollPane();
    private JButton     btn1    = new JButton("Button 1");
    private JButton     btn2    = new JButton("Button 2");
    private JButton     btn3    = new JButton("Button 3");
    private JButton     btn4    = new JButton("Button 4");


    public UI_View()
    {
        this.setTitle("Prototype MVC Arhitecture");
        this.setMinimumSize(new Dimension(800, 600));
        this.setExtendedState(this.MAXIMIZED_BOTH);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new MigLayout());

        JPanel content = new JPanel(new MigLayout());
        content.setBackground(Color.black);

        JPanel right  = new JPanel(new MigLayout());
        JPanel center = new JPanel(new MigLayout());
        JPanel left   = new JPanel(new MigLayout());

        right.setBackground(Color.red);
        right.setMinimumSize(new Dimension(200, 600));
        right.setMaximumSize(new Dimension(200, 37500));

        center.setBackground(Color.green);
        center.setMinimumSize(new Dimension(400, 600));

        left.setBackground(Color.blue);
        left.setMinimumSize(new Dimension(200, 600));
        left.setMaximumSize(new Dimension(200, 37500));

        content.add(left);
        content.add(center);
        content.add(right);

        this.setContentPane(content);
    }

    public static void main(String[] args)
    {
        new UI_View().setVisible(true);
    }

}

I have tryed bounding them to another content panel and adding that panel as ContentPane to JFrame that automatically bounds it to JFrame border but the thing is still pretty much fixed.

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

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

发布评论

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

评论(1

蓝梦月影 2024-12-30 03:06:54

如果您使用 MiGLayout 为什么不在添加组件时配置约束?

例如,这可能会有所帮助(尽管我也是 MiGLayout 初学者):

 content.add(left, "growy");
 content.add(center, "grow"); //the same as growx, growy
 content.add(right, "growy");

在某些情况下,我还需要添加 pushx 但我不确定何时这是需要的。请参阅文档以获取更多信息。

编辑:似乎您总是必须为导致列/行增长的组件添加push。否则,单独的“增长”将使组件与它们所在的列/行一样大,而这些组件又由该列/行中最大的组件定义。如果有更多可用空间,则如果没有 push 关键字,可用列/行将不会增长以填充它。

从文档中:

组件永远不会使用grow关键字将列/行的大小“推”得更大。

If you're using MiGLayout why don't you configure the constraints when adding the components?

This, for example, might help (although I'm a MiGLayout beginner, too):

 content.add(left, "growy");
 content.add(center, "grow"); //the same as growx, growy
 content.add(right, "growy");

In some cases I needed to also add a pushx but I'm not sure when this is needed. Please refer to the documentation for further information.

Edit: it seems like you always have to add push for components that should cause the column/row to grow. Otherwise grow alone would make the components as big as the column/row they are in which in turn is defined by the largest component in that column/row. If there is more space available columns/rows won't grow to fill it without the push keyword.

From the documentation:

Components will never "push" the column/row's size to be larger using the grow keyword.

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