使用CSS来美化你的java桌面
在网上看到了一个CSS jar包,一个对于桌面开发很好的一个东西,看了看,现在把它写出来:帮助文档:https://javacss.dev.java.net/docs/javadoc/overview-summary.html
类库下载: https://javacss.dev.java.net/servlets/ProjectDocumentList
转个小例子,再带点说明吧:
这个东西,可以用于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文件,然后交给StylesheetStylesheet style = CSSParser.parse(MainFrame.class.getResource("style.css")); //此方法就可以对控件进行绑定style.applyTo(this);} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.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;}
页:
[1]