gpt4 book ai didi

java - 设置为不可编辑时如何在 JTextArea 中设置自动滚动?

转载 作者:行者123 更新时间:2023-11-29 05:26:53 28 4
gpt4 key购买 nike

我用过this回答以获得在 JTextArea 上自动滚动的功能。但它似乎只有在同一 JTextArea 的 setEditable 属性设置为 TRUE 时才有效。当然,我使用的是 JScrollPane。

我正在开发一个聊天应用程序作为一个大学项目。显示消息的区域与 JTextArea 完全相同。我在上面链接的第一个答案中使用了第二组代码。但是当 setEditable 设置为 FALSE 时,我需要它来使其工作。

即使在 JTextArea 和 JScrollPane 的 setAutoScrolls 属性设置为 TRUE 后,它也不会工作。

请帮忙。谢谢。

最佳答案

I have used this answer to get the functionality of Autoscrolling on a JTextArea. But it seems that it works only when the setEditable property of the same JTextArea is set to TRUE. Of course, i use a JScrollPane

  • 我没有发现 setEditable(false) 和/或 setEnabled(false) 及其可能的组合有任何问题

    <
  • (在黑暗中疯狂拍摄)不包括 EventDispatchThread 的问题, 当 setText, append等未在 EDT 上完成,例如包装到 invokeLater()

.

import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.DefaultCaret;

public class CaretAndJTextArea {

private JTextArea textArea = new JTextArea();
private static final String string =
"Trail: Creating a GUI with JFC/Swing\n"
+ "Lesson: Learning Swing by Example\n"
+ "This lesson explains the concepts you need to\n"
+ " use Swing components in building a user interface.\n"
+ " First we examine the simplest Swing application you can write.\n"
+ " Then we present several progressively complicated examples of creating\n"
+ " user interfaces using components in the javax.swing package.\n"
+ " We cover several Swing components, such as buttons, labels, and text areas.\n"
+ " The handling of events is also discussed,\n"
+ " as are layout management and accessibility.\n"
+ " This lesson ends with a set of questions and exercises\n"
+ " so you can test yourself on what you've learned.\n"
+ "http://docs.oracle.com/javase/tutorial/uiswing/learn/index.html\n";

public CaretAndJTextArea() {
DefaultCaret caret = (DefaultCaret) textArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
textArea.setEditable(false);
textArea.setEnabled(false);
textArea.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
setModelText();
}

@Override
public void removeUpdate(DocumentEvent e) {
setModelText();
}

@Override
public void changedUpdate(DocumentEvent e) {
setModelText();
}

private void setModelText() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
//textArea.setText(textArea.getText());
}
});
}
});
JButton button2 = new JButton("Append String");
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textArea.append(string);
}
});
JFrame frame = new JFrame();
frame.add(new JScrollPane(textArea), BorderLayout.CENTER);
frame.add(button2, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new CaretAndJTextArea();
}
});
}
}

关于java - 设置为不可编辑时如何在 JTextArea 中设置自动滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22379973/

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