Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将JScrollBar/JScrollPane添加到此JTextPane时遇到问题_Java_Swing_Jscrollpane_Jtextpane - Fatal编程技术网

Java 将JScrollBar/JScrollPane添加到此JTextPane时遇到问题

Java 将JScrollBar/JScrollPane添加到此JTextPane时遇到问题,java,swing,jscrollpane,jtextpane,Java,Swing,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(image

好的,下面是我的一段代码,其中包含了这个问题:

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中,以给它一个滚动条。提前谢谢

基本上,您实际上从未向
JScrollPane
添加有效组件

private JTextPane images;
private JScrollPane jsp = new JScrollPane(images); 
执行此操作时,
images
null
,因此基本上,您正在调用
newjscrollpane(null)

然后,基本上在框架上的(替换)
jsp
顶部添加
images

add(jsp);
add(images, BorderLayout.CENTER);
默认位置为
BorderLayout。中心
和边框布局只能在其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());
}

基本上,您实际上从未向
JScrollPane
添加有效组件

private JTextPane images;
private JScrollPane jsp = new JScrollPane(images); 
执行此操作时,
images
null
,因此基本上,您正在调用
newjscrollpane(null)

然后,基本上在框架上的(替换)
jsp
顶部添加
images

add(jsp);
add(images, BorderLayout.CENTER);
默认位置为
BorderLayout。中心
和边框布局只能在其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());
}