在java swing中设置窗口的最小大小限制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2781939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting minimum size limit for a window in java swing
提问by Abhijeet Rastogi
I have a JFramewhich has 3 JPanelsin GridBagLayout..
我有一个JFrame,它在GridBagLayout 中有3 个 JPanel..
Now, when I minimize a windows, after a certain limit, the third JPanel tends to disappear. I tried setting minimizing size of JFrame using setMinimumSize(new Dimension(int,int))but no success. The windows can still be minimized.
现在,当我最小化一个窗口时,在某个限制之后,第三个 JPanel 趋于消失。我尝试使用setMinimumSize(new Dimension(int,int))设置最小化 JFrame 的大小,但没有成功。窗口仍然可以最小化。
So, I actually want to make a threshhold, that my window cannot be minimized after a certain limit.
所以,我实际上想设定一个阈值,即我的窗口在达到某个限制后无法最小化。
How can I do so?
我怎么能这样做?
Code:-
代码:-
import java.awt.Dimension;
import javax.swing.JFrame;
public class JFrameExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Hello World");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setMinimumSize(new Dimension(400, 400));
frame.setVisible(true);
}
}
Also:
还:
shadyabhi@shadyabhi-desktop:~/java$ java --showversion
java version "1.5.0"
gij (GNU libgcj) version 4.4.1
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Usage: gij [OPTION] ... CLASS [ARGS] ...
to invoke CLASS.main, or
gij -jar [OPTION] ... JARFILE [ARGS] ...
to execute a jar file
Try `gij --help' for more information.
shadyabhi@shadyabhi-desktop:~/java$
Gives me output like
给了我这样的输出
**UPDATE: ** The same when run though Netbeans IDE gives expected output.. When I run through "java JFrameExample" compiler, I am facing issues.. Now, what that means??
**更新:** 运行 Netbeans IDE 时同样会给出预期的输出。当我运行“java JFrameExample”编译器时,我遇到了问题。
采纳答案by ablaeul
The documentationtells me, that this behavior is platform dependent. Especially, since the following example code works for me as desired in Windows Vista:
该文件告诉我,这种行为是与平台相关的。特别是,由于以下示例代码在 Windows Vista 中按我的需要工作:
import java.awt.Dimension;
import javax.swing.JFrame;
public class JFrameExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Hello World");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setMinimumSize(new Dimension(100, 100));
frame.setVisible(true);
}
}
回答by Bat0u89
There actually is a way to ensure minimum size on any platform. You need to set the minimum size of the JFrame to the minimum size of its content pane and then you need to write a ComponentAdapter and override componentResized. Then you just use getSize and getMinimum size on your JFrame and substitute width and/or height with the minimum width or height if it is greater. Assuming you are extending JFrame:
实际上有一种方法可以确保任何平台上的最小尺寸。您需要将 JFrame 的最小尺寸设置为其内容窗格的最小尺寸,然后您需要编写一个 ComponentAdapter 并覆盖 componentResized。然后您只需在 JFrame 上使用 getSize 和 getMinimum size 并用最小宽度或高度替换宽度和/或高度(如果它更大)。假设您正在扩展 JFrame:
this.addComponentListener(new ComponentAdapter(){
public void componentResized(ComponentEvent e){
Dimension d=YourJFrame.this.getSize();
Dimension minD=YourJFrame.this.getMinimumSize();
if(d.width<minD.width)
d.width=minD.width;
if(d.height<minD.height)
d.height=minD.height;
YourJFrame.this.setSize(d);
}
});