-1
사용자와 내 프로그램 간의 대화 상자를 나타내는 창에서 작업하고 있습니다. 내가 원하는 것은 사용자 메시지가 대화 영역의 오른쪽에서 끝나고 컴퓨터에서 생성 된 메시지가 왼쪽에 있다는 것입니다. 내가 가지고있는 대화 영역은 스크롤바가있는 JtextArea입니다. JTextArea에 텍스트가 표시되는 경우
사용자와 내 프로그램 간의 대화 상자를 나타내는 창에서 작업하고 있습니다. 내가 원하는 것은 사용자 메시지가 대화 영역의 오른쪽에서 끝나고 컴퓨터에서 생성 된 메시지가 왼쪽에 있다는 것입니다. 내가 가지고있는 대화 영역은 스크롤바가있는 JtextArea입니다. JTextArea에 텍스트가 표시되는 경우
오른쪽/왼쪽 정렬을 제어하기 위해 텍스트를 텍스트에 추가 할 때 JTextPane을 사용하고 "단락"특성을 설정할 수 있습니다.
다음은 텍스트를 삽입 할 때 텍스트를 "가운데 맞추는"방법을 보여주는 간단한 예제입니다. 이 개념은 오른쪽/왼쪽 정렬에서도 동일합니다.
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는이 제한이 너무 많아서보다 유연한 문자 및 줄 형식을 허용하는 텍스트 구성 요소를 사용하려고합니다. –
이것은 원래 다음과 같은 복제본으로 마감되었습니다. http://stackoverflow.com/questions/24315757/java-align-jtextarea-to-the-right. 이 예제는 전체 텍스트 팬을 올바른 정렬로 설정합니다. 이 요구 사항은 오른쪽/왼쪽 정렬을 번갈아 사용하는 것입니다. – camickr