gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:02:41 24 4
gpt4 key购买 nike

我的目标是在一个框中显示通过 UDP 服务器接收到的消息。为此,我创建了一个 JScrollBar,并在其中添加了一个 JPanel。当我收到消息时,会创建扩展 JTextArea 的对象 ReceivedCommand 并将其添加到 JPanel。我的问题是,当 JPanel 中显示太多消息时,它会自动调整 TextArea 的大小。如何设置 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();

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

How can I set the TextAreas not resizable

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

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

请使用 BoxLayoutGridBagLayout

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

and then make the scrollbar finally usefull

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

关于java - 带有滚动条的 JPanel 设置内容不可调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55516242/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com