java - Windows 7下无法修改SystemLookAndFeel

标签 java swing windows-7

我在 Windows 7 下的 Swing SystemLookAndFeel 遇到了一个微妙的问题。下面的小程序设置 SystemLookAndFeel,然后修改 MenuBar 和 MenuItem 的背景颜色。这在 Windows XP 上运行良好,在激活了 Windows 经典主题的 Windows 7 上也运行良好。但它对 Windows 7 标准主题没有影响。有人对此有解释吗?

问候,马丁。

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JApplet;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

@SuppressWarnings("serial")
public class Win7TestApplet extends JApplet {

    public void init() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            UIManager.put("MenuBar.background", Color.decode( "#efecea" ));
            UIManager.put("MenuItem.background", Color.decode( "#9999ff" ));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }

        // Setup panel
        JPanel mainPanel = new JPanel();
        mainPanel.setBackground( Color.white );
        mainPanel.setLayout( new BorderLayout() );
        mainPanel.setOpaque( true );
        this.getContentPane().add( mainPanel, BorderLayout.CENTER );

        // Create menubar
        JMenuBar menuBar = new JMenuBar();
        getContentPane().add(menuBar, BorderLayout.NORTH);

        // Add menu
        JMenu m_file = new JMenu( "File" );
        menuBar.add( m_file );

        // Add menu items
        m_file.add( new JMenuItem( "First item" ) );
        m_file.add( new JMenuItem( "Second item" ) );
    }

    public void start() {}
    public void stop() {}
    public void destroy() {}
}

最佳答案

Martin 你可以用这个代替

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");

关于java - Windows 7下无法修改SystemLookAndFeel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3476834/

相关文章:

java.lang.NoClassDefFoundError 安卓工作室

java - 我如何残酷无情地中止 Java 中的任务?

ruby-on-rails - 无法在 Windows 上的 Rails 中安装 libv8 3.11.8.17

visual-studio - 有人在 Linux 主机上的 Windows 7 VM 上成功使用 Visual Studio/IIS/SQL Server 进行工作吗?

python - rpy2 的 R_HOME 错误

java - ButtonClickListener 无法转换为 OnClickListener

java - 字节码指令和处理器操作之间的关系

java - 如何在Java中更新LookAndFeel?

Java Swing 错误

java - 选择被格式化程序覆盖