2013-11-28 65 views
0

好了,所以這裏有麻煩就是含有這個問題我的代碼片段:增加的JScrollBar/JScrollPane的這個的JTextPane

private JTextField userText; 
private ObjectOutputStream output; 
private ObjectInputStream input; 
private ServerSocket server; 
private Socket connection; 
private JTextPane images; 
private JScrollPane jsp = new JScrollPane(images); 

public Server(){ 
    super(name+" - IM Server"); 
    images = new JTextPane(); 
    images.setContentType("text/html"); 
    HTMLDocument doc = (HTMLDocument)images.getDocument(); 
    userText = new JTextField(); 
    userText.setEditable(false); 
    userText.addActionListener(
    new ActionListener(){ 
     public void actionPerformed(ActionEvent event){ 
      sendMessage(event.getActionCommand()); 
      userText.setText(""); 
     } 
    } 
); 
    add(userText, BorderLayout.NORTH); 
    add(jsp); 
    add(images, BorderLayout.CENTER); 
    images.setEditable(false); 
    try { 
    doc.insertString(0, "This is where images and text will show up.\nTo send an image, do\n*image*LOCATION OF IMAGE\n with NO SPACES or EXTRA TEXT.", null); 
} catch (BadLocationException e) { 
    e.printStackTrace(); 
} 
    setSize(700,400); 
    setVisible(true); 
    ImageIcon logo = new javax.swing.ImageIcon(getClass().getResource("CHAT.png")); 
    setIconImage(logo.getImage()); 
} 

,當我使用它,還有我的JTextPane沒有滾動條?我試過移動add(jsp);在上面和下面的位置,並將其移動到下方(圖像,BorderLayout.NORTH);灰色嗎?!所以我想知道的是如何將這個JScrollPane添加到我的JTextPane中以給它一個滾動條。提前致謝!

回答

5

Bascially,你卻從未添加有效成分的JScrollPane ...

private JTextPane images; 
private JScrollPane jsp = new JScrollPane(images); 

當此執行,imagesnull,所以基本上,你在呼喚new JScrollPane(null);

然後,你基本上添加images在框架上方(替代)jsp ...

add(jsp); 
add(images, BorderLayout.CENTER); 

默認位置是BorderLayout.CENTER和邊框佈局只能支持單個組件在任何它的5個可用的位置...

相反,你可以試試...

public Server(){ 
    super(name+" - IM Server"); 
    images = new JTextPane(); 
    images.setContentType("text/html"); 
    HTMLDocument doc = (HTMLDocument)images.getDocument(); 
    userText = new JTextField(); 
    userText.setEditable(false); 
    userText.addActionListener(
    new ActionListener(){ 
     public void actionPerformed(ActionEvent event){ 
      sendMessage(event.getActionCommand()); 
      userText.setText(""); 
     } 
    } 
    ); 
    add(userText, BorderLayout.NORTH); 
    jsp.setViewportView(images); 
    add(jsp); 
    //add(images, BorderLayout.CENTER); 
    images.setEditable(false); 
    try { 
     doc.insertString(0, "This is where images and text will show up.\nTo send an image, do\n*image*LOCATION OF IMAGE\n with NO SPACES or EXTRA TEXT.", null); 
    } catch (BadLocationException e) { 
     e.printStackTrace(); 
    } 
    setSize(700,400); 
    setVisible(true); 
    ImageIcon logo = new javax.swing.ImageIcon(getClass().getResource("CHAT.png")); 
    setIconImage(logo.getImage()); 
} 
+0

謝謝! @MadProgrammer – user3042719

+0

不用擔心...;) – MadProgrammer