java - 使用 JScrollPane 自动滚动 JTextArea
问题描述
我浏览了 StackOverflow 并尝试了许多不同的东西。我会给出一些链接:How to set AUTO-SCROLLING of JTextArea in Java GUI? ,以及我使用过的 Oracle 网站:https ://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html 。但他们都没有帮助我。
txtara = new JTextArea("");
panel = new JPanel();
txtfld = new JTextField("");
scrollBar = new JScrollPane(txtara,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollBar.setPreferredSize(new Dimension(10, 10));
txtfld.setPreferredSize(new Dimension(740, 20));
txtfld.setLocation(new Point (0, 510));
txtfld.setBackground(Color.BLACK);
txtfld.setForeground(Color.WHITE);
txtara.setPreferredSize(new Dimension(740, 510));
txtara.setBackground(Color.BLACK);
txtara.setForeground(Color.WHITE);
txtara.setEditable(false);
panel.setPreferredSize(new Dimension(750, 575));
panel.setForeground(Color.BLACK);
panel.setBackground(Color.BLACK);
panel.add(scrollBar);
panel.add(txtara);
panel.add(txtfld);
DefaultCaret caret = (DefaultCaret)txtara.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
scrollBar.setViewportView(txtara);
解决方案
推荐阅读
- ios - 如何更改水平滚动集合视图中单元格之间的水平间距
- node.js - 用于授权外部应用 NodeJS 的令牌
- c# - Control.InvokeRequired 在任务中为假
- git - Microsoft Sql Server 分析服务 (SSAS) 上的版本控制
- angular - 在现有的 Angular 9 应用程序中集成 express api
- kotlin - 枚举接口数组的类型推导失败
- wso2 - 授权策略 (PDP) 不适用于 WSO2 身份服务器 5.10.0 中的联合用户
- javascript - 如何等待元素完全加载到 DOM 中?
- sql - sql查询查找员工处理的订单
- c - D 类型限定符对强制类型转换没有意义