Java 包不调整大小

发布于 2024-08-29 06:01:57 字数 363 浏览 5 评论 0原文

我现在正在学习 Java,有以下问题:

我将控件添加到 JFrame 中,然后在显示之前添加 pack() 。

这会运行应用程序,一切都非常好。

我想知道有没有办法阻止用户调整应用程序窗口的大小?

还有一种方法可以让 JLabel 中的图像随着用户更改应用程序窗口而扩展?

目前我将其设置为:

    constraints.fill = GridBagConstraints.BOTH;
    constraints.anchor = GridBagConstraints.CENTER;

并且它仅将图像居中,我希望能够扩大/缩小图像。

谢谢。

i am learning Java at the moment and have the following question:

i am adding my controls to JFrame and then pack() before displaying.

this runs the application and all is very nice.

i was wondering is there a way to stop the user from resizing the application window?

also is there a way to for the image in JLabel to expand as the user changes the application window?

at the moment i have it as:

    constraints.fill = GridBagConstraints.BOTH;
    constraints.anchor = GridBagConstraints.CENTER;

and it only centers the image, i would like to be able to expand/shrink the image.

thanks.

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

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

发布评论

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

评论(4

北陌 2024-09-05 06:01:57

我想知道有没有办法阻止
用户无法调整应用程序的大小
窗口?

如果您根本不想允许用户调整窗口大小,那么您可以这样做

frame.setResizable (false);


我不确定该技术是否可以与 pack() 同时成功使用。你当然应该尝试,但我记得有一个问题,如果你这样做:

frame.setResizable (false);
pack();

那么 pack() 根本不会做它应该做的事情,如果你这样做:

pack();
frame.setResizable(false);

那么你的窗口会变得更窄一点仅在 pack() 之后。当然,可以找到一些解决方法(或者甚至可能只是我的糟糕经历)。

i was wondering is there a way to stop
the user from resizing the application
window?

If you don't want to allow users to resize your window at all then you can do

frame.setResizable (false);


I'm not sure that this technique can be successfully used at the same time with pack(). You should certainly try, but I remember there was an issue that if you do:

frame.setResizable (false);
pack();

then pack() simply doesn't do what it should and if you do:

pack();
frame.setResizable(false);

then you window becomes a little more narrow then after only pack(). Of course, some workaround can be found (or maybe even it's just my bad experience).

长梦不多时 2024-09-05 06:01:57

对于第一个问题,您可以使用方法 setResizes(boolean)

frame.setResizable(false);

我不使用 GridBagLayout,所以我无法帮助您解决第二个问题问题。

for the first question, you can stop the user from resizing your window using the method setResizable(boolean):

frame.setResizable(false);

I don't use GridBagLayout, so I can't help you with the second question.

拧巴小姐 2024-09-05 06:01:57

要防止调整窗口大小,您只需在 Window 或 (J)Frame 上调用 setResizable(false) 即可。

对于JLabel和图像,没有简单的方法可以使其根据实际大小缩放图像。如果您仅将标签用于图像(无文本),那么您可能更容易实现自己的组件,在其中自己绘制图像。

To prevent resizing the window, you can simply call setResizable(false) on your Window or (J)Frame.

As to the JLabel and image, there is no easy way to make it scale the image according to it's actual size. If you use the label just for the image (no text), it's probably easier for you to implement your own component, in which you paint the image yourself.

小嗷兮 2024-09-05 06:01:57
also is there a way to for the image in JLabel to expand as the user changes the application window?

是的,您需要创建自己的 LabelUI 类并通过 setUI() 将其设置为您的标签。
在您的自定义 LabelUI 类中,您必须重写 getPreferredSize 并根据需要绘制 imageIcon。

它有点程序化,您可能需要查看 BasicLabelUI 的源代码以了解如何完成工作。( http://www.java2s.com/Open-Source/Java -Document/Apache-Harmony-Java-SE/javax-package/javax/swing/plaf/basic/BasicLabelUI.java.htm

或者

您可以覆盖 paintComponent() API通过创建您自己的扩展 JLabel 类来使用 JLabel。

also is there a way to for the image in JLabel to expand as the user changes the application window?

Yes you need to create your own LabelUI class and set it to your label via setUI().
In your custom LabelUI class, you will have to override getPreferredSize and paint the imageIcon as you wish.

It is slightly programmatic, you may want to look at source of BasicLabelUI to understand how things are done.( http://www.java2s.com/Open-Source/Java-Document/Apache-Harmony-Java-SE/javax-package/javax/swing/plaf/basic/BasicLabelUI.java.htm )

or

you could over-ride paintComponent() API of JLabel by creating your own extended JLabel class.

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