【问题标题】:Java Metal Look & Feel hides Windows toolbar when maximizedJava Metal Look & Feel 最大化时隐藏 Windows 工具栏
【发布时间】:2012-10-17 14:53:35
【问题描述】:

在我的应用程序中,我们使用 CrossPlatform L&F(金属)并且我们希望最大化启动主 JFrame。执行时,我发现 Windows 工具栏隐藏。如果我使用 System L&F,则不会发生这种情况。

为什么会这样?有什么办法可以避免吗?

强制Metal L&F的代码摘录是:

    try {
        UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        MetalLookAndFeel.setCurrentTheme(new OceanTheme());
        UIManager.setLookAndFeel(new MetalLookAndFeel());
    } catch (ClassNotFoundException ex) {
        code to catch this exception;
    } catch (InstantiationException ex) {
           code to catch this exception;
    } catch (IllegalAccessException ex) {
           code to catch this exception;
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
           code to catch this exception;
    }

    JFrame.setDefaultLookAndFeelDecorated(Boolean.TRUE); 

最大化窗口的方法如下:

 private void formWindowOpened(java.awt.event.WindowEvent evt) {  
    setExtendedState(JFrame.MAXIMIZED_BOTH);
    return;
}    

提前致谢

【问题讨论】:

  • 对不起,不是windows工具栏;隐藏的东西是在屏幕底部运行的 Windows 任务列表
  • 也许添加一些屏幕截图和SSCCE 会让你更快地获得结果

标签: java swing jframe look-and-feel uimanager


【解决方案1】:

调用时似乎是已知问题:

JFrame.setDefaultLookAndFeelDecorated(Boolean.TRUE);

查看此链接:http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4737788

它还通过子类化 JFrame 并返回适当的最大界限来显示一种解决方法。这是此解决方法的演示代码:

import java.awt.Frame;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.plaf.metal.OceanTheme;

public class TestJFrame {

    private void initUI() {
        final JFrame frame = new JFrame(TestJFrame.class.getSimpleName()) {
            private Rectangle maxBounds;

            @Override
            public Rectangle getMaximizedBounds() {
                return maxBounds;
            }

            @Override
            public synchronized void setMaximizedBounds(Rectangle maxBounds) {
                this.maxBounds = maxBounds;
                super.setMaximizedBounds(maxBounds);
            }

            @Override
            public synchronized void setExtendedState(int state) {
                if (maxBounds == null && (state & Frame.MAXIMIZED_BOTH) == Frame.MAXIMIZED_BOTH) {
                    Insets screenInsets = getToolkit().getScreenInsets(getGraphicsConfiguration());
                    Rectangle screenSize = getGraphicsConfiguration().getBounds();
                    Rectangle maxBounds = new Rectangle(screenInsets.left + screenSize.x, screenInsets.top + screenSize.y, screenSize.x
                            + screenSize.width - screenInsets.right - screenInsets.left, screenSize.y + screenSize.height
                            - screenInsets.bottom - screenInsets.top);
                    super.setMaximizedBounds(maxBounds);
                }

                super.setExtendedState(state);
            }
        };
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowOpened(WindowEvent e) {
                frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
            }
        });
        JLabel label = new JLabel("some label in the middle");
        label.setHorizontalAlignment(JLabel.CENTER);
        frame.add(label);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
            MetalLookAndFeel.setCurrentTheme(new OceanTheme());
            UIManager.setLookAndFeel(new MetalLookAndFeel());
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        JFrame.setDefaultLookAndFeelDecorated(Boolean.TRUE);
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestJFrame().initUI();
            }
        });
    }

}

或者,不要打电话给JFrame.setDefaultLookAndFeelDecorated(Boolean.TRUE);

【讨论】:

  • Guillaume,我已经实现了它并且工作正常。非常感谢……
猜你喜欢
  • 2010-10-08
  • 1970-01-01
  • 1970-01-01
  • 2016-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多