使用JTextPane制作文本下划线字体? [英] making text underline font by using JTextPane?

查看:104
本文介绍了使用JTextPane制作文本下划线字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 fontlist 的类,并且我想使用 JTextPane 来制作带下划线的字体,但是我发现很难做到这一点. 粗体或斜体都可以正常工作,但是当我添加下划线代码时,它给了我一些错误.

I have a class with the name of fontlist and I want to make a underline font with the use of JTextPane but I find some difficulty to get this. Both bold or italic works properly but when i added underline code then its give me some error.

我的代码是:

    import java.awt.*;
    import java.awt.font.TextAttribute;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.*;
    import java.io.*;
    import java.text.*;
    import javax.swing.text.*;
    import javax.swing.event.*;
    import javax.swing.plaf.basic.BasicTextPaneUI;


class fontlist extends JFrame implements ItemListener,ActionListener
    { 
        JComboBox jcb,fontSize;
        Container content;
        JTextPane jta;
        JScrollPane jsp;
        JToggleButton bold,italic,underline;
        Font font;
        private static final int[] fontsize = {8,9,10,11,12,14,16,18,20,22,24,26,28,36,48,72};
        fontlist()
        {
            content=getContentPane();
            setLayout(null);
            setBackground(Color.WHITE);
            jcb=new JComboBox();
            content.add(jcb);
            jcb.setBounds(100,100,100,20);
            fontSize=new JComboBox();
            content.add(fontSize);
            fontSize.setBounds(200,100,40,20);
            bold=new JToggleButton("B");
            content.add(bold);
            bold.setBounds(240,100,45,22);
            italic=new JToggleButton("I");
            content.add(italic);
            italic.setBounds(285,100,45,22);
            underline=new JToggleButton("U");
            content.add(underline);
            underline.setBounds(330,100,45,22);
            jta=new JTextPane();
            jsp=new JScrollPane(jta);
            content.add(jsp);
            jsp.setBounds(100,120,500,200);
            jcb.addItemListener(this);
            fontSize.addItemListener(this);
            bold.addActionListener(this);
            italic.addActionListener(this);
            underline.addActionListener(this);
            String fonts[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
                for ( int i = 0; i < fonts.length; i++ )
                {
                  jcb.addItem(fonts[i]);
                }
                for ( int j = 0; j <16; j++ )
                {
                  fontSize.addItem(fontsize[j]);
                }
        }
        public void itemStateChanged(ItemEvent ie)
        {
         try
         {
            if (ie.getStateChange() == ItemEvent.SELECTED)
            {
                int size = new Integer(fontSize.getSelectedItem()+"");
                font = new Font(jcb.getSelectedItem().toString(),Font.PLAIN,size);
                jta.setFont(font);

            }
         }
         catch(NumberFormatException e){}
        }
        public void actionPerformed(ActionEvent ae)
        {

           int size = new Integer(fontSize.getSelectedItem()+"");
            if (bold.isSelected() && italic.isSelected() && underline.isSelected() )
            {

                    font = new Font(jcb.getSelectedItem().toString(),Font.BOLD+Font.ITALIC,size);
                    jta.setFont(font);
             }
            else if(bold.isSelected() && italic.isSelected())
            {
             font = new Font(jcb.getSelectedItem().toString(),Font.BOLD+Font.ITALIC,size);
              jta.setFont(font);
            }
            else if(bold.isSelected())
            {
                font = new Font(jcb.getSelectedItem().toString(),Font.BOLD,size);
              jta.setFont(font);
            }
            else if(italic.isSelected())
            {
                font = new Font(jcb.getSelectedItem().toString(),Font.ITALIC,size);
                jta.setFont(font);
            }
            else if(underline.isSelected())
            {
                 font = new Font(jcb.getSelectedItem().toString(),Font.UNDERLINE,size);
                jta.setFont(font);
            }
            else
            {
              font = new Font(jcb.getSelectedItem().toString(),Font.PLAIN,size);
              jta.setFont(font);
            }
        }
        public static void main(String args[])
        {
            fontlist fl=new fontlist();
            fl.setSize(700,500);
            fl.setVisible(true);
        }
    }

推荐答案

由于使用的是JTextPane,因此应使用

Since you are using JTextPane, you should use SimpleAttributeSet:

SimpleAttributeSet attributeSet = new SimpleAttributeSet();
StyleConstants.setUnderline(attributeSet, true);
jta.getStyledDocument().setCharacterAttributes(0, jta.getText().length(),
    attributeSet, false); 

在切换U按钮时,您必须将下划线样式设置为false.

You will have to set underline style to false, when the U button is toggled.

这篇关于使用JTextPane制作文本下划线字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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