|
上一文章说到了javaCSS,这个东西深研究后,觉得很不好,里面的东西不太健全.出现了很多问题,最后重新选择了CSSEngine这个东西.研究了一下,这个东西不错,很不错.可以让你自己定义各种样式的格式,而且很智能化,比如没有找到在css文件中定义的样式组件,它不会报错,只是不使用这个样式罢了,而且javaCSS则会报空.
好了,详细说一下这个东西的用法吧.....
先说下这个东西的官方:
http://tk-ui.sourceforge.net/user-guide/cssengine/cssengine.html
从这里开始,你可以学会它.
里面有包的下载,源码的下载,例子的代码....
然后贴个封装的代码吧:
CSSSwingResources.java
import java.io.InputStream;/** * 获取CSS文件 * */public class CSSSwingResources {/** * 获取默认样式 * */public static InputStream getDefaultStyle(){return CSSSwingResources.class.getResourceAsStream("style/default.css");}/** * 获取Matrix样式 * */public static InputStream getMaxtrixStyle(){return CSSSwingResources.class.getResourceAsStream("style/Matrix.css");}/** * 获取OSX样式 * */public static InputStream getOsxStyle(){return CSSSwingResources.class.getResourceAsStream("style/Osx.css");}/** * 获取Vista样式 * */public static InputStream getVistaStyle(){return CSSSwingResources.class.getResourceAsStream("style/Vista.css");}}
AddCss.java
这是对多个 css文件进行封装的代码:进行实例化后,可以使用addCss(Object obj,boolean style)方法,进行一个样式的启用.
import java.io.IOException;import org.akrogen.tkui.css.core.engine.CSSEngine;import org.akrogen.tkui.css.swing.engine.CSSSwingEngineImpl;/** * 为组件添加CSS样式绑定类 */public class AddCss {/** * 样式的种类 */private static final int DEFAULT_STYLE = 1;private static final int MAXTRIX_STYLE = 2;private static final int OSX_STYLE = 3;private static final int VISTA_STYLE = 4;private static CSSEngine css = new CSSSwingEngineImpl();public AddCss(int style) throws IOException {switch (style) {default:css.parseStyleSheet(CSSSwingResources.getDefaultStyle());case DEFAULT_STYLE:css.parseStyleSheet(CSSSwingResources.getDefaultStyle());case MAXTRIX_STYLE:css.parseStyleSheet(CSSSwingResources.getMaxtrixStyle());case OSX_STYLE:css.parseStyleSheet(CSSSwingResources.getOsxStyle());case VISTA_STYLE:css.parseStyleSheet(CSSSwingResources.getVistaStyle());}}/** * 打开样式表,直接整个窗口的样式变化,每一个窗口中要想使用样式,在窗口初使化后,调用此方法 * * @param obj * Object 使用样式的窗口 * @param isStyle * boolean 是否使用样式 * */public void addCss(Object obj, boolean isStyle) {css.applyStyles(obj, isStyle);}}
测试例子代码:
MainFrameTest.java测试主窗口
/** * 对于CSSEmgine的测试类 * */import java.awt.EventQueue;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.IOException;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;public class MainFrameTest {private JFrame frame;/** * Launch the application * * @param args */public static void main(String args[]) {EventQueue.invokeLater(new Runnable() {public void run() {try {MainFrameTest window = new MainFrameTest();window.frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}/** * Create the application */public MainFrameTest() {createContents();}/** * Initialize the contents of the frame */private void createContents() {// 主窗口需要绑定样式的话,就要添加以下代码AddCss css = null;try {css = new AddCss(1);} catch (IOException e1) {e1.printStackTrace();}frame = new JFrame();frame.setBounds(100, 100, 330, 170);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);final JPanel panel = new JPanel();panel.setLayout(null);frame.getContentPane().add(panel, FlowLayout.LEFT);final JButton button = new JButton();button.addActionListener(new ActionListener() {public void actionPerformed(final ActionEvent arg0) {DialogTest dialog = new DialogTest();dialog.setVisible(true);}});// 使用总的样式,不需要设置名字button.setText("使用总的样式");button.setBounds(80, 22, 145, 28);panel.add(button);// 需要单独使用一个样式,则需要设置名字,然后在css文件中单独定义一个样式final JButton button_1 = new JButton();button_1.setText("单独设置一个样式");button_1.setName("aaa");button_1.setBounds(80, 70, 145, 28);panel.add(button_1);// 开启样式css.addCss(frame, true);}}
DialogTest.java测试JDialog
import java.awt.BorderLayout;import java.awt.EventQueue;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.IOException;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JLabel;import javax.swing.JPanel;import com.jgoodies.forms.factories.DefaultComponentFactory;/** * 对方法进行使用测试 * * @author 陆健美 */public class DialogTest extends JDialog {/** * Launch the application * * @param args */public static void main(String args[]) {EventQueue.invokeLater(new Runnable() {public void run() {try {DialogTest dialog = new DialogTest();dialog.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}});dialog.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}/** * Create the dialog */public DialogTest() {super();setBounds(100, 100, 278, 178);// JDialog需要绑定的话,也要对AddCss进行实例化AddCss css = null;try {css = new AddCss(1);} catch (IOException e1) {e1.printStackTrace();}final JPanel panel = new JPanel();panel.setLayout(null);getContentPane().add(panel, BorderLayout.CENTER);final JButton button = new JButton();button.addActionListener(new ActionListener() {public void actionPerformed(final ActionEvent e) {}});button.setText("测试一个组件");// 为组设置一个名称,就可以对其进行绑定了button.setName("button");button.setBounds(54, 26, 158, 28);panel.add(button);final JLabel label = DefaultComponentFactory.getInstance().createLabel("测试一个label");label.setBounds(69, 71, 101, 18);panel.add(label);//// 开启样式css.addCss(this, true);}}
再附上使用说明,也就全了
CSS Engine使用说明:使用CSSEngine 需要做三个工作:第一个,需要在每个要绑定样式的组件上,使用setName("你的组件的名字"),或者putClientProperty("class", "你定义的样式class");第二个,需要在相关的default.css(这里最好是每个样式文件都写上)文件中,添加你的组件与名字: eg: JLabel#aaa{ 可由美工设计样式具体内容 } 第三,在所用的窗口中,使用如下代码: 窗口初使化前添加:AddCss css = new AddCss(int style)来初使化哪个样式文件,这个style可以在主窗口中,使用一个全局变量,来整体控制. 窗口初使化后添加:css.addCssByFile(Object obj,boolean isStyle)来启用样式.传入窗口(JFrame,JDialog) * <li>对于这里的style样式的书写格式: (1) * <li>组件使用setName()设置唯一的名字(aaa,bbb), 如果不添加(#aaa)的话,用于窗口中所有JLabel * <li>JLabel#aaa {color:red;} JTextField#bbb{background-color:green;} (2) * <li>当组件使用putClientProperty("class", "blueClass");方法时 * <li>.blueClass {color:blue;} .greenClass {color:green;} 使用时,它只对绑定的窗口有用,没有绑定的窗口不起作用.在css文件中定义的,找到名字的有用,没有找到名字的,有全局的,使用全局,没有全局,不起作用.
最后说明,它还有很多种样式的使用方式,不会低于html中的css,很强大,有兴趣的人,可以好好研究研究啊...这里只起个抛砖引玉的作用..
有问题的话,欢迎拍砖啊
再为不喜欢写代码的人,贴个源码吧. |
|