首页 > 解决方案 > 使用 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);

标签: javaswingjscrollpane

解决方案


推荐阅读