2012-05-14 151 views
5

請看看下面的代碼的JTextPane:如何設置字體大小

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.FlowLayout; 
import java.util.ArrayList; 
import java.util.Scanner; 
import javax.swing.*; 
import javax.swing.event.DocumentEvent; 
import javax.swing.event.DocumentListener; 
import javax.swing.text.BadLocationException; 
import javax.swing.text.Style; 
import javax.swing.text.StyleConstants; 
import javax.swing.text.StyledDocument; 

public class Form extends JFrame 
{ 
    private JTextPane textPane; 
    private JLabel results; 
    private JPanel center,south; 
    private FlowLayout flow; 
    private ArrayList array; 
    private Color color; 
    private StyledDocument doc; 
    private Style style, fontSize; 



    public Form() 
    { 
     textPane = new JTextPane(); 
     textPane.setMinimumSize(new Dimension(100,100)); 

     doc = textPane.getStyledDocument(); 
     doc.addDocumentListener(new TextActions()); 


     results = new JLabel("Number of letters: "); 

     array = new ArrayList(); 
     array.add("public"); 
     array.add("static"); 
     array.add("void"); 
     array.add("private"); 
     array.add("protected"); 

     color = new Color(185,224,247); 

     //Adding styles 
     style = doc.addStyle("blue", null);  
     StyleConstants.setForeground(style, color);  


     fontSize = doc.addStyle("fontSize", null); 
     StyleConstants.setFontSize(fontSize, 25); 


     //Setting the font Size 
     doc.setCharacterAttributes(0, doc.getLength(), fontSize, false); 

     center = new JPanel(); 
     flow = new FlowLayout(); 

     center.setLayout(flow); 
     center.add(textPane); 

     south = new JPanel(); 
     south.setLayout(new FlowLayout()); 
     south.add(results); 

     getContentPane().add(textPane,"Center"); 
     getContentPane().add(south,"South"); 



    } 

    private class TextActions implements DocumentListener 
    { 
     @Override 
     public void insertUpdate(DocumentEvent e) 
     { 
      try { 
       highlighat(); 
      } catch (BadLocationException ex) { 
       ex.printStackTrace(); 
      } 
     } 

     @Override 
     public void removeUpdate(DocumentEvent e) 
     { 
      try { 
       highlighat(); 
      } catch (BadLocationException ex) { 
       ex.printStackTrace(); 
      } 
     } 

     @Override 
     public void changedUpdate(DocumentEvent e) 
     { 

     } 

    } 

     private void highlighat() throws BadLocationException 
    {  

     EventQueue.invokeLater(new Runnable()  
     {  
      public void run()  
      {  
       String text = ""; 
       String content = null; 
       try { 
        content = doc.getText(0, doc.getLength()).toLowerCase(); 
       } catch (BadLocationException ex) { 
        ex.printStackTrace(); 
       } 
      int last=0; 

      for(int i=0;i<array.size();i++) 
      { 
       text = array.get(i).toString(); 

       if(content.contains(text)) 
       { 
        while((last=content.indexOf(text,last))!=-1) 
        {      

         int end = last+text.length(); 

         doc.setCharacterAttributes(last, end, textPane.getStyle("blue"), true);  

         last++; 

        } 

       } 
      } 
      }  
     } 
    ); 
    } 

    public static void main(String[]args) 
    { 
     try 
     { 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     } 
     catch(Exception e) 
     { 

     } 
     Form f = new Form(); 
     f.setVisible(true); 
     f.setSize(800,600); 

     f.validate(); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 
} 

在那裏,我也在努力的字體大小設置爲25,但你可以看到,這是不加工。 「textPane.setFont()」也沒有工作。我怎樣才能正確設置字體大小?請幫助..

+0

'textPane.setFont(新字體( 「宋體」,Font.BOLD,22);'沒有工作 – nullpotent

+0

當然,你可以測試它,它不工作? –

回答

7

當然,你可以創建一個字體對象,並用它來設置你的文本窗格中的字體。 實例化這樣的:

Font f = new Font(Font.SANS_SERIF, 3, 5); 
+0

哇!必須說這是工作和驚人的!非常感謝! –

+2

那麼,我的提議怎麼樣沒有工作? – nullpotent

+0

你的字體有2個字符串和一個數字字體構造函數只需要一個字符串類型和一個數字我不知道如何使用特定字體粗體顯示,也許你可以應用2種字體 – loveToCode