为什么这个 JPanel 不坚持指定的大小?

发布于 2024-09-26 10:23:50 字数 482 浏览 0 评论 0原文

所以,我正在尝试学习 Java Swing 和自定义组件。我创建了一个 JFrame,为其指定了背景颜色,并添加了一个 JPanel:

    JFrame frame = new JFrame("Testing");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(1000, 2000);
    frame.setBackground(Color.WHITE);

    JPanel jp = new JPanel();
    jp.setBackground(Color.BLUE);
    jp.setSize(40, 40);
    frame.add(jp);

    frame.setVisible(true);

结果是一个蓝色的 1000x2000 窗口(而不是一个内部有 40x40 蓝色框的白色窗口)。为什么 JPanel 会超出其指定大小?

So, I'm trying to learn Java Swing and custom components. I've created a JFrame, given it a background color, and added a JPanel:

    JFrame frame = new JFrame("Testing");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(1000, 2000);
    frame.setBackground(Color.WHITE);

    JPanel jp = new JPanel();
    jp.setBackground(Color.BLUE);
    jp.setSize(40, 40);
    frame.add(jp);

    frame.setVisible(true);

The result is a 1000x2000 window colored blue (as opposed to a white window with a 40x40 blue box inside it). Why is the JPanel expanding beyond its specified size?

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

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

发布评论

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

评论(2

白龙吟 2024-10-03 10:23:53

JFrame 的默认布局管理器是 边框布局。当您将组件添加到没有约束的框架时,它将使用 BorderLayout.CENTER 作为默认约束。这意味着组件会占用所有可用空间,无论其请求的大小如何。

The default layout manager for a JFrame is BorderLayout. When you add a component to the frame without constraints, it uses BorderLayout.CENTER as the default constraint. This means the component takes up all available space, regardless of its requested size.

十年九夏 2024-10-03 10:23:52

使用您的代码,只需添加一行即可更改 JFrameLayoutManager。然后,当您添加组件时,它将保持其首选大小。

另外,不要调用 jp.setSize(40,40),而是调用 jp.setPreferredSize(new Dimension(40,40))。

并且,您需要在 JFrame 上调用 pack() 来告诉它布局其组件。

JFrame frame = new JFrame("Testing");
frame.setLayout(new FlowLayout()); // New line of code
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(1000, 2000));  // Modified line of code
frame.setBackground(Color.WHITE);

JPanel jp = new JPanel();
jp.setBackground(Color.BLUE);
jp.setPreferredSize(new Dimension(40, 40)); // Modified line of code
frame.add(jp);

frame.pack(); // added line of code
frame.setVisible(true);

另外,您应该阅读所有可用的不同 LayoutManager这里是一个很棒的教程。

Using your code, simply add one line to change the LayoutManager of the JFrame. Then, when you add the component, it will keep it's preferred size.

Also, instead of calling jp.setSize(40,40), call jp.setPreferredSize(new Dimension(40,40)).

And, you need to call pack() on the JFrame to tell it to layout its components.

JFrame frame = new JFrame("Testing");
frame.setLayout(new FlowLayout()); // New line of code
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(1000, 2000));  // Modified line of code
frame.setBackground(Color.WHITE);

JPanel jp = new JPanel();
jp.setBackground(Color.BLUE);
jp.setPreferredSize(new Dimension(40, 40)); // Modified line of code
frame.add(jp);

frame.pack(); // added line of code
frame.setVisible(true);

Also, you should read up on all of the different LayoutManagers available to you. Here is a great tutorial.

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