具有滚动条设置内容的JPanel无法调整大小

时间:2019-04-04 12:40:54

标签: java swing jpanel jscrollpane

我的目标是在一个框中显示通过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);
    }
}

ReceivedCommand:

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)));   

    }
}

1 个答案:

答案 0 :(得分:1)

JPanel RXCommand = new JPanel();

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

  

如何设置TextAreas不可调整大小

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

不要使用GridLayout。 GridLayout将占用所有可用空间。因此,第一个组件占用了100%的空间。当您有两个时,每个占50%。

请改为使用BoxLayoutGridBagLayout

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

  

然后使滚动条最终有用

已经收到安德鲁的上述评论。