Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.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 当我调整JScrollPane的内容时,它没有调整大小_Java_Swing - Fatal编程技术网

Java 当我调整JScrollPane的内容时,它没有调整大小

Java 当我调整JScrollPane的内容时,它没有调整大小,java,swing,Java,Swing,我正在做一个聊天服务器,我的显示器有问题。我在JScrollPane中有一个JPanel。JPanel显示文本,但当文本低于屏幕上的下边框时,JScrollPane不会显示任何滚动条。 我的窗口代码: package com.main; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.GridLayout;

我正在做一个聊天服务器,我的显示器有问题。我在JScrollPane中有一个JPanel。JPanel显示文本,但当文本低于屏幕上的下边框时,JScrollPane不会显示任何滚动条。 我的窗口代码:

package com.main;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import com.panels.ChatPanel;
import com.panels.LinePanel;

public class Window extends JFrame
{
    private ChatPanel chatPanel;

    private JPanel content;

    private TextField textField;

    private ArrayList<LinePanel> linePanels = new ArrayList<LinePanel> ();

    private Font font = new Font("Arial", Font.BOLD, 17);

    private JScrollPane scrollPanel;

    public Window(TextField textField)
    {
        //Sets the title of the window
        this.setTitle("Chat");
        //Sets the size of the window
        this.setSize(1000, 800);
        //Sets the location of the window on the screen
        this.setLocation(50, 0);
        //The program will close when the red X is pressed
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.chatPanel = new ChatPanel();

        this.content = new JPanel();

        this.scrollPanel = new JScrollPane(this.chatPanel);
        this.scrollPanel.getVerticalScrollBar().setUnitIncrement(16);
        this.scrollPanel.setBorder(BorderFactory.createEmptyBorder());

        this.textField = textField;
        this.textField.setPreferredSize(new Dimension(this.getWidth(), 30));
        this.textField.setFont(this.font);

        //Sets the content panel
        this.setContentPane(content);
        this.setLayout(new BorderLayout());

        this.content.add(this.scrollPanel, BorderLayout.CENTER);
        this.content.add(this.textField, BorderLayout.SOUTH);
        this.content.setBackground(Color.white);

        //Makes the window visible
        this.setVisible(true);
    }

    public void screen()
    {
        this.chatPanel.repaint();

        this.scrollPanel.repaint();
        this.scrollPanel.revalidate();

        this.getContentPane().repaint();
        this.revalidate();
    }

    public void addLine(String newLine)
    {
        this.chatPanel.addLine(newLine);
    }
}
package.com.main;
导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Font;
导入java.awt.GridLayout;
导入java.util.ArrayList;
导入javax.swing.BorderFactory;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.JScrollPane;
导入com.panels.ChatPanel;
导入com.panels.LinePanel;
公共类窗口扩展了JFrame
{
私人聊天室聊天室;
私有JPanel内容;
私有文本字段文本字段;
private ArrayList linePanels=new ArrayList();
私有字体=新字体(“Arial”,Font.BOLD,17);
私有JScrollPane滚动面板;
公共窗口(TextField TextField)
{
//设置窗口的标题
本文件的标题为“聊天”;
//设置窗口的大小
此.setSize(1000800);
//设置窗口在屏幕上的位置
此设置位置(50,0);
//按下红色X时,程序将关闭
此.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.chatPanel=新建chatPanel();
this.content=new JPanel();
this.scrollPanel=newjscrollpane(this.chatPanel);
this.scrollPanel.getVerticalScrollBar().setUnitIncrement(16);
this.scrollPanel.setboorder(BorderFactory.createEmptyByOrder());
this.textField=textField;
this.textField.setPreferredSize(新维度(this.getWidth(),30));
this.textField.setFont(this.font);
//设置内容面板
此.setContentPane(内容);
此.setLayout(新的BorderLayout());
this.content.add(this.scrollPanel,BorderLayout.CENTER);
this.content.add(this.textField,BorderLayout.SOUTH);
本.内容.背景(颜色.白色);
//使窗口可见
此.setVisible(true);
}
公共空白屏幕()
{
this.chatPanel.repaint();
this.scrollPanel.repaint();
this.scrollPanel.revalidate();
这个.getContentPane().repaint();
这个。重新验证();
}
公共void addLine(字符串换行符)
{
this.chatPanel.addLine(换行符);
}
}
显示文本的JPanel是chatPanel,JScrollPane是scrollPanel

以下是chatPanel的代码:

package com.panels;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.util.ArrayList;

import javax.swing.JPanel;

public class ChatPanel extends JPanel
{
    private ArrayList<LinePanel> linePanels = new ArrayList<LinePanel> ();

    private ArrayList<String> chat = new ArrayList<String> ();

    private GridLayout layout = new GridLayout(1, 1);

    private static final int LINE_HEIGHT = 25;

    public ChatPanel()
    {
        super();

        this.setLayout(layout);
    }

    public void paintComponent(Graphics g)
    {

        g.setColor(Color.white);
        g.fillRect(0,  0, this.getWidth(), this.getHeight());

        g.setFont(new Font("Arial", Font.BOLD, 20));
        g.setColor(Color.black);

        for (int i = 0; i < this.chat.size(); i ++)
            g.drawString(this.chat.get(i), 10,  (i + 1) * LINE_HEIGHT);
    }

    public void addLine(String newLine)
    {
        this.chat.add(newLine);

        if (this.getHeight() < this.chat.size() * LINE_HEIGHT)
            this.setSize(new Dimension(this.chat.size() * LINE_HEIGHT, this.getWidth()));
    }
}
package.com.panels;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Font;
导入java.awt.Graphics;
导入java.awt.GridLayout;
导入java.util.ArrayList;
导入javax.swing.JPanel;
公共类聊天面板扩展JPanel
{
private ArrayList linePanels=new ArrayList();
private ArrayList chat=new ArrayList();
私有网格布局=新网格布局(1,1);
专用静态最终整型线高度=25;
公众谘询委员会(
{
超级();
这个.setLayout(布局);
}
公共组件(图形g)
{
g、 setColor(Color.white);
g、 fillRect(0,0,this.getWidth(),this.getHeight());
g、 setFont(新字体(“Arial”,Font.BOLD,20));
g、 设置颜色(颜色为黑色);
for(int i=0;i
“width”值应该是传递给类的参数,以建议组件的默认宽度

而且,你的绘画正在重塑轮子。JPanel将重新绘制其背景。因此,代码应该是:

//g.setColor(Color.white);
//g.fillRect(0,  0, this.getWidth(), this.getHeight());
super.paintComponent(g);
然后你会调用:

setBackground( Color.WHITE );

在构造函数中

通常在调用方法
repaint()
之前调用方法
revalidate()
是否有不使用或的原因?请阅读,尤其是“帮助他人重现问题”标题下的部分
//g.setColor(Color.white);
//g.fillRect(0,  0, this.getWidth(), this.getHeight());
super.paintComponent(g);
setBackground( Color.WHITE );