我的目标是在框中显示通过UDP服务器接收的消息。为此,我创建了一个 JScroll 栏,并添加了一个 JPanel。当我收到消息和对象接收时,将创建扩展 JTextArea 并将其添加到 JPanel 中。我的问题是,当我在JPanel中显示太多消息时,它会自动调整我的文本区域的大小。如何设置 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"));
}
}
守着一只汪
相关分类