Java Swing JTextArea同时写入左侧和右侧

Java Swing JTextArea同时写入左侧和右侧,java,swing,user-interface,Java,Swing,User Interface,我正在用Java制作一个简单的消息传递应用程序。我想在文本区域的左侧和右侧显示消息,如所有whatsapp、messenger等。更改方向会更改所有文本的方向,因此没有任何用处 非常感谢您不能使用JTextArea 一种解决方案是使用JTextPane并为插入的每行文本设置属性: import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.text.*; public class Te

我正在用Java制作一个简单的消息传递应用程序。我想在文本区域的左侧和右侧显示消息,如所有whatsapp、messenger等。更改方向会更改所有文本的方向,因此没有任何用处


非常感谢

您不能使用JTextArea

一种解决方案是使用
JTextPane
并为插入的每行文本设置属性:

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

public class TextPaneChat
{
    private static void createAndShowGUI()
    {
        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(), "Are you busy tonight?", left );
            doc.setParagraphAttributes(doc.getLength(), 1, left, false);
            doc.insertString(doc.getLength(), "\nNo", right );
            doc.setParagraphAttributes(doc.getLength(), 1, right, false);
            doc.insertString(doc.getLength(), "\nFeel like going to a movie?", left );
            doc.setParagraphAttributes(doc.getLength(), 1, left, false);
            doc.insertString(doc.getLength(), "\nSure", right );
            doc.setParagraphAttributes(doc.getLength(), 1, right, false);
        }
        catch(Exception e) { System.out.println(e); }

        JFrame frame = new JFrame("Text Pane Chat");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new JScrollPane( textPane ) );
        frame.setLocationByPlatform( true );
        frame.setSize(200, 200);
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater( () -> createAndShowGUI() );
/*
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
*/
    }
}

你不能使用JTextArea

一种解决方案是使用
JTextPane
并为插入的每行文本设置属性:

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

public class TextPaneChat
{
    private static void createAndShowGUI()
    {
        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(), "Are you busy tonight?", left );
            doc.setParagraphAttributes(doc.getLength(), 1, left, false);
            doc.insertString(doc.getLength(), "\nNo", right );
            doc.setParagraphAttributes(doc.getLength(), 1, right, false);
            doc.insertString(doc.getLength(), "\nFeel like going to a movie?", left );
            doc.setParagraphAttributes(doc.getLength(), 1, left, false);
            doc.insertString(doc.getLength(), "\nSure", right );
            doc.setParagraphAttributes(doc.getLength(), 1, right, false);
        }
        catch(Exception e) { System.out.println(e); }

        JFrame frame = new JFrame("Text Pane Chat");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new JScrollPane( textPane ) );
        frame.setLocationByPlatform( true );
        frame.setSize(200, 200);
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater( () -> createAndShowGUI() );
/*
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
*/
    }
}

这不是一个复制品。如果我没弄错的话@Ilkin需要在同一文本区域中左对齐一些文本,右对齐一些文本。话虽如此。。。该问题的答案与该问题的答案相同,但用户键入/添加的每个段落的对齐方式都必须更改。同意上述评论。答案是更改整个文本窗格的对齐方式,而不是单个文本行的对齐方式。更好的例子可能是:。因为这个例子是不可执行的,所以我重新打开了这个问题,这样就可以发布一个完整的SSCCE。是的,@MadPiranha,我已经提到这不是一个重复的。如果我没弄错的话@Ilkin需要在同一文本区域中左对齐一些文本,右对齐一些文本。话虽如此。。。该问题的答案与该问题的答案相同,但用户键入/添加的每个段落的对齐方式都必须更改。同意上述评论。答案是更改整个文本窗格的对齐方式,而不是单个文本行的对齐方式。更好的例子可能是:。因为这个例子是不可执行的,所以我重新打开了这个问题,以便发布一个完整的SSCCE。是的,@MadPiranha,我已经提到了