在JTextArea中显示的文本 [英] text displaying in a JTextArea

查看:398
本文介绍了在JTextArea中显示的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个应该代表用户与我的程序之间的对话的聊天框的窗口上工作.我想要的是,用户消息最终出现在会话区域的右侧,而计算机生成的消息最终出现在左侧.我的会话区域是带有滚动条的JtextArea

I'm working on a window that is supposed to represent a chat box of a dialogue between the user and the my program. What I want is that the user messages end up on the right side of the conversation zone and the messages generated by the computer end up on the left side. The conversation zone that I have is a JtextArea with a scrollbar

推荐答案

在将文本添加到文本窗格中以控制左右对齐时,可以使用JTextPane并设置段落"属性.

You can use a JTextPane and set "paragraph" attributes as you add text to the text pane to control the right/left alignment.

这是一个简单的示例,显示了如何在插入文本时居中"文本.左右对齐的概念相同.

Here is a simple example that shows how to "center" text as it is inserted. The concept is the same for right/left alignment.

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

public class TextPaneAttributes extends JPanel
{

    public TextPaneAttributes()
    {
        setLayout( new BorderLayout() );

        JTextPane textPane = new JTextPane();
        textPane.setText( "one\ntwo\nthree\nfour\nfive\nsix\nseven\neight" );

//      DefaultHighlighter highlighter =  (DefaultHighlighter)textPane.getHighlighter();
//      highlighter.setDrawsLayeredHighlights(false);

        //  Define some character and paragraph attributes

        SimpleAttributeSet keyWord = new SimpleAttributeSet();
        StyleConstants.setBold(keyWord, true);

        SimpleAttributeSet green = new SimpleAttributeSet();
        StyleConstants.setForeground(green, Color.GREEN);

        SimpleAttributeSet center = new SimpleAttributeSet();
        StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);

        SimpleAttributeSet left = new SimpleAttributeSet();
        StyleConstants.setAlignment(left, StyleConstants.ALIGN_LEFT);

        //  Change attributes on some existing text

        StyledDocument doc = textPane.getStyledDocument();
        doc.setCharacterAttributes(0, 3, keyWord, false);
        doc.setCharacterAttributes(8, 5, green, true);
        doc.setParagraphAttributes(20, 1 , center, false);

        //  Add some text with attributes

        try
        {
            doc.insertString(doc.getLength(), "\nNormal text", null);
            doc.insertString(doc.getLength(), "\nGreen text centered", green);
            doc.setParagraphAttributes(doc.getLength(), 1 , center, false);
            doc.insertString(doc.getLength(), "\nKeyword text", keyWord);
            doc.setParagraphAttributes(doc.getLength(), 1 , left, false);

            //  Newly typed text at the end of the document will inherit the
            //  "keyword" attributes unless we remove the attributes

            textPane.setCaretPosition(doc.getLength());
            textPane.getInputAttributes().removeAttributes(keyWord);
        }
        catch(Exception e) {}

        //  Add text pane to frame

        JScrollPane scrollPane = new JScrollPane( textPane );
        scrollPane.setPreferredSize( new Dimension( 200, 250 ) );
        add( scrollPane );

        //  Create a Button panel

        JPanel buttons = new JPanel();
        add(buttons, BorderLayout.PAGE_END);

        //  Add a Bold button

        JButton bold = new JButton( new StyledEditorKit.BoldAction() );
        buttons.add( bold );

        //  Add Right Alignment button

        JButton right = new JButton( new StyledEditorKit.AlignmentAction("Align Right", StyleConstants.ALIGN_RIGHT) );
        buttons.add( right );
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new TextPaneAttributes());
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

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

这篇关于在JTextArea中显示的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆