2017-06-16 28 views
0

我正在嘗試使用Java創建聊天應用程序;我選擇使用JTextPane來顯示消息,因爲我讀它支持對齊。我的問題是我想知道如何對齊文本。就像我是發件人時一樣,發送的信息將會正確對齊;當我是接收器時,它將在左邊對齊。如何在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); 
} 

} 

有誰知道如何做到這一點?

+3

可能重複[Java Swing JTextArea左右寫入](https://stackoverflow.com/questions/37365522/java-swing-jtextarea-write-both-left-andright) – Frakcool

+0

@Frakcool它didn沒有工作表 – Karim

+4

什麼沒有工作?請[編輯]你的問題,併發佈一個有效的證明你的問題的[mcve],解釋爲什麼這個鏈接的問題不起作用 – Frakcool

回答

0

在代碼示例中的「setTextToTextPane」中插入「txt =」\ n「」。這應該是「」txt +「\ n」。沒有這個改變,它不會打印任何東西。 您在「setTextToPane」中所做的操作在設置屬性方面是正確的。如果您進行單元測試,則根據「收到」的值將文本左右放置正確。

相關問題