当前位置: 首页 > 知识库问答 >
问题:

如何为JTextPane中的文本设置删除线和下划线样式选项?

傅皓君
2023-03-14

我有一个JTextPane组件,我试图将用户键入的文本样式设置为同时下划线和删除线。

应该将下一个类型化字符的删除线属性设置为true的相关代码片段如下:

JEditorPane editor = getEditor(e);
if (editor != null) {
     StyledEditorKit kit = getStyledEditorKit(editor);
     MutableAttributeSet attr = kit.getInputAttributes();            
     SimpleAttributeSet sas = new SimpleAttributeSet();
     StyleConstants.setStrikeThrough(sas, true);                
     setCharacterAttributes(editor, sas, false);
}

这确实会将文本样式设置为删除线,但如果已经将其样式设置为下划线,则会丢失下划线样式信息。仔细查看styleConstants.setStrikeThrough(...)背后的实际代码我注意到,下划线和删除线属性的CSS样式标记都是相同的(即“text-demaily”),当在包含属性的hashtable中更新值时,它将被覆盖。

StyleConstants.setStrikeThrough(sas, true);
StyleConstants.setUnderlineThrough(sas, true);

共有1个答案

司寇正志
2023-03-14

为什么不使用styleddocument,并使用两种style:primarysecondary,其中primarysecondary的父样式:

  StyledDocument styleDocument =  jTextPane1.getStyledDocument();
  Style primaryStyle = styleDocument.addStyle("Primary", null);
  Style secondaryStyle = styleDocument.addStyle("Secondary", primaryStyle);


  StyleConstants.setFontFamily(primaryStyle, "American Captain");
  StyleConstants.setFontSize(primaryStyle, 24);

//  StyleConstants.setFontFamily(secondaryStyle, "Bira PERSONAL USE ONLY");
  StyleConstants.setFontSize(secondaryStyle, 20);
  StyleConstants.setForeground(primaryStyle, new Color(0x552AFF));
  StyleConstants.setForeground(secondaryStyle, Color.black);
  StyleConstants.setStrikeThrough(secondaryStyle, true);
  StyleConstants.setUnderline(primaryStyle, true);

  try {
      styleDocument.insertString(0, "Title with American Captain font\n\n", primaryStyle);
      styleDocument.insertString(styleDocument.getLength(), "Font demonstration with JTextPane. "
              + "Seriously, it is powerful and has the power to do all kind of styling with text. "
              + "check it out, check its mighty power and be embrassed\n", secondaryStyle);
   } catch (BadLocationException ex) {
                Logger.getLogger(JTextPaneTest.class.getName()).log(Level.SEVERE, null, ex);
   }

编辑:

假设你有4个切换按钮--斜体、粗体、下划线和删除线。每次按下或不按下其中一个时,我需要根据下一个将键入的字符的需要调整样式。

class CStyleDocument extends DefaultStyledDocument
{
    private  Style primaryStyle;

    public CStyleDocument() {
        super();
         primaryStyle = this.addStyle("Primary", null);
    }
    public Style getAttrStyle()
    {
        return primaryStyle;
    }

    @Override
    public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
        super.insertString(offs, str, primaryStyle); 

    }

}

public class JTextPaneTest extends javax.swing.JFrame {

    CStyleDocument styleDocument;

    public JTextPaneTest() {
        initComponents();

        styleDocument = new CStyleDocument();
        jTextPane1.setDocument(styleDocument);

}

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();
        jPanel1 = new javax.swing.JPanel();
        boldSelButton = new javax.swing.JToggleButton();
        ulSelButton = new javax.swing.JToggleButton();
        strkSelButton = new javax.swing.JToggleButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(400, 200));

        jScrollPane1.setViewportView(jTextPane1);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        boldSelButton.setText("Bold");
        boldSelButton.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                boldSelButtonStateChanged(evt);
            }
        });
        jPanel1.add(boldSelButton);

        ulSelButton.setText("Under Lined");
        ulSelButton.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                ulSelButtonStateChanged(evt);
            }
        });
        jPanel1.add(ulSelButton);

        strkSelButton.setText("Strike Through");
        strkSelButton.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                strkSelButtonStateChanged(evt);
            }
        });
        jPanel1.add(strkSelButton);

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_START);

        pack();
    }// </editor-fold>                        

    private void boldSelButtonStateChanged(javax.swing.event.ChangeEvent evt) {                                           
             StyleConstants.setBold(styleDocument.getAttrStyle(), ((JToggleButton)evt.getSource()).isSelected());
             jTextPane1.requestFocus();




    }                                          

    private void ulSelButtonStateChanged(javax.swing.event.ChangeEvent evt) {                                         
        StyleConstants.setUnderline(styleDocument.getAttrStyle(), ((JToggleButton)evt.getSource()).isSelected());
        jTextPane1.requestFocus();
    }                                        

    private void strkSelButtonStateChanged(javax.swing.event.ChangeEvent evt) {                                           
       StyleConstants.setStrikeThrough(styleDocument.getAttrStyle(), ((JToggleButton)evt.getSource()).isSelected());
       jTextPane1.requestFocus();
    }                                          


    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JTextPaneTest();
                frame.setVisible(true);

            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JToggleButton boldSelButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextPane jTextPane1;
    private javax.swing.JToggleButton strkSelButton;
    private javax.swing.JToggleButton ulSelButton;
    // End of variables declaration                   
}
 类似资料: