making text underline font by using JTextPane?

2019-08-28 01:35发布

问题:

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.

My Code is :

    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);
        }
    }

回答1:

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); 

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



回答2:

When you use a JTextPane you would generally set blocks of text to a specific Font, not the entire text pane. That is, it would typically work like a text editor like Word.

Read the section from the Swing tutorial on Text Component Features for a working example that shows you how to use the StyledEditorKit Actions to do this for you.

If your goal is to only be able to change the Font for the entire text component then you should be using a JTextArea since it is a simpler component. In this case there is no Font.UNDERLINE property that you can set. To create a Font with an underline you need code like:

Font font = textArea.getFont();
Map<TextAttribute, Object> map = new HashMap<TextAttribute, Object>();
map.put(TextAttribute.FONT, font);
map.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
font = Font.getFont(map);
textArea.setFont(font);