使用CSS来美化你的java桌面

在网上看到了一个CSS jar包,一个对于桌面开发很好的一个东西,看了看,现在把它写出来:
帮助文档:[url]https://javacss.dev.java.net/docs/javadoc/overview-summary.html[/url]
类库下载: [url]https://javacss.dev.java.net/servlets/ProjectDocumentList [/url]

转个小例子,再带点说明吧:
这个东西,可以用于swing,不能用于 awt,swt
JFrame窗口:

package com.sunray.css.test;

import java.awt.EventQueue;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import com.sun.stylesheet.Stylesheet;
import com.sun.stylesheet.css.CSSParser;
import com.sun.stylesheet.css.ParseException;

public class MainFrame extends JFrame {

private JTextField textField;

/**
* Launch the application
*
* @param args
*/
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame
*/
public MainFrame() {
super();

try {
//从类当前目录里读取css文件,然后交给Stylesheet
Stylesheet style = CSSParser.parse(MainFrame.class
.getResource("style.css"));
//此方法就可以对控件进行绑定
style.applyTo(this);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

setTitle("一个CSS样式的例子");
getContentPane().setLayout(null);
setBounds(100, 100, 294, 196);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JButton button = new JButton();
button.setName("button");
button.setText("测试一个Button");
button.setBounds(38, 21, 166, 28);
getContentPane().add(button);

textField = new JTextField();
textField.setText("测试一个Text");
textField.setBounds(167, 76, 87, 22);
//给这个控件设置一个名称,好让javacss 可以在css中找到一个对应的名称,然后绑定.
textField.setName("anysky");
getContentPane().add(textField);

final JLabel label = new JLabel();
label.setText("测试一个lable");
label.setBounds(38, 76, 98, 18);
getContentPane().add(label);
//

}

}


CSS文件:

JFrame {
font-size: 50pt;
}
//一个name为anysky的JTextField的样式定义
JTextField#anysky{
font-size:10pt;
foreground: red;
background:green;
}

JButton#button {
font: Helvetica-BOLD-24;
font-size:60pt;
foreground:red;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值