改变Swing的LookAndFeel

本文介绍如何正确地在Swing应用程序中更改用户界面的外观,包括使用正确的步骤来实现WindowsLookAndFeel,并强调了setLookAndFeel方法与updateComponentTreeUI方法结合使用的必要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

要改变Swing默认的LookAndFeel,网上都说用UIManager下的一个静态方法setLookAndFeel即可,但是我用了这个方法有半年的时间也没有看到真正的WindowsLookAndFeel。昨天网上无意中才看到正解,要设置LookAndFeel,不仅要调用上面提到的方法,还要调用一个SwingUtilities类中的静态方法updateComponentTreeUI。即

try{
    javax.swing.UIManager.setLookAndFeel(new com.sun.java.swing.plaf.windows.WindowsLookAndFeel());
    javax.swing.SwingUtilities.updateComponentTreeUI(this);
}catch(javax.swing.UnsupportedLookAndFeelException e){
    e.printStackTrace();
}


后者在运行时对整个ComponentTree进行更新,应用当前的UI设置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值