2013-11-28 27 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