Java 包不调整大小
我现在正在学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您根本不想允许用户调整窗口大小,那么您可以这样做
我不确定该技术是否可以与
pack()
同时成功使用。你当然应该尝试,但我记得有一个问题,如果你这样做:那么
pack()
根本不会做它应该做的事情,如果你这样做:那么你的窗口会变得更窄一点仅在
pack()
之后。当然,可以找到一些解决方法(或者甚至可能只是我的糟糕经历)。If you don't want to allow users to resize your window at all then you can do
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:then
pack()
simply doesn't do what it should and if you do: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).对于第一个问题,您可以使用方法
setResizes(boolean)
:我不使用
GridBagLayout
,所以我无法帮助您解决第二个问题问题。for the first question, you can stop the user from resizing your window using the method
setResizable(boolean)
:I don't use
GridBagLayout
, so I can't help you with the second question.要防止调整窗口大小,您只需在 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.
是的,您需要创建自己的
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。Yes you need to create your own
LabelUI
class and set it to your label viasetUI()
.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.