java Swing 中的布局指定大小

发布于 2025-01-03 05:23:48 字数 677 浏览 0 评论 0原文

我的问题是如何指定布局上零件的尺寸?
我需要以某种方式设置“零件”的大小,而不是使用preferedSize,也许在布局管理器中,无论在哪里 - 我只需要稳定的大小。

我想为游戏创建布局。我已经创建了一个,但我正在处理组件大小的问题。所以我认为最好对我的布局有更好的概念。
让我们看看我的草稿。

+-----------+ 
| UPPER     |
+-----+-----+
|  A  |     |
+-----+  C  |
|  B  |     |
+-----+-----+
| Footer    |
+-----------+

A+B+C make together Center.

主要部分由该树部分组成:
上层-会有菜单。
中心 - 由 A、B、C 3 部分组成
页脚 - 会有状态栏


我的想法是能够设置每个组件的大小。 所有布局都取决于 C 部分,它的大小可能为 450x450 像素或 600x600 像素。
对于 A 部分和 B 部分,我只需要指定宽度,因为只有一些文本信息 - 它应该约为 300 px。
我尝试将 GridBagLayout 用于中心部分,但 C 的 setSize 效果不佳。
我在容器 (java.awt.Container) 中制作部件 - 在其中添加每个部件的内容,然后将容器添加到上层。

My question is how can I specify size of the parts on my layout?
I need somehow set size of "parts" not using preferedSize, maybe in layout managers, doesn't matter where - only I need is stable size.

I want to create layout for game. I've already created one but I'm dealing with problem with size of components. So I considered that it would be better to make better concept of my layout.
Let's look at my draft.


+-----------+ 
| UPPER     |
+-----+-----+
|  A  |     |
+-----+  C  |
|  B  |     |
+-----+-----+
| Footer    |
+-----------+

A+B+C make together Center.

Main part consist of this tree parts:
Upper- there will be menu.
Center - this consists of 3 parts A,B,C
Footer - there will be status bar

My idea is to be able to set the size of each component.
All layout is dependent on part C it could have size 450x450 px or 600x600 px.
For part A and B i need specify only the width, because there will be only some text info - it should be about 300 px.
I tryed to use GridBagLayout for Center part but setSize for C didn't worked well.
I make the parts in Containers (java.awt.Container) - in them I add the content of each part and then add the Container to the upper level.

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

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

发布评论

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

评论(5

飘落散花 2025-01-10 05:23:48

最简单的方法:对 contentPane 使用 BorderLayout (已经是)
- 上部面板向北
- 页脚面板向南
- 面板 A 和 B 进入带有 GridLayout(2,1) 的面板 ab
- 面板 ab 和 C 进入带有 GridLayout(1,2) 的面板 abc
- 面板 abc 进入中心
以及 A、B、C 的 setPreferedSize()

The simplest way: use BorderLayout for the contentPane (which already is)
- Upper panel goes to North
- Footer panel goes to South
- Panels A and B goes into a Panel ab with GridLayout(2,1)
- Panel ab and C goes into a Panel abc with GridLayout(1,2)
- Panel abc goes into the Center
And setPrefferedSize() of your A, B, C

念三年u 2025-01-10 05:23:48

一般来说,GridBagLayout 会忽略您使用 setSize 为控件设置的值,而是询问控件的首选大小(通过调用 getPreferredSize)并使用它来计算总体布局。不建议您自己简单地设置首选大小,因为大多数控件都会在触发布局时重新计算这些值,因此您将很难让它们“粘住”。

如果您确实想确保 UI 元素 C 具有一定的大小,请将其实现为从合适的基类(例如 JPanel)派生的自定义类,并覆盖 getPreferredSize 方法使其返回您的 UI 该部分想要/需要的大小。

编辑:这是一个包装器的小示例,它可以包含另一个 UI 元素,并且可以设置为固定大小(使用已被覆盖的 setSize 方法),它应该受到布局管理器的尊重:

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JComponent;
import javax.swing.JPanel;

public class FixedSizeComponent extends JPanel {
    private Dimension size;
    private final JComponent content;

    public FixedSizeComponent(JComponent content) {
        super(new BorderLayout());
        this.content = content;
        super.add(content, BorderLayout.CENTER);
    }

    @Override
    public void setSize(Dimension d) {
        size = d;
    }

    @Override
    public void setSize(int width, int height) {
        size = new Dimension(width, height);
    }

    @Override
    public Dimension getSize() {
        if (size != null) return size;
        return content.getSize();
    }

    @Override
    public Dimension getSize(Dimension rv) {
        if (size != null) {
            if (rv == null) rv = new Dimension();
            rv.height = size.height;
            rv.width = size.width;
            return rv;
        }
        return content.getSize(rv);
    }

    @Override
    public Dimension getPreferredSize() {
        if (size != null) return size;
        return content.getPreferredSize();
    }

    @Override
    public Dimension getMaximumSize() {
        if (size != null) return size;
        return content.getMaximumSize();
    }

    @Override
    public Dimension getMinimumSize() {
        if (size != null) return size;
        return content.getMinimumSize();
    }
}

In general, GridBagLayout ignores the values you set for controls with setSize, instead it asks the controls for their preferred size (by calling getPreferredSize) and uses that for calculating the overall layout. Simply setting that preferred size yourself is not recommended, since most controls tend to recalculate those values whenever a layout is triggered, so you will have a hard time getting them to "stick".

If you really want to make sure the UI element C has a certain size, implement it as a custom class deriving from a suitable base (JPanel, for example) and override the getPreferredSize method to make it return the size you want/need for that part of your UI.

Edit: Here's a little example for a wrapper that can contain another UI element and can be set to a fixed size (using the setSize method which has been overridden), which should be respected by layout managers:

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JComponent;
import javax.swing.JPanel;

public class FixedSizeComponent extends JPanel {
    private Dimension size;
    private final JComponent content;

    public FixedSizeComponent(JComponent content) {
        super(new BorderLayout());
        this.content = content;
        super.add(content, BorderLayout.CENTER);
    }

    @Override
    public void setSize(Dimension d) {
        size = d;
    }

    @Override
    public void setSize(int width, int height) {
        size = new Dimension(width, height);
    }

    @Override
    public Dimension getSize() {
        if (size != null) return size;
        return content.getSize();
    }

    @Override
    public Dimension getSize(Dimension rv) {
        if (size != null) {
            if (rv == null) rv = new Dimension();
            rv.height = size.height;
            rv.width = size.width;
            return rv;
        }
        return content.getSize(rv);
    }

    @Override
    public Dimension getPreferredSize() {
        if (size != null) return size;
        return content.getPreferredSize();
    }

    @Override
    public Dimension getMaximumSize() {
        if (size != null) return size;
        return content.getMaximumSize();
    }

    @Override
    public Dimension getMinimumSize() {
        if (size != null) return size;
        return content.getMinimumSize();
    }
}
楠木可依 2025-01-10 05:23:48

我遇到了类似的问题,底部的状态工具栏包含许多其他组件。我的问题是它会变得更高。所以我所做的就是覆盖最大尺寸,将最大高度设置为最小高度。

JPanel status = new JPanel( new SpringLayout() ) {
  @Override
  public Dimension getMaximumSize() {
    Dimension max = super.getMaximumSize();
    Dimension min = getMinimumSize();
    return new Dimension( max.width, min.height );
  }
};

I had a similar problem, with a status tool bar at the bottom containing a number of other components. My problem was that it would get taller. So what I did was to override the maximum size setting the maximum height to be the minimum height.

JPanel status = new JPanel( new SpringLayout() ) {
  @Override
  public Dimension getMaximumSize() {
    Dimension max = super.getMaximumSize();
    Dimension min = getMinimumSize();
    return new Dimension( max.width, min.height );
  }
};
百合的盛世恋 2025-01-10 05:23:48

这个答案太少了,而且太晚了,

(也许这个方法在提出这个问题时不存在)

就像getPreferredSize一样,还有一个setPreferredSize方法它接受一个 Dimension 对象。

默认情况下,您的布局将忽略组件大小(您可能已使用 setSize 设置),而是使用首选大小。

通过使用setPreferredSize,您将能够覆盖组件的默认首选尺寸

This answer is too little and WAY too late,

(maybe this method did not exist at the time of asking of this question)

just like getPreferredSize, there is also a setPreferredSize method which takes a Dimension object.

By default, your layout will ignore your components sizes (which you may have set using setSize), instead it will use the preferred sizes.

By using setPreferredSize, you will be able to override the default preferred sizes of the component

云淡风轻 2025-01-10 05:23:48

我希望我的回答可以在某种程度上帮助您。根据设置 JPanel 或 JFrame 大小的经验,我一直使用 setPreferredSize(new Dimension(WIDTH,HEIGHT));

I hope my answer can help you in some way. From experience with setting JPanel or JFrame size, I have always used setPreferredSize(new Dimension(WIDTH,HEIGHT));

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