如何使用 MigLayout 控制面板的整体尺寸限制?

发布于 2024-07-30 05:44:45 字数 996 浏览 8 评论 0原文

我正在为一个项目测试 MigLayout,但我似乎无法弄清楚 MigLayout 控制正在布局的整个面板大小的方法。 我正在改编 MigLayout 白皮书 中的示例。 另外,我使用 Python 编写并使用 Jython,而不是使用 Java 编写。 也就是说,这是我当前的代码。

layout = MigLayout("fillx",
                   # Column constraints
                   "[right]rel[grow,fill]",
                   # Row constraints
                   "[]10[]10[]")
panel = JPanel(layout)

panel.add(JLabel("Enter size:"),
          "")
panel.add(JTextField(""),
          "wrap, width 150:250")
panel.add(JLabel("Enter weight:"),
          "")
panel.add(JTextField(""),
          "wrap, width 150:250")
panel.add(JButton("Ok"),
          "span 2, align center, width 100:150")

然后该面板进入 JFrame 进行显示。 当我调整框架大小时,控件会很好地调整大小并遵循其最小尺寸。 但是,框架上没有最小尺寸,似乎也没有任何方法可以达到面板的实际最小尺寸来为框架设置它。 询问面板的最小尺寸会返回一个尺寸,当在框架上设置该尺寸(加上插图)时,会切断按钮和一半的文本字段。

我想要的是能够设置框架的最小尺寸(不是硬编码!),以便面板适合并且其控件不会被切断。 做这个的最好方式是什么?

I am testing out MigLayout for a project, and I can't seem to figure out the MigLayout way of controlling the size of the whole panel which is being layed out. I am adapting the example in the MigLayout whitepaper. Also, I am writing in Python and using Jython rather than writing in Java. That said, here is my current code.

layout = MigLayout("fillx",
                   # Column constraints
                   "[right]rel[grow,fill]",
                   # Row constraints
                   "[]10[]10[]")
panel = JPanel(layout)

panel.add(JLabel("Enter size:"),
          "")
panel.add(JTextField(""),
          "wrap, width 150:250")
panel.add(JLabel("Enter weight:"),
          "")
panel.add(JTextField(""),
          "wrap, width 150:250")
panel.add(JButton("Ok"),
          "span 2, align center, width 100:150")

The panel then goes into a JFrame for display. When I resize the frame, the controls resize nicely and obey their minimum sizes. However, there is no minimum size on the frame, nor does there seem to be any way to get to the real minimum size of the panel to set it for the frame. Asking for the panel's minimum size returns a size which, when set on the frame (plus insets), cuts off the button and half of the text fields.

What I want is to be able to set the minimum size for the frame (not hard-coded!) so that the panel fits and none of its controls are cut off. What is the best way to do this?

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

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

发布评论

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

评论(3

清风挽心 2024-08-06 05:44:45

包怎么样方法???

更新:

现在我明白你想要做什么。 很抱歉让你失望了,但是90%
的独立应用程序是使用

  1. 具有固定大小(不可调整大小)和完美调整内容的对话框窗口

  2. < p>可调整大小但具有最小尺寸(例如 800x600)的主窗口

您想要的(没有硬编码值的可调整大小的窗口)是可能的但很难实现(这是另外 10% 的应用程序)

由于 Java Swing 在布局方面存在着众所周知的困难(这是存在如此多布局管理器的主要原因),我建议您采用简单的方法。

您可以使用各种组件的 setPreferedSize 和 setMinimum size 方法,但是
很难让它完美。

How about the pack method ???

Update:

Now I understand what you are trying to do. I am sorry to disappoint you but 90%
of standalone applications are written with

  1. Dialog windows that have a fixed size (non-resizable) and a perfectly tuned to content

  2. Main Windows that are resizable but have a minimum size (e.g. 800x600)

What you want (a resizable window with no hard-coded values) is possible but very difficult to achieve (this is the other 10% of applications)

Since Java Swing has notorious difficulties with layout (this is the main reason of the existance of so many Layout managers) I suggest you follow the easy way.

You can play with the setPrefferedSize and setMinimum size methods of various component but
it will be hard to get it perfect.

撑一把青伞 2024-08-06 05:44:45

就我个人而言,我不会过分强调最小尺寸,只要首选尺寸(在 pack() 之后得到的尺寸是合适的)。如果由于某种原因用户想要将窗口缩小到其中组件的尺寸也许他们只是想把你的窗口底部移开,这样他们就可以同时看到显示器上剩下的东西和其他东西,

我认为任何窗口的真正最小尺寸是。它的边框和标题栏以及标题文本的几个字符和省略号的空间的总和如果用户想要比允许所有组件显示未剪辑的内容更小的内容,那么您将

使用以下几个字符中的任何一个 来强制执行?网络上提供基于表格的布局管理器,例如 MIG Layout,您应该能够轻松获得所需的大小调整行为

无耻的自插件:如果您发现 MIG Layout 很难使用,我的 MatrixLayout 我认为更容易使用。 但MIG布局更强大。 我特意选择对 MatrixLayout 进行编码,以免阻止面板减小到计算出的最小尺寸以下。

Personally, I wouldn't stress too much about the minimum size as long as the preferred size (what you get after pack() is appropriate. If for some reason the user wants to reduce the window down to a size where the components in it get clipped, then let them. Maybe they just want to get the bottom of your window out the way so they can see what remains and something else on their monitor at the same time.

I would argue that the true minimum size of any window is the sum of it's border and title bar and room for a few characters of the title text and an ellipsis. If the user want something smaller than what will allow all the components to show unclipped who are you to enforce otherwise?

Using any of the several table-based layout managers available on the web, like MIG Layout, you should be able to easily get the resizing behavior you want.

Shameless self plug: If you find MIG Layout hard to use, my MatrixLayout is, I think, easier to use. But MIG layout is more powerful. I deliberately chose to code MatrixLayout to not inhibit panels from reducing below their calculated minimum size.

情仇皆在手 2024-08-06 05:44:45

我刚刚弄清楚如何根据所有包含的组件的大小总和来刷新帧大小,而无需对任何内容进行硬编码。

  1. 创建一个 JPanel 面板 作为您的工作面板,而不是触摸 contentPane。 只需将其添加回 contentPane 即可。 不要触摸 contentPane,它是关键。

  2. 设置panel的布局,无需硬编码行高、列宽等这可能会破坏布局,因为您的硬编码高度可能会更小或更多,从而留下一些尺寸错误的行,并保留最后一行/列的一半。

  3. 将您的元素添加到面板中。 添加它们时,您可以指定大小。

  4. panel添加回contentPanegetContentPane().add(panel);我们不需要设置<的布局代码>contentPane。

  5. 最后,pack()setVisible(true)如你所愿。 无需setSize()setBounds()等。插入和间隙将由MigLayout自动处理。Viola!

SCCEE:

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

import net.miginfocom.swing.MigLayout;

public class InsetsAndBorder extends JFrame {
    public InsetsAndBorder() {
        begin();
    }

    private void begin() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        panel.setLayout(new MigLayout("insets 2 2 2 2, fillx, debug", "3[]3[]3[]3", "5[]5[]5[]5"));

        JLabel label1 = new JLabel("1");
        JLabel label2 = new JLabel("2");

        JButton button = new JButton("No way!");

        panel.add(label1, "cell 1 2, grow");

        panel.add(label2, "cell 2 2, grow");

        panel.add(button, "cell 0 1, grow");

        getContentPane().add(panel);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                InsetsAndBorder frame = new InsetsAndBorder();

            }

        });
    }
}

I just figured out how to refresh the frame size according to the sum of size of all the contained components without hardcoding anything.

  1. Create a JPanel panel as your working panel, instead of touching the contentPane. Just add it back to the contentPane. Don't touch the contentPane, it is the key.

  2. Set the layout of panel without hardcoded row height, column width, etc. This may ruin the layout, because your hardcoded height may be lesser or more of it is needed, leaving some line with wrong size, and leave your last line/column half cut off.

  3. Add your elements into panel. When adding them you can specify sizes.

  4. Add panel back to contentPane: getContentPane().add(panel); We don't need to set the layout of contentPane.

  5. At last, pack(), setVisible(true) as you wish. No need to setSize(), setBounds(), etc. The insets and gaps will be handled automatically by MigLayout. Viola!

A SCCEE:

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

import net.miginfocom.swing.MigLayout;

public class InsetsAndBorder extends JFrame {
    public InsetsAndBorder() {
        begin();
    }

    private void begin() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        panel.setLayout(new MigLayout("insets 2 2 2 2, fillx, debug", "3[]3[]3[]3", "5[]5[]5[]5"));

        JLabel label1 = new JLabel("1");
        JLabel label2 = new JLabel("2");

        JButton button = new JButton("No way!");

        panel.add(label1, "cell 1 2, grow");

        panel.add(label2, "cell 2 2, grow");

        panel.add(button, "cell 0 1, grow");

        getContentPane().add(panel);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                InsetsAndBorder frame = new InsetsAndBorder();

            }

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