1

我遇到了 JTextPane 的问题。我正在使用的代码设置了许多字体属性,例如 BOLD、ITALIC 等。但是如果初始文本以单个换行符结尾,并且用户单击最后一行,或者被发送到最后一行,则用户键入的任何其他文本都会显示默认字体设置。

具体来说,此文本按我的预期工作:jTextPane.setText(String.format("Test This"));。

此文本没有:
jTextPane.setText(String.format("Test This%n%n%n"));

我认为 JTextPane 可能会认为这是一个新段落。如果是这样,我想要么

a.) 知道如何设置适用于整个 JTextPane 实例段落的通用字体。

或者

b.) 告诉 JTextPane 实例将其所有可编辑区域视为一个段落。

这是一个玩具程序,向您展示我的意思。如果您运行它,并在文本末尾开始输入,则字体将是您的 Swing 实现的默认字体。我还尝试设置 JTextPane 的文档模型并在 JTextPane 构造函数中使用 Font 实例。结果是一样的。

另一种选择是改用 JTextArea 实例,但这是非常复杂的代码,我不愿做出可能会破坏应用程序其他区域而不是我正在工作的区域的更改。

import javax.swing.*;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import java.awt.*;

public class JTextPaneExampleOne {
    public static void main(String args[]) {
        JFrame frame = new JFrame("JTextPane Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container cp = frame.getContentPane();
        JTextPane pane = new JTextPane();

        String welcomeString = String.format("Welcome%n%n%nStranger!%n%n%n");
        pane.setText( welcomeString );

        pane.invalidate();
        SimpleAttributeSet attributeSet = new SimpleAttributeSet();
        StyleConstants.setBold(attributeSet, true);
        StyleConstants.setItalic(attributeSet, true);
        StyleConstants.setForeground(attributeSet, Color.red);

        pane.setSelectionStart( 0 );
        pane.setSelectionEnd( pane.getText().length() );
        pane.setParagraphAttributes( attributeSet, true );

        pane.setSelectionStart( pane.getText().length() );

        pane.validate();

        JScrollPane scrollPane = new JScrollPane(pane);
        cp.add(scrollPane, BorderLayout.CENTER);

        frame.setSize(400, 300);
        frame.setVisible(true);
    }
}
4

1 回答 1

0

a.) 知道如何设置适用于整个 JTextPane 实例段落的通用字体。

在这种情况下,您也许可以使用JTextPane#setLogicalStyle(Style)

import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;

public class JTextPaneExampleOne2 {
  public Component makeUI() {
    JTextPane pane = new JTextPane();

    Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
    Style body = pane.getStyledDocument().addStyle("body", def);
    StyleConstants.setBold(body, true);
    StyleConstants.setItalic(body, true);
    StyleConstants.setForeground(body, Color.RED);
    pane.setLogicalStyle(body);

    String welcomeString = String.format("Welcome%n%n%nStranger!%n%n%n");
    pane.setText(welcomeString);

//     SimpleAttributeSet attributeSet = new SimpleAttributeSet();
//     StyleConstants.setBold(attributeSet, true);
//     StyleConstants.setItalic(attributeSet, true);
//     StyleConstants.setForeground(attributeSet, Color.RED);
//
//     pane.setSelectionStart(0);
//     pane.setSelectionEnd(pane.getText().length());
//     pane.setParagraphAttributes(attributeSet, true);
//     pane.setSelectionStart(pane.getText().length());

    return new JScrollPane(pane);
  }

  public static void main(String[] args) {
    EventQueue.invokeLater(() -> {
      JFrame f = new JFrame("JTextPane Example");
      f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      f.getContentPane().add(new JTextPaneExampleOne2().makeUI());
      f.setSize(400, 400);
      f.setLocationRelativeTo(null);
      f.setVisible(true);
    });
  }
}
于 2019-05-23T12:18:41.913 回答