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

问题描述 投票:0回答:1

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

这是我的测试代码来说明:

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

    }
}
java swing jpanel jscrollpane
1个回答
1
投票
JPanel RXCommand = new JPanel();

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

如何设置TextArea不可调整大小

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

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

而是使用BoxLayoutGridBagLayout

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

然后使滚动条最终有用

安德鲁的上述评论已经回答了这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.