使用JScrollPane自动滚动JTextArea

时间:2019-04-12 13:58:20

标签: java jscrollpane

我浏览了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);

0 个答案:

没有答案