带滚动条的 JPanel 设置内容不可调整大小

JPanel with scrollbar set content not resizable

我的目标是在框中显示通过 UDP 服务器接收的消息。 为此,我创建了一个 JScrollBar,并在其中添加了一个 JPanel。 当我收到一条消息时,将创建扩展 JTextArea 的对象 ReceivedCommand 并将其添加到 JPanel。 我的问题是,当我在 JPanel 中显示太多消息时,它会自动调整我的 TextAreas 的大小。 如何将 TextAreas 设置为不可调整大小,以便添加消息,即使它们在面板中不可见,然后使滚动条最终有用。

下面是我的测试代码来说明:

package test;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.LineBorder;

public class test {

    public static void main(String args[]){
        JFrame frame  = new JFrame();

        JPanel RXCommand = new JPanel();
        RXCommand.setPreferredSize(new Dimension(500, 250));
        RXCommand.setBorder(new LineBorder(Color.black));
        RXCommand.setLayout(new GridLayout(0,1));

        JScrollPane scrollPane = new JScrollPane(RXCommand, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.setPreferredSize(new Dimension(500, 250));

        RXCommand.add(new ReceivedCommand("11:02:56", "5", "5", "command exemple", "command exemple"));
        RXCommand.add(new ReceivedCommand("11:02:56", "5", "5", "command exemple", "command exemple"));
        RXCommand.add(new ReceivedCommand("11:02:56", "5", "5", "command exemple", "command exemple"));
        RXCommand.add(new ReceivedCommand("11:02:56", "5", "5", "command exemple", "command exemple"));
        RXCommand.add(new ReceivedCommand("11:02:56", "5", "5", "command exemple", "command exemple"));
        RXCommand.add(new ReceivedCommand("11:02:56", "5", "5", "command exemple", "command exemple"));
        RXCommand.add(new ReceivedCommand("11:02:56", "5", "5", "command exemple", "command exemple"));
        RXCommand.add(new ReceivedCommand("11:02:56", "5", "5", "command exemple", "command exemple"));
        RXCommand.add(new ReceivedCommand("11:02:56", "5", "5", "command exemple", "command exemple"));
        RXCommand.add(new ReceivedCommand("11:02:56", "5", "5", "command exemple", "command exemple"));
        RXCommand.add(new ReceivedCommand("11:02:56", "5", "5", "command exemple", "command exemple"));
        RXCommand.add(new ReceivedCommand("11:02:56", "5", "5", "command exemple", "command exemple"));
        RXCommand.add(new ReceivedCommand("11:02:56", "5", "5", "command exemple", "command exemple"));
        RXCommand.add(new ReceivedCommand("11:02:56", "5", "5", "command exemple", "command exemple"));
        RXCommand.add(new ReceivedCommand("11:02:56", "5", "5", "command exemple", "command exemple"));

        frame.add(scrollPane);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

收到命令:

package test;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JTextArea;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

public class ReceivedCommand extends JTextArea {
    public ReceivedCommand(String time, String init, String now, String cmd1, String cmd2) {
        this.setPreferredSize(new Dimension(495, 50));
        this.setText("Reçu : " + time +" Canal initial : " + init + " Canal actuel : " + now + "\nCommande 1 :" + cmd1 + "\nCommande 2 : " + cmd2); 
        this.setBorder(new CompoundBorder(new EmptyBorder(5, 5, 5, 5), new LineBorder(Color.black)));   

    }
}
JPanel RXCommand = new JPanel();

首先,变量名不应该以大写字符开头。论坛将突出显示 class 个名称以使代码易于阅读。请注意论坛如何认为您的变量名称是 class 名称?学习并遵循 Java 命名约定。

How can I set the TextAreas not resizable

RXCommand.setLayout(new GridLayout(0,1));

不要使用 GridLayout。 GridLayout 将占用所有可用的 space。所以第一个组件占了 space 的 100%。当你有两个时,每个占 50%。

而是使用 BoxLayoutGridBagLayout

阅读 Layout Managers 上的 Swing 教程部分,了解更多信息和示例以帮助您入门。

and then make the scrollbar finally usefull

上面安德鲁的评论已经回答了。