用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

java内置的几种UI外观 更改皮肤(LookAndFeel)

2013-04-13 作者: 小蜜锋举报

[java]代码库

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

/**
 * 一个封装了LookAndFeel子菜单 可用于改变组件的LookAndFeel 这些LookAndFeel是JRE 自带的 LookAndFeel
 * 这些组件必须与一个父组件的相 关联,才可更新LookAndFeel
 * 
 * @author QIU_BaiChao
 */
public class JLookAndFeelMenu extends JMenu {

	private static final String defaultMenuName = "LookAndFeel";

	// 得到系统的安装的所有LookAndFeel
	UIManager.LookAndFeelInfo[] info = UIManager.getInstalledLookAndFeels();

	// 按钮组,将一组LookAndFeel的单选按钮放在里面
	ButtonGroup buttonGroup = new ButtonGroup();

	// 父组件,更新LookAndFeel只能更新 在同一个父组件的LookAndFeel
	Component parent;

	public JLookAndFeelMenu(String menuName, final Component parent) {
		// 生成各个可以选择LookAndFeel的单选按钮子菜单
		// generateLookAndFeelSubMenu(parent);
		setParentComponent(parent);
		// 设置菜单名
		this.setText(menuName);
	}

	public JLookAndFeelMenu(Component parent) {
		this(defaultMenuName, parent);
	}

	public JLookAndFeelMenu() {
		super(defaultMenuName);
	}

	/**
	 * 生成LookAndFeel的单选子菜单
	 * 
	 * @param parent
	 */
	private void generateLookAndFeelSubMenu(final Component parent) {
		for (int i = 0; i < info.length; i++) {
			JRadioButtonMenuItem item = new JRadioButtonMenuItem(
					info[i].getName(), i == 0);
			final String className = info[i].getClassName();
			// 增加事件处理
			item.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evn) {
					try {
						UIManager.setLookAndFeel(className);
					} catch (Exception e) {
						e.printStackTrace();
					}
					// 更新组件的LookAndFeel
					SwingUtilities.updateComponentTreeUI(parent);
				}
			});
			buttonGroup.add(item);
			add(item);
		}
	}

	public Component getParentComponent() {
		return parent;
	}

	public void setParentComponent(Component parent) {
		this.parent = parent;
		generateLookAndFeelSubMenu(parent);
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(400, 300);
		frame.setLocation(200, 200);
		JMenuBar bar = new JMenuBar();
		JLookAndFeelMenu menu = new JLookAndFeelMenu();
		menu.setText("外观选择");
		menu.setParentComponent(frame);
		bar.add(menu);
		frame.getContentPane().add(BorderLayout.NORTH, bar);
		frame.getContentPane().add(BorderLayout.CENTER, new JButton("Hello"));
		frame.setVisible(true);
	}

}

[代码运行效果截图]


java内置的几种UI外观 更改皮肤(LookAndFeel)


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...