Windows 7下无法修改SystemLookAndFeel
我在 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() {}
}
I'm experiencing a subtle issue with the Swing SystemLookAndFeel under Windows 7. The applet below sets the SystemLookAndFeel and then modifies the background colour of MenuBar and MenuItem. This works perfectly well with Windows XP and it works also well with Windows 7 having the Windows Classic theme activated. But it has no effect with the Windows 7 standard theme. Does anyone have an explanation for it?
Regards, Martin.
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() {}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
马丁,你可以用这个代替
Martin you can use this instead
Windows 7 可以使用
NimbusLookAndFeel
,它有自己的默认值和一种不同的方式定义颜色。附录:如果没有,您可能需要指定
ColorUIResource
,例如Windows 7 may use the
NimbusLookAndFeel
, which has its own defaults and a different way to define colors.Addendum: If not, you may need to specify a
ColorUIResource
, for example我的问题在 Oracle Java 论坛 中得到了解答:
这似乎是太正确了。
马丁.
I got my question answered in the Oracle Java Forum:
It seems to be just too right.
Martin.