我试图用Java创建聊天应用程序;我选择使用JTextPane
来显示消息,因为我读到它支持对齐。我的问题是我想知道如何对齐文本。就像我发送者一样,发送的消息将在右边对齐;当我接收器时,它将在左侧对齐。
以下是我编写的代码,但它在右侧或左侧对齐整个文本:
package memory;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import java.awt.Toolkit;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextPane;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import javax.swing.ScrollPaneConstants;
import java.awt.Font;
@SuppressWarnings("serial")
public class ChatFrame extends JFrame {
private JPanel contentPane;
private JTextPane textPane= new JTextPane();;
private String msg=null;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ChatFrame frame = new ChatFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
//setRemote Text
//false->me true->him
public void setTextToTextPane(String txt,boolean received) throws BadLocationException{
StyledDocument doc=textPane.getStyledDocument();
SimpleAttributeSet left = new SimpleAttributeSet();
StyleConstants.setAlignment(left, StyleConstants.ALIGN_LEFT);
SimpleAttributeSet right = new SimpleAttributeSet();
StyleConstants.setAlignment(right, StyleConstants.ALIGN_RIGHT);
if(received==false){
doc.insertString(doc.getLength(), txt="\n", right);
doc.setParagraphAttributes(doc.getLength(), 1, right, false);
}else{
doc.insertString(doc.getLength(), txt="\n", left);
doc.setParagraphAttributes(doc.getLength(), 1, left, false);
}
}
/**
* Create the frame.
*/
public ChatFrame() {
setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\PC-HOME\\Desktop\\design\\Speech Bubble-41.png"));
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 299, 380);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
JScrollPane scrollPane_1 = new JScrollPane();
GroupLayout gl_panel = new GroupLayout(panel);
gl_panel.setHorizontalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 272, GroupLayout.PREFERRED_SIZE)
.addComponent(scrollPane_1, GroupLayout.PREFERRED_SIZE, 272, GroupLayout.PREFERRED_SIZE))
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
gl_panel.setVerticalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
.addComponent(scrollPane_1, GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 65, GroupLayout.PREFERRED_SIZE))
);
textPane.setFont(new Font("Consolas", Font.PLAIN, 14));
textPane.setEditable(false);
scrollPane_1.setViewportView(textPane);
JTextArea textArea = new JTextArea();
textArea.setFont(new Font("Consolas", Font.PLAIN, 14));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.addKeyListener(new KeyAdapter() {
@SuppressWarnings("unchecked")
@Override
public void keyPressed(KeyEvent event) {
if(event.getKeyCode()==KeyEvent.VK_ENTER){
if(event.isShiftDown())
textArea.setText(textArea.getText()+"\n");
else{
msg=textArea.getText();
event.consume();
textArea.setText(null);
try {
//HelperMethods.sendMessageSocket(msg,port);
setTextToTextPane(msg,false);
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ArrayList<String> sendMsg=new ArrayList<>();
sendMsg.add(getTitle());
sendMsg.add(msg);
StaticData.sendMsg.add(sendMsg);
}
}
}
});
addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
String name=getTitle();
StaticData.frameMap.remove(name);
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
});
scrollPane.setViewportView(textArea);
panel.setLayout(gl_panel);
}
}
有谁知道怎么做?
答案 0 :(得分:0)
在&#34; setTextToTextPane&#34;在您的代码示例中,您要插入&#34; txt =&#34; \ n&#34;&#34;。这应该是&#34;&#34; txt +&#34; \ n&#34;。如果没有这种改变,它就不会打印任何东西。 你在&#34; setTextToPane&#34;中做了什么在设置属性方面是正确的。如果你进行单元测试,它会根据&#34;收到&#34;的值正确地向左或向右定位文本。