统一设置字体和颜色简单实现换肤功能

通过统一设置界面中所有组件的字体和颜色可以简单的实现“换肤”功能。

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Font;

public class GuiTool {
/**
* 统一设置字体和颜色
* @param Comp 组件(容器)
* @param font 字体
* @param back 背景色
* @param fore 前景色
*/
public static void setGlobalFontAndColor(Component Comp,Font font,Color back,Color fore) {
Comp.setFont(font);
Comp.setBackground(back);
Comp.setForeground(fore);
if (Comp instanceof Container) {
Component[] components = ((Container)Comp).getComponents();
for (int i = 0; i < components.length; i++) {
Component child = components[i];
setGlobalFontAndColor(child,font,back,fore);
}
}
}

public static Font getFont(String s_type,String s_style,int size){
int tyle = Font.PLAIN;
if(s_style.equals("Blod")){
tyle = Font.BOLD;
}else if(s_style.equals("Italic")){
tyle = Font.ITALIC;
}
return new Font(s_type,tyle,size);
}

public static Color[] getColor(String colorze){

Color[] colors = new Color[2];
colors[0] = Color.BLACK;
colors[1] = Color.WHITE;
if(colorze!=null){
if(colorze.equals("黑-红")){
colors[0] = Color.BLACK;
colors[1] = Color.RED;
}
if(colorze.equals("灰-绿")){
colors[0] = Color.LIGHT_GRAY;
colors[1] = Color.GREEN;
}
if(colorze.equals("粉-蓝")){
colors[0] = Color.PINK;
colors[1] = Color.BLUE;
}
if(colorze.equals("橙-紫")){
colors[0] = Color.ORANGE;
colors[1] = Color.MAGENTA;
}
if(colorze.equals("青-黄")){
colors[0] = Color.CYAN;
colors[1] = Color.YELLOW;
}
}
return colors;
}
}

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

public class ThemeStyle extends JFrame{
private JPanel p1,p2;
private JLabel l1,l2,l3;
private JComboBox type,style,size,color;
private JCheckBox reset;
private JScrollPane sp;
private JTextArea ta;
private JButton b;
public ThemeStyle() {
init();
display();
this.setTitle("Java简单实现换肤功能");
this.setLocationRelativeTo(null);//窗口居中
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void init(){
String[] s_type = {"宋体","Serif","Dialog",};
String[] s_style = {"Plain","Blod","Italic"};
String[] s_size = {"9","10","11","12","13","14","15"};
String[] s_color = {"黑-白","黑-红","灰-绿","粉-蓝","橙-紫","青-黄"};
p1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
p2 = new JPanel();
l1 = new JLabel("设置字体:");
l2 = new JLabel("设置颜色:");
reset = new JCheckBox("窗口自适");
reset.setToolTipText("窗口大小自动适应");
type = new JComboBox(s_type);
style = new JComboBox(s_style);
size = new JComboBox(s_size);
size.setSelectedItem("12");
color = new JComboBox(s_color);
color.setToolTipText("背景色-前景色");
size.setMaximumRowCount(5);
ta = new JTextArea();
sp = new JScrollPane(ta);
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
b = new JButton("OK");
l3 = new JLabel("<html><hr>Developed by Kinganpo in Shanghai.2011-01-17 <hr></html>");
p1.add(l1);
p1.add(type);
p1.add(style);
p1.add(size);
p1.add(l2);
p1.add(color);
p1.add(reset);
p1.add(b);
p2.add(l3);
add(p1,BorderLayout.NORTH);
add(sp,BorderLayout.CENTER);
add(p2,BorderLayout.SOUTH);
ta.setText("利用统一改变界面所有组件的字体及颜色来实现简单的换肤功能!\n" +
"setGlobalFontAndColor(Component Comp,\n" +
"Font font,Color back,Color fore)\n" +
"遍历组件进行统一设置\n" +
"再setVisible(true)显示出来!\n" +
"================================\n\n");
ta.setLineWrap(true);
ta.setEditable(false);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
display();
}
});
}

public void display(){
this.dispose();
String s_type = (String)type.getSelectedItem();
String s_style = (String)style.getSelectedItem();
int s_size = Integer.parseInt((String)size.getSelectedItem());
Font font = GuiTool.getFont(s_type, s_style, s_size);
Color[] colors = GuiTool.getColor((String)color.getSelectedItem());

GuiTool.setGlobalFontAndColor(this,font,colors[0],colors[1]);

if(reset.isSelected()){
this.pack();
}else{
this.setSize(600,450);
}
ta.append(font.toString()+"\n");
ta.append(colors[0].toString()+"\n");
ta.append(colors[1].toString()+"\n\n");
this.setVisible(true);
}

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


[img]http://dl.iteye.com/upload/attachment/393565/c6e346c4-4c80-3b4a-ae27-c89948e33109.png[/img]

[img]http://dl.iteye.com/upload/attachment/393567/f6cfd819-cba9-32ef-9c2b-782fdea5eefe.png[/img]

虽然不是真正意义上的换肤,但是效果自认为还是可以的。今天找了几个swing风格包,突发奇想就弄了这个简单的。代码简单明了留着纪念啦!(*^__^*) 嘻嘻……
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值