JTextPane (Swing) 中溢出 unicode 字符的问题
Problem with overflowing unicode characters in JTextPane (Swing)
我想使用 unicode 字符可视化扑克牌游戏。我在 JTextPane 中将它们打印在不同的行上。正如您在第一张图片中看到的那样,它们有时会溢出到其他行。我正在使用相同的方法输出卡片,所以它只是偶尔发生真的很奇怪。这完全是随机的。
每张卡片都通过SimpleAttributeSet 设置了相同的样式。卡片的行高看起来比应有的要小。所以我在想Swing可能对unicode字符支持不好,所以我在卡片之间添加了一个'M'字符,与卡片的样式相同。之后一切似乎都正常。
这是我如何打印卡片的示例(卡片是循环打印的):
StyledDocument doc = jTextPane.getStyledDocument();
doc.insertString(doc.getLength(), "Karty na stole: \n", attributeSet);
doc.insertString(doc.getLength(), " \n" , attributeSetForCards);
卡片有时会溢出:
在这里你可以看到它们被正确输出:
下面的代码是该错误的示例,但该错误有时会发生。我注意到 Thread.sleep() 对它有一些影响,因为这个命令发生的可能性增加了。正如您在下面的代码中所看到的,没有 Thread.sleep() 命令,但错误仍然出现。
(我更改了字母 "M" 的卡片 unicode)
这是代码示例:
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.WindowConstants;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
public class WhosebugProblem {
public static void main(String[] args) throws BadLocationException {
JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JTextPane textPane = new JTextPane();
textPane.setEditable(false);
StyledDocument doc = textPane.getStyledDocument();
SimpleAttributeSet keyWord = new SimpleAttributeSet();
SimpleAttributeSet attributeSet = new SimpleAttributeSet();
StyleConstants.setBold(keyWord, true);
StyleConstants.setFontSize(attributeSet, 100);
StyleConstants.setForeground(attributeSet, Color.RED);
JScrollPane sp = new JScrollPane(textPane);
frame.getContentPane().add(sp);
frame.setVisible(true);
int x = 0;
while (x < 100) {
x++;
doc.insertString(doc.getLength(), "Karty na stole: " + "\n", keyWord);
StyleConstants.setForeground(attributeSet, Color.RED);
for (int i = 0; i < 5; i++) {
doc.insertString(doc.getLength(), "M", attributeSet);
if (i > 1) {
StyleConstants.setForeground(attributeSet, Color.BLACK);
}
}
doc.insertString(doc.getLength(), "\n", keyWord);
doc.insertString(doc.getLength(), "Karty na stole: " + "\n", keyWord);
for (int i = 0; i < 2; i++) {
doc.insertString(doc.getLength(), "M", attributeSet);
}
doc.insertString(doc.getLength(), "\n", keyWord);
}
}
}
备用代码:(?)
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
public class WhosebugProblem {
public static void main(String[] args) throws BadLocationException {
JFrame frame = new JFrame();
frame.setSize(1500, 600);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JTextPane textPane = new JTextPane();
textPane.setEditable(false);
StyledDocument doc = textPane.getStyledDocument();
SimpleAttributeSet keyWord = new SimpleAttributeSet();
SimpleAttributeSet attributeSet = new SimpleAttributeSet();
StyleConstants.setBold(keyWord, true);
StyleConstants.setFontSize(attributeSet, 100);
StyleConstants.setFontFamily(attributeSet, getFontFamily());
StyleConstants.setForeground(attributeSet, Color.RED);
JScrollPane sp = new JScrollPane(textPane);
frame.getContentPane().add(sp);
frame.setVisible(true);
String[][] obrazkyKariet = new String[4][14];
for (int ii = 0; ii < 14; ii++) {
obrazkyKariet[0][ii] = new String(Character.toChars(ii + 127137));
}
for (int ii = 0; ii < 14; ii++) {
obrazkyKariet[1][ii] = new String(Character.toChars(ii + 127153));
}
for (int ii = 0; ii < 14; ii++) {
obrazkyKariet[2][ii] = new String(Character.toChars(ii + 127169));
}
for (int ii = 0; ii < 14; ii++) {
obrazkyKariet[3][ii] = new String(Character.toChars(ii + 127185));
}
doc.insertString(doc.getLength(), "Karty na stole: " + "\n", keyWord);
StyleConstants.setForeground(attributeSet, Color.RED);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 14; j++) {
if (i == 0 || i == 3) {
StyleConstants.setForeground(attributeSet, Color.BLACK);
} else {
StyleConstants.setForeground(attributeSet, Color.RED);
}
doc.insertString(doc.getLength(), obrazkyKariet[i][j], attributeSet);
}
doc.insertString(doc.getLength(), "\n", keyWord);
}
}
public static String getFontFamily() {
Font[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
for (Font font : fonts) {
if (font.canDisplay(127137)) {
System.out.println("First compatible font: " + font.getFamily());
return font.getFamily();
}
}
return "";
}
}
Swing 是单线程的。
所以所有Swing组件的创建和组件或其模型的更新都必须在Event Dispatch Thread (EDT)
上完成,否则会出现随机问题。
此处似乎发生的情况是,在调用另一个 insertString(…)
方法之前文档尚未完全更新,并且某些文本未插入到文档中的正确位置。
阅读有关 Concurrency 的 Swing 教程部分,了解有关 EDT 的更多信息。
因此解决方案是将要执行的代码放在 EDT
上。代码应该是这样的:
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.WindowConstants;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
public class WhosebugProblem {
private static void createAndShowGUI() throws Exception
{
JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JTextPane textPane = new JTextPane();
textPane.setEditable(false);
StyledDocument doc = textPane.getStyledDocument();
SimpleAttributeSet keyWord = new SimpleAttributeSet();
SimpleAttributeSet attributeSet = new SimpleAttributeSet();
StyleConstants.setBold(keyWord, true);
StyleConstants.setFontSize(attributeSet, 100);
StyleConstants.setForeground(attributeSet, Color.RED);
JScrollPane sp = new JScrollPane(textPane);
frame.getContentPane().add(sp);
frame.setVisible(true);
int x = 0;
while (x < 100) {
x++;
doc.insertString(doc.getLength(), "Karty na stole: " + "\n", keyWord);
StyleConstants.setForeground(attributeSet, Color.RED);
for (int i = 0; i < 5; i++) {
doc.insertString(doc.getLength(), "M", attributeSet);
if (i > 1) {
StyleConstants.setForeground(attributeSet, Color.BLACK);
}
}
doc.insertString(doc.getLength(), "\n", keyWord);
doc.insertString(doc.getLength(), "Karty na stole: " + "\n", keyWord);
for (int i = 0; i < 2; i++) {
doc.insertString(doc.getLength(), "M", attributeSet);
}
doc.insertString(doc.getLength(), "\n", keyWord);
}
}
public static void main(String[] args) throws Exception
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
createAndShowGUI();
}
catch(Exception e) { System.out.println(e); }
}
});
}
}
invokeLater(…)
将代码放在 EDT
.
我想使用 unicode 字符可视化扑克牌游戏。我在 JTextPane 中将它们打印在不同的行上。正如您在第一张图片中看到的那样,它们有时会溢出到其他行。我正在使用相同的方法输出卡片,所以它只是偶尔发生真的很奇怪。这完全是随机的。
每张卡片都通过SimpleAttributeSet 设置了相同的样式。卡片的行高看起来比应有的要小。所以我在想Swing可能对unicode字符支持不好,所以我在卡片之间添加了一个'M'字符,与卡片的样式相同。之后一切似乎都正常。
这是我如何打印卡片的示例(卡片是循环打印的):
StyledDocument doc = jTextPane.getStyledDocument();
doc.insertString(doc.getLength(), "Karty na stole: \n", attributeSet);
doc.insertString(doc.getLength(), " \n" , attributeSetForCards);
卡片有时会溢出:
在这里你可以看到它们被正确输出:
下面的代码是该错误的示例,但该错误有时会发生。我注意到 Thread.sleep() 对它有一些影响,因为这个命令发生的可能性增加了。正如您在下面的代码中所看到的,没有 Thread.sleep() 命令,但错误仍然出现。
(我更改了字母 "M" 的卡片 unicode)
这是代码示例:
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.WindowConstants;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
public class WhosebugProblem {
public static void main(String[] args) throws BadLocationException {
JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JTextPane textPane = new JTextPane();
textPane.setEditable(false);
StyledDocument doc = textPane.getStyledDocument();
SimpleAttributeSet keyWord = new SimpleAttributeSet();
SimpleAttributeSet attributeSet = new SimpleAttributeSet();
StyleConstants.setBold(keyWord, true);
StyleConstants.setFontSize(attributeSet, 100);
StyleConstants.setForeground(attributeSet, Color.RED);
JScrollPane sp = new JScrollPane(textPane);
frame.getContentPane().add(sp);
frame.setVisible(true);
int x = 0;
while (x < 100) {
x++;
doc.insertString(doc.getLength(), "Karty na stole: " + "\n", keyWord);
StyleConstants.setForeground(attributeSet, Color.RED);
for (int i = 0; i < 5; i++) {
doc.insertString(doc.getLength(), "M", attributeSet);
if (i > 1) {
StyleConstants.setForeground(attributeSet, Color.BLACK);
}
}
doc.insertString(doc.getLength(), "\n", keyWord);
doc.insertString(doc.getLength(), "Karty na stole: " + "\n", keyWord);
for (int i = 0; i < 2; i++) {
doc.insertString(doc.getLength(), "M", attributeSet);
}
doc.insertString(doc.getLength(), "\n", keyWord);
}
}
}
备用代码:(?)
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
public class WhosebugProblem {
public static void main(String[] args) throws BadLocationException {
JFrame frame = new JFrame();
frame.setSize(1500, 600);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JTextPane textPane = new JTextPane();
textPane.setEditable(false);
StyledDocument doc = textPane.getStyledDocument();
SimpleAttributeSet keyWord = new SimpleAttributeSet();
SimpleAttributeSet attributeSet = new SimpleAttributeSet();
StyleConstants.setBold(keyWord, true);
StyleConstants.setFontSize(attributeSet, 100);
StyleConstants.setFontFamily(attributeSet, getFontFamily());
StyleConstants.setForeground(attributeSet, Color.RED);
JScrollPane sp = new JScrollPane(textPane);
frame.getContentPane().add(sp);
frame.setVisible(true);
String[][] obrazkyKariet = new String[4][14];
for (int ii = 0; ii < 14; ii++) {
obrazkyKariet[0][ii] = new String(Character.toChars(ii + 127137));
}
for (int ii = 0; ii < 14; ii++) {
obrazkyKariet[1][ii] = new String(Character.toChars(ii + 127153));
}
for (int ii = 0; ii < 14; ii++) {
obrazkyKariet[2][ii] = new String(Character.toChars(ii + 127169));
}
for (int ii = 0; ii < 14; ii++) {
obrazkyKariet[3][ii] = new String(Character.toChars(ii + 127185));
}
doc.insertString(doc.getLength(), "Karty na stole: " + "\n", keyWord);
StyleConstants.setForeground(attributeSet, Color.RED);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 14; j++) {
if (i == 0 || i == 3) {
StyleConstants.setForeground(attributeSet, Color.BLACK);
} else {
StyleConstants.setForeground(attributeSet, Color.RED);
}
doc.insertString(doc.getLength(), obrazkyKariet[i][j], attributeSet);
}
doc.insertString(doc.getLength(), "\n", keyWord);
}
}
public static String getFontFamily() {
Font[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
for (Font font : fonts) {
if (font.canDisplay(127137)) {
System.out.println("First compatible font: " + font.getFamily());
return font.getFamily();
}
}
return "";
}
}
Swing 是单线程的。
所以所有Swing组件的创建和组件或其模型的更新都必须在Event Dispatch Thread (EDT)
上完成,否则会出现随机问题。
此处似乎发生的情况是,在调用另一个 insertString(…)
方法之前文档尚未完全更新,并且某些文本未插入到文档中的正确位置。
阅读有关 Concurrency 的 Swing 教程部分,了解有关 EDT 的更多信息。
因此解决方案是将要执行的代码放在 EDT
上。代码应该是这样的:
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.WindowConstants;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
public class WhosebugProblem {
private static void createAndShowGUI() throws Exception
{
JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JTextPane textPane = new JTextPane();
textPane.setEditable(false);
StyledDocument doc = textPane.getStyledDocument();
SimpleAttributeSet keyWord = new SimpleAttributeSet();
SimpleAttributeSet attributeSet = new SimpleAttributeSet();
StyleConstants.setBold(keyWord, true);
StyleConstants.setFontSize(attributeSet, 100);
StyleConstants.setForeground(attributeSet, Color.RED);
JScrollPane sp = new JScrollPane(textPane);
frame.getContentPane().add(sp);
frame.setVisible(true);
int x = 0;
while (x < 100) {
x++;
doc.insertString(doc.getLength(), "Karty na stole: " + "\n", keyWord);
StyleConstants.setForeground(attributeSet, Color.RED);
for (int i = 0; i < 5; i++) {
doc.insertString(doc.getLength(), "M", attributeSet);
if (i > 1) {
StyleConstants.setForeground(attributeSet, Color.BLACK);
}
}
doc.insertString(doc.getLength(), "\n", keyWord);
doc.insertString(doc.getLength(), "Karty na stole: " + "\n", keyWord);
for (int i = 0; i < 2; i++) {
doc.insertString(doc.getLength(), "M", attributeSet);
}
doc.insertString(doc.getLength(), "\n", keyWord);
}
}
public static void main(String[] args) throws Exception
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
createAndShowGUI();
}
catch(Exception e) { System.out.println(e); }
}
});
}
}
invokeLater(…)
将代码放在 EDT
.