JTextPane:如何设置字体大小 [英] JTextPane: How to set the font size

查看:264
本文介绍了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()也没有用。如何正确设置字体大小?请帮助..

In there, I am also trying to set the font size to 25, but as you can see, it is not working. "textPane.setFont()" also didn't work. How can I set the font size correctly? Please help..

推荐答案

当然,您可以创建一个字体对象并使用它来设置文本窗格的字体。
像这样实例化:

Sure, you can create a font object and use it to set the font of your text pane. Instantiate it like this:

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

这篇关于JTextPane:如何设置字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆