0
私はJavaで簡単なメッセージングアプリケーションを作っています。私はすべてのwhatsapp、メッセンジャーなどのように私のtextAreaの左右の両方のメッセージを表示したいと思います。Java Swing JTextAreaは左右両方に書き込みます
多くのありがとう
私はJavaで簡単なメッセージングアプリケーションを作っています。私はすべてのwhatsapp、メッセンジャーなどのように私のtextAreaの左右の両方のメッセージを表示したいと思います。Java Swing JTextAreaは左右両方に書き込みます
多くのありがとう
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
これは、[Javaの - 右に揃えJTextAreaに]の重複ではありません(のhttp:// stackoverflowの.com/questions/24315757/java-align-jtextarea-to-the-right)。もしImが間違っていないならば、@Ilkinは同じテキストエリア内でいくつかのテキストを左揃えと右揃えにする必要があります。その質問に答えても、ユーザーがタイプ/追加する段落ごとに整列を変更しなければならないという点を除いて、この質問に対する答えがあります。 –
上記のコメントに同意します。答えは、個々のテキスト行ではなく、テキストペイン全体の配置を変更することです。より良い例がありました:http://stackoverflow.com/questions/36409784/how-can-i-align-text-to-the-right-in-a-jtextarea/36410566#36410566。その例は実行可能ではないので、この質問を再開して、完全なSSCCEを投稿することができます。 – camickr
はい、@MadPiranha、私はすでに言及した – Ilkin