开发者

Java Pack No Resize

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 ce开发者_Python百科nters the image, i would like to be able to expand/shrink the image.

thanks.


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).


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜