第二十八篇:JAVA观感器

本文介绍如何在Java Swing GUI应用程序中更改默认观感器皮肤。通过示例代码展示了如何获取已安装的所有观感器,并允许用户从菜单中选择不同的皮肤。

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

Java的GUI开发中,Swing组件使用的是默认的皮肤,我们可以通过设置观感器来更改默认的皮肤。 

如图所示,这就是JAVA默认观感器(XP系统)现实的效果。这一篇博客将通过示例代码为大家演示如何更改默认的观感器。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

/**
 * 观感器示例
 * 
 * @author jianggujin
 *
 */
public class LookAndFeelDemo extends JFrame implements ActionListener
{
   public LookAndFeelDemo()
   {
      super("SwingDemo");

      JMenuBar menuBar = new JMenuBar();
      JMenu skin = new JMenu("皮肤");
      // 获得所有已安装观感器
      LookAndFeelInfo[] lookAndFeelInfos = UIManager.getInstalledLookAndFeels();
      // 遍历观感器并添加菜单项
      for (LookAndFeelInfo lookAndFeelInfo : lookAndFeelInfos)
      {
         JMenuItem item = new JMenuItem(lookAndFeelInfo.getName());
         item.setActionCommand(lookAndFeelInfo.getClassName());
         item.addActionListener(this);
         skin.add(item);
      }
      menuBar.add(skin);
      setJMenuBar(menuBar);

      JPanel root = new JPanel();
      root.add(new JButton("我是JButton"));
      root.add(new JToggleButton("我是JToggleButton"));
      root.add(new JLabel("我是JLabel"));
      root.add(new JCheckBox("我是JCheckBox"));
      root.add(new JRadioButton("我是JRadioButton"));
      root.add(new JTextField("我是JTextField"));
      root.add(new JPasswordField("我是JPasswordField"));
      root.add(new JTextArea("我是JTextArea"));
      add(root);
      setSize(400, 300);
      setLocationRelativeTo(null);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      setVisible(true);
   }

   public static void main(String[] args)
   {
      new LookAndFeelDemo();
   }

   @Override
   public void actionPerformed(ActionEvent e)
   {
      JMenuItem item = (JMenuItem) e.getSource();
      try
      {
         UIManager.setLookAndFeel(item.getActionCommand());
         SwingUtilities.updateComponentTreeUI(this);
      }
      catch (Exception e2)
      {
         e2.printStackTrace();
      }

   }
}
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84

运行效果: 
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值