为所有窗口管理器编写正确的 JFrame
在 Linux(这里是在不同的窗口管理器下)、Mac 和 Windows 上以相同的方式显示 Java JFrame
窗口的正确方法是什么?
实际上,我们在使用 JFrame
时遇到了一些麻烦,这是非常不一致的,因为在 Windows 上,JFrame
是最大化的,而在我的带有很棒的 WM 的 Linux 笔记本上,窗口只是被打包了,并且在 Mac 下,窗口最大化了一半,因此它比 Linux 上更大,但没有压缩...
我们尝试将屏幕设置为最大尺寸,这似乎在 Windows 下工作得很好,但在 Mac 和 Linux 下则不然(很棒的 WM )。
这是一个非常简单的框架,它解释了我们当前正在做的事情:
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JFrameMaximize extends JFrame {
private static final long serialVersionUID = 1L;
public JFrameMaximize() {
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel();
// A Fake Dimension, inside this panel are a lot of swing components
panel.setPreferredSize(new Dimension(333, 666));
setContentPane(panel);
// Now we want to set the Extended State
if (Toolkit.getDefaultToolkit().isFrameStateSupported(MAXIMIZED_BOTH)) {
setExtendedState(MAXIMIZED_BOTH);
} else {
System.out.println("Oh oh... i have a problem...");
// Now we need a Fullscreen but we dont have the size of a WindowManager Panels and
// stuff
Dimension max = Toolkit.getDefaultToolkit().getScreenSize();
// asume that the screen has a 20px bar on top (=awesome wm)
max.height -= 20;
setSize(max);
setLocation(0, 20);
// The alternative is to call pack() here, but then the window is totally broken, because of the internal components. Every Panel we load in here has a different size, so the window will scale up and down on every panel change.
}
}
public static void main(String... args) {
new JFrameMaximize();
}
}
问题是某些窗口管理器没有 Maximize_both 扩展状态,因此我们必须计算 - 这对于每台 PC 来说都是不可能知道的! 我们如何在每个窗口管理器上将窗口设置为正确的大小?
唯一的解决方案是为 JFrame 提供固定的默认大小吗?并尝试最大化窗口?
What is the correct way to display a Java JFrame
Window on Linux (and here under different Window Managers), Mac and Windows allways in the same way?
We have actually some trouble with a JFrame
, that is very inconsistent because on Windows the JFrame
is maximized, on my Linux Notebook with awesome WM, the Window is only packed, and under Mac the Window gets half maximized, so its bigger then on Linux but not packed...
We tried to set the Screen just to the Maximium Size, which seems to work very well under windows, but not under Mac and Linux (Awesome WM).
Here is a very simple Frame, which explains what we are doing currently:
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JFrameMaximize extends JFrame {
private static final long serialVersionUID = 1L;
public JFrameMaximize() {
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel();
// A Fake Dimension, inside this panel are a lot of swing components
panel.setPreferredSize(new Dimension(333, 666));
setContentPane(panel);
// Now we want to set the Extended State
if (Toolkit.getDefaultToolkit().isFrameStateSupported(MAXIMIZED_BOTH)) {
setExtendedState(MAXIMIZED_BOTH);
} else {
System.out.println("Oh oh... i have a problem...");
// Now we need a Fullscreen but we dont have the size of a WindowManager Panels and
// stuff
Dimension max = Toolkit.getDefaultToolkit().getScreenSize();
// asume that the screen has a 20px bar on top (=awesome wm)
max.height -= 20;
setSize(max);
setLocation(0, 20);
// The alternative is to call pack() here, but then the window is totally broken, because of the internal components. Every Panel we load in here has a different size, so the window will scale up and down on every panel change.
}
}
public static void main(String... args) {
new JFrameMaximize();
}
}
the problem is that some Window Managers dont have the Maximize_both extendend state, so we have to calculate - which is impossible to know for every PC!
How do we can set the window to a correct size on every window manager?
Is the only solution a fixed default size, which is given to the JFrame
? and the try to maximize the window?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一些观察:
始终从事件调度线程开始.
调用
pack()
然后调整帧大小然后执行setVisible()
。考虑使用
getMaximumWindowBounds()
:修订sscce:
Some observations:
Always start on the event dispatch thread.
Invoke
pack()
then adjust the frame size then dosetVisible()
.Consider using
getMaximumWindowBounds()
:Revised sscce: