2017-04-07 105 views
-1

我正在處理一個應該表示用戶和我的程序之間的對話聊天框的窗口。我想要的是用戶消息最終位於對話區的右側,而計算機生成的消息最終位於左側。我有談話區是一個滾動條chat frame顯示在JTextArea中的文本

+0

JTextArea是可能太限制了這一點,你需要使用一個文本組件,允許更靈活的字符和行格式。 –

+0

這最初是作爲以下副本而封閉的:http://stackoverflow.com/questions/24315757/java-align-jtextarea-to-the-right。該示例將整個文本窗格設置爲右對齊。這個要求是交替的右/左對齊。 – camickr

回答

2

您可以使用JTextPane中,並設置「款」屬性爲你添加文本到文本窗格控制左/右對齊一個JTextArea。

下面是一個簡單的例子,顯示了插入文本時如何「居中」。右/左對齊的概念是相同的。

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(); 
      } 
     }); 
*/ 
    } 
}