Java 如何在JTextArea中将文本向右对齐?
我正在构建一个基于文本的冒险游戏,我正在尝试使计算机的响应显示在左侧,而您所做的选择显示在右侧,以便轻松区分两者。问题是,我似乎找不到将文本向右对齐的方法。我在Java 如何在JTextArea中将文本向右对齐?,java,swing,text,Java,Swing,Text,我正在构建一个基于文本的冒险游戏,我正在尝试使计算机的响应显示在左侧,而您所做的选择显示在右侧,以便轻松区分两者。问题是,我似乎找不到将文本向右对齐的方法。我在JScrollPane中使用JTextArea,所有这些都在JFrame中 非常感谢您的帮助,谢谢。:) 不能使用JTextArea来更改单个文本行的对齐方式 要更改各行的属性,最简单的方法是使用JTextPane。比如: JTextPane textPane = new JTextPane(); StyledDocument doc =
JScrollPane
中使用JTextArea
,所有这些都在JFrame
中
非常感谢您的帮助,谢谢。:) 不能使用JTextArea来更改单个文本行的对齐方式 要更改各行的属性,最简单的方法是使用
JTextPane
。比如:
JTextPane textPane = new JTextPane();
StyledDocument doc = textPane.getStyledDocument();
SimpleAttributeSet left = new SimpleAttributeSet();
StyleConstants.setAlignment(left, StyleConstants.ALIGN_LEFT);
StyleConstants.setForeground(left, Color.RED);
SimpleAttributeSet right = new SimpleAttributeSet();
StyleConstants.setAlignment(right, StyleConstants.ALIGN_RIGHT);
StyleConstants.setForeground(right, Color.BLUE);
try
{
doc.insertString(doc.getLength(), "\nLeft aligned text.", left );
doc.setParagraphAttributes(doc.getLength(), 1, left, false);
doc.insertString(doc.getLength(), "\nRight aligned text.", right );
doc.setParagraphAttributes(doc.getLength(), 1, right, false);
doc.insertString(doc.getLength(), "\nMore left aligned text.", left );
doc.setParagraphAttributes(doc.getLength(), 1, left, false);
doc.insertString(doc.getLength(), "\nMore right aligned text.", right );
doc.setParagraphAttributes(doc.getLength(), 1, right, false);
}
catch(Exception e) {}
不能使用JTextArea来更改单个文本行的对齐方式 要更改各行的属性,最简单的方法是使用
JTextPane
。比如:
JTextPane textPane = new JTextPane();
StyledDocument doc = textPane.getStyledDocument();
SimpleAttributeSet left = new SimpleAttributeSet();
StyleConstants.setAlignment(left, StyleConstants.ALIGN_LEFT);
StyleConstants.setForeground(left, Color.RED);
SimpleAttributeSet right = new SimpleAttributeSet();
StyleConstants.setAlignment(right, StyleConstants.ALIGN_RIGHT);
StyleConstants.setForeground(right, Color.BLUE);
try
{
doc.insertString(doc.getLength(), "\nLeft aligned text.", left );
doc.setParagraphAttributes(doc.getLength(), 1, left, false);
doc.insertString(doc.getLength(), "\nRight aligned text.", right );
doc.setParagraphAttributes(doc.getLength(), 1, right, false);
doc.insertString(doc.getLength(), "\nMore left aligned text.", left );
doc.setParagraphAttributes(doc.getLength(), 1, left, false);
doc.insertString(doc.getLength(), "\nMore right aligned text.", right );
doc.setParagraphAttributes(doc.getLength(), 1, right, false);
}
catch(Exception e) {}
请尝试以下代码:)
将此代码放入构造函数中
请尝试以下代码:)
将此代码放入构造函数。可能重复:@KevinO我尝试过这些,但不起作用。这可能是因为我使用的是
append(String)
方法吗?没有提供足够的信息来确切说明您是如何尝试实现结果的。您需要共享简洁的代码,清楚地说明问题,并清楚地解释期望和失败。但是,如果您的目的是尝试在同一文本区域中左对齐和右对齐文本,则必须将回复字符串的格式设置为右对齐。ApacheCommons有一些实用程序可以提供帮助。我会亲自使用一个JScrollPane,并为对话的每个部分添加新的小部件,一些左对齐,一些右对齐。你必须使用JEditorPane或JTextPane来完成这项工作。也许更简单的方法是使用两个并排的JTextAreas,或者一个JList或一个2列jTable,可能是:@KevinO的副本,我试过了,但不起作用。这可能是因为我使用的是append(String)
方法吗?没有提供足够的信息来确切说明您是如何尝试实现结果的。您需要共享简洁的代码,清楚地说明问题,并清楚地解释期望和失败。但是,如果您的目的是尝试在同一文本区域中左对齐和右对齐文本,则必须将回复字符串的格式设置为右对齐。ApacheCommons有一些实用程序可以提供帮助。我会亲自使用一个JScrollPane,并为对话的每个部分添加新的小部件,一些左对齐,一些右对齐。你必须使用JEditorPane或JTextPane来完成这项工作。也许更简单的方法是使用两个并排的JTextAreas,或者一个JList或一个2列jTable当我试图编译时,它说在这里找不到变量StyleConstants
:StyleConstants.setAlignment(左,**StyleConstants**.ALIGN_左)代码>但不是开头的代码。我遗漏了什么吗?请阅读StyleConstants
类的API,找出需要导入的包。当我尝试编译时,它说在这里找不到变量StyleConstants
:StyleConstants.setAlignment(左,**StyleConstants**.ALIGN\u左)代码>但不是开头的代码。我遗漏了什么吗?请阅读StyleConstants
类的API,找出需要导入的包。