如何使用JFrame最大化图标

时间:2012-10-05 18:04:46

标签: java swing jframe

如何在Mac上的eclipse中创建一个JFrame窗口,该窗口的图标使窗口全屏显示在右上方大多数窗口的双箭头图标上?

1 个答案:

答案 0 :(得分:4)

看看

<强>更新

幸运的是JFrame通过Window ... {/ p>延伸Frame

enter image description here

public class TestMacFullScreen {

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

            @Override
            public void run() {

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(200, 200);
                frame.setLocationRelativeTo(null);
                frame.setLayout(new BorderLayout());

                JLabel label = new JLabel("Look ma, no hands");

                frame.add(label);

                enableOSXFullscreen(frame);

                frame.setVisible(true);


            }
        });
    }

    public static void enableOSXFullscreen(Window window) {
        try {
            Class util = Class.forName("com.apple.eawt.FullScreenUtilities");
            Class params[] = new Class[]{Window.class, Boolean.TYPE};
            Method method = util.getMethod("setWindowCanFullScreen", params);
            method.invoke(util, window, true);
        } catch (ClassNotFoundException exp) {
            exp.printStackTrace();
        } catch (Exception exp) {
            exp.printStackTrace();
        }
    }
}