Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 窗格和系统字体中的换行字_Java_Swing_Java 7_Jeditorpane - Fatal编程技术网

Java 窗格和系统字体中的换行字

Java 窗格和系统字体中的换行字,java,swing,java-7,jeditorpane,Java,Swing,Java 7,Jeditorpane,我想创建一个大的文本字段,供用户键入内容。我还希望它使用默认的系统字体来匹配预期的外观和感觉。所以我尝试使用带有默认构造函数的JEditorPane,它使用纯文本编码 JEditorPane editorPane = new JEditorPane(); editorPane.setText(gettysburgAddress); 这样做的问题是,纯文本编码不会在每个单词的末尾换行,只有在字符用完时才会换行 我尝试使用HTML编码,word将其包装: JEditorPane editorPa

我想创建一个大的文本字段,供用户键入内容。我还希望它使用默认的系统字体来匹配预期的外观和感觉。所以我尝试使用带有默认构造函数的JEditorPane,它使用纯文本编码

JEditorPane editorPane = new JEditorPane();
editorPane.setText(gettysburgAddress);

这样做的问题是,纯文本编码不会在每个单词的末尾换行,只有在字符用完时才会换行

我尝试使用HTML编码,word将其包装:

JEditorPane editorPane = new JEditorPane("text/html", "");
editorPane.setText(gettysburgAddress);

它有wrap这个词,但是它的默认字体与系统的默认字体不同(Mac OS X上的Helvetica,我不想要它)


我怎样才能两全其美:word wrap和系统默认字体?我不需要任何特殊的格式或任何东西,所以如果可能的话,纯文本编码就可以了。

如果只需要使用系统字体的word wrapped JEditorPane,而您不需要任何特殊的东西,比如样式化的文本或图像,那么它就是专业的bably最好切换到a,这是一个纯文本的文本组件。默认情况下它不会自动换行,但很容易实现:

JTextArea textArea = new JTextArea();
textArea.setLineWrap(true); //Makes the text wrap to the next line
textArea.setWrapStyleWord(true); //Makes the text wrap full words, not just letters
textArea.setText(gettysburgAddress);
如果出于某种原因您必须使用JEditorPane,您必须卷起袖子,对JEditorPane呈现文本的方式进行一些更改。另一方面,您可以选择几种不同的方法。您可以:

  • 将编码设置为HTML(word包装),并使用CSS指定字体(已描述)
  • 将编码设置为RTF(word包装),并修改底层RTFEditorKit(已描述)的字体值
  • 创建SimpleAttributeSet,并在添加字符串时使用它,以指定它们应以这种方式显示(已描述)

JTextArea和JTextPane换行。@camickr,这可能行得通。把它转换成一个答案,展示它(也许还展示了如何使用JEditorPane实现这一点)我会接受的。比如使用
SimpleAttributeSet
把它变成一个显示它的答案
-什么?我不会为你写代码的。如果你自己尝试的话,你现在就应该有你的答案了。@camickr,我想你想要一个答案的名声;这就是我所说的“把它变成一个显示它的答案”。如果你不感兴趣,我可以自己写答案。Java7中有一个bug,有截图