开发者

How to make a window smaller than 125 x 50 in Java?

I'm trying to create a 50x50 window in Java but the window won't go smaller than 125x50, even if I try to manually resize it.

Here's my code currently:

import javax.swing.*;
public class smallwindow {
    public static void main(String args[]) {
        JFrame frame = new JFrame("");
        frame.setSize(开发者_Python百科50, 50);
        frame.setVisible(true);
    }
}

I am running this with the latest version of Java on Mac OS X. Is there any way to do this with a JFrame, or would I need to use something else, like maybe AWT?

**edit: is there a way to do this while retaining the titlebar, window management buttons, etc.?


You would have to do the following on the JFrame:

frame.setUndecorated(true);


Guys, I did a little more looking into it, apparently there is a method called setMinimumSize

Basically, all you need to do is add

Dimension minimumSize = new Dimension(50, 50);  
frame.setMinimumSize(minimumSize);`

I've found that if the size is less than about 75x75, then resizing it will suddenly change the minimum width to around 75. The solution is to just to do frame.setResizable(false)

But anyways, thanks for all your help!


but is there anyway to do this such that you still retain the tilebar, window management buttons, etc.?

You can use the Metal LAF which includes the title bar:

JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame(...);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜