Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.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 如何在JTextArea中将文本向右对齐?_Java_Swing_Text - Fatal编程技术网

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,找出需要导入的包。