JLabel支持CSS

阅读更多

文章来源:(http://esus.com/applying-css-to-an-html-rendered-jlabel/)

import javax.swing.text.html.*;
import javax.swing.text.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;
    
public class Main  extends JFrame {
    public Main(StyleSheet s) {
       getContentPane().setLayout( new FlowLayout());
   
       HTMLEditorKit kit =  new HTMLEditorKit();
       kit.setStyleSheet(s);
  
       final JLabel label =  new JLabel( "

Socrates

"
);
       getContentPane().add(label);
  
       addWindowListener( new WindowAdapter() {
          public void windowClosing(WindowEvent event) {
             System.exit( 0 );  
          }     
       });
  
       setSize( 300 300 );
    }
  
    public static StyleSheet loadStyleSheet(InputStream is)  throws IOException
    {
       StyleSheet s =  new StyleSheet();
       BufferedReader br =  new BufferedReader( new InputStreamReader(is));
       s.loadRules(br,  null );
       br.close();
   
       return s;
    }
   
    public static void main(String[] args)  throws Exception {
       StyleSheet s = loadStyleSheet( new FileInputStream( "default.css" ));
  
       ( new Main(s)).show();
    }
}

你可能感兴趣的:(JLabel,CSS)