为所有Window Manager编写正确的JFrame

时间:2012-01-18 10:06:18

标签: java swing jframe window-managers

在Linux上(以及在不同的窗口管理器下),Mac和Windows以相同的方式显示Java JFrame窗口的正确方法是什么?

我们实际上遇到了JFrame的问题,这是非常不一致的,因为在Windows上JFrame已经最大化了,在我的Linux笔记本上有很棒的WM,窗口只是打包,而在Mac下窗口得到了一半最大化,所以它在Linux上更大但没有打包......

我们尝试将屏幕设置为Maximium Size,这在Windows下似乎运行良好,但不适用于Mac和Linux(Awesome 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 extendend状态,所以我们必须计算 - 这对每台PC都是不可能知道的! 我们如何在每个窗口管理器上将窗口设置为正确的大小?

唯一的解决方案是固定的默认大小,是JFrame吗?并尝试最大化窗口?

1 个答案:

答案 0 :(得分:1)

一些观察结果:

  • 始终从event dispatch thread

  • 开始
  • 调用pack() 然后调整框架尺寸然后执行setVisible()

  • 考虑使用getMaximumWindowBounds()

    GraphicsEnvironment env =
        GraphicsEnvironment.getLocalGraphicsEnvironment();
    Rectangle bounds = env.getMaximumWindowBounds();
    

修订sscce

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class JFrameMaximize extends JFrame {

    public JFrameMaximize() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        // A fake panel representing many swing components
        panel.setPreferredSize(new Dimension(333, 666));
        panel.setBorder(BorderFactory.createLineBorder(Color.blue, 10));
        this.add(panel);
        this.pack();
        // Now we want to set the Extended State, if supported
        if (Toolkit.getDefaultToolkit().isFrameStateSupported(MAXIMIZED_BOTH)) {
            System.out.println("Frame state supported.");
            this.setExtendedState(MAXIMIZED_BOTH);
        } else {
            System.out.println("Frame state not supported.");
            Dimension max = Toolkit.getDefaultToolkit().getScreenSize();
            // TODO see getMaximumWindowBounds()
            max.height -= 20;
            this.setSize(max);
            this.setLocation(0, 20);
        }
        this.setVisible(true);
    }

    public static void main(String... args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new JFrameMaximize();
            }
        });
    }
}