java 使用 JPanel 的大小来调整组件的大小

发布于 2024-11-16 19:30:58 字数 1057 浏览 5 评论 0原文

是否可以使用 JPanel 的大小来设置 JPanel 内组件的大小?当我尝试在 JPanel 上使用 getHeight() 或 getWidth() 时,它总是返回 0。我知道一旦 JFrame 被打包,它就会获取它的大小,但是如何使用 JPanel 的尺寸并将其应用到里面有组件吗?像这样的

JPanel panel = new JPanel();
JLabel label = new JLabel();
label.setWidth(panel.getWidth());
panel.add(label);

编辑: 请参阅下面的示例代码。如果我希望我的 Jlabel 与 JPanel 一样宽,我该怎么办?在这种情况下使用boxlayout是错误的吗?

public class Main extends JFrame{

public Main(){
    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
    panel.setBorder(BorderFactory.createLineBorder(Color.blue));

    JLabel label = new JLabel();
    label.setBorder(BorderFactory.createLineBorder(Color.red));
    label.setText("label1");
    label.setMinimumSize(panel.getPreferredSize());
    label.setPreferredSize(panel.getPreferredSize());
    panel.add(label);


    add(panel);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();
    setSize(500,500);
    setVisible(true);
}

public static void main(String[] args)
{
    new Main();
}

}

Is it possible to use the size of a JPanel to set the size of components inside the JPanel? When I try to use getHeight() or getWidth() on the JPanel it always returns 0. I know that it gets it's size once the JFrame is packed, but how would one go about using the dimensions of the JPanel and applying it to a component inside it? Something like this

JPanel panel = new JPanel();
JLabel label = new JLabel();
label.setWidth(panel.getWidth());
panel.add(label);

EDIT:
See sample code below. What should I do if I want my Jlabel to be as wide as my JPanel? Is it wrong to use boxlayout in this case?

public class Main extends JFrame{

public Main(){
    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
    panel.setBorder(BorderFactory.createLineBorder(Color.blue));

    JLabel label = new JLabel();
    label.setBorder(BorderFactory.createLineBorder(Color.red));
    label.setText("label1");
    label.setMinimumSize(panel.getPreferredSize());
    label.setPreferredSize(panel.getPreferredSize());
    panel.add(label);


    add(panel);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();
    setSize(500,500);
    setVisible(true);
}

public static void main(String[] args)
{
    new Main();
}

}

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

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

发布评论

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

评论(3

想挽留 2024-11-23 19:30:58

是否可以使用 JPanel 的大小来设置 JPanel 内组件的大小?

这是布局管理器的工作。

当我尝试在 JPanel 上使用 getHeight() 或 getWidth() 时,它总是返回 0

当调用布局管理器时,容器将具有有效的大小,以便布局管理器可以正确完成其工作。

您没有理由玩弄尺寸。将其留给布局经理来完成他们的工作。

更新:

如果我希望我的 Jlabel 与 JPanel 一样宽,我该怎么办?在这种情况下使用boxlayout是不是错误?

BoxLayout 尝试遵守组件的最小/最大尺寸。在您的情况下,您应该能够执行以下操作:

JLabel label new JLabel("some text");
label.setBorder(....);
Dimension d = label.getPreferredSize();
d.width = 32767;
label.setMaximumSize( d );

或者也许更简单的方法是从 BorderLayout 开始。您可以将标签添加到 NORTH。然后创建另一个面板并将其添加到 CENTER。

Is it possible to use the size of a JPanel to set the size of components inside the JPanel?

This is the job of the layout manager.

When I try to use getHeight() or getWidth() on the JPanel it always returns 0

When the layout manager is invoked the Container will have a valid size so the layout manager can do its job properly.

There is no reason for you to be playing with sizes. Leave it to the layout managers to do their jobs.

Update:

What should I do if I want my Jlabel to be as wide as my JPanel? Is it wrong to use boxlayout in this case?

The BoxLayout attempts to respect the minimum/maximum size of the component. In you case you should be able to do something like:

JLabel label new JLabel("some text");
label.setBorder(....);
Dimension d = label.getPreferredSize();
d.width = 32767;
label.setMaximumSize( d );

Or maybe a simpler approach is to start with a BorderLayout. You can add the label to the NORTH. Then create another panel and add it to the CENTER.

﹎☆浅夏丿初晴 2024-11-23 19:30:58

当然可以,但请注意结果取决于 JPanel 的布局管理器及其子组件的数量。

Of course you can, but be careful that the result depends on the layout manager of your JPanel and on the number of its child components.

情徒 2024-11-23 19:30:58

设置宽度会忽略调整大小。布局管理器通常会询问组件的首选宽度和高度,并扩展/收缩项目以在调整框架大小后保持视觉上吸引人的位置。因此,您最好的选择是利用布局管理器并报告“首选宽度”。

您可以使用setPreferredWidth(...);但是,如果您的首选宽度是在程序运行过程中发生变化(由于窗口大小变化),您将需要监听有问题的面板,并在面板的首选宽度发生变化时更新按钮的首选宽度(假设它发生了变化) ,这可能不是真的)。

Setting the width ignores resizing. LayoutManagers typically ask components for their preferred widths and heights, and expand / contract items to maintain a visually appealing position after the frame has been resized. So, your best option is to leverage the layout manager and report a "preferred width".

You can use setPreferredWidth(...); but, if your preferred width is to change over the run of the program (due to window size changes), you will need to listen to the panel in question and update your button's preferred width as the panel's preferred width changes (this assumes it changes, which might not be true).

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