Java JTextPane 自动滚动在选择文本后停止工作
Java JTextPane auto-scroll stops working after selecting text
所以这个问题困扰了我很长时间了,如果它被触发,它实际上会使我的游戏无法玩。
情况是我的 GUI 中有四个项目:
private JPanel panel;
private JTextPane content;
private JScrollPane scroll;
private JTextField input;
整个事情都在 BorderLayout 中设置,带有插入符更新策略,当文本到达底部时自动滚动屏幕。但是,如果我 select JTextPane 中的任何文本,自动滚动会突然停止工作,添加到窗格的任何新文本将保持不可见,直到用户手动滚动滚动条。每次附加文本时,我都尝试重新应用插入符号更新策略,但这没有用。我不知道如何解决这个问题,并且尝试 google 这个问题没有结果。
作为参考,这里是构造函数的相关代码:
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(width, height);
setResizable(false);
panel = new JPanel();
input = new JTextField(30);
input.setBackground(Color.BLACK);
input.setForeground(Color.GREEN);
input.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, width / 40));
input.addActionListener(this);
content = new JTextPane();
scroll = new JScrollPane(content);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setPreferredSize(new Dimension(width, height - 80));
scroll.setMinimumSize(new Dimension(640, 480));
scroll.setBorder(null);
content.setBackground(Color.BLACK);
content.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, width / 40));
content.setEditable(false);
DefaultCaret caret = (DefaultCaret) content.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
panel.setLayout(new BorderLayout());
panel.add(scroll, BorderLayout.NORTH);
panel.add(input, BorderLayout.SOUTH);
panel.setBackground(Color.BLACK);
getContentPane().add(panel);
setVisible(true);
是否有可能的解决方案,或者这是 Java AWT 的限制?
I've attempted reapplying the caret update policy every time text is appended but that didn't work.
您还需要将插入符号重置为文档末尾:
textPane.getDocument().insertString(...);
textPane.setCaretPosition(textArea.getDocument().getLength());
所以这个问题困扰了我很长时间了,如果它被触发,它实际上会使我的游戏无法玩。 情况是我的 GUI 中有四个项目:
private JPanel panel;
private JTextPane content;
private JScrollPane scroll;
private JTextField input;
整个事情都在 BorderLayout 中设置,带有插入符更新策略,当文本到达底部时自动滚动屏幕。但是,如果我 select JTextPane 中的任何文本,自动滚动会突然停止工作,添加到窗格的任何新文本将保持不可见,直到用户手动滚动滚动条。每次附加文本时,我都尝试重新应用插入符号更新策略,但这没有用。我不知道如何解决这个问题,并且尝试 google 这个问题没有结果。 作为参考,这里是构造函数的相关代码:
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(width, height);
setResizable(false);
panel = new JPanel();
input = new JTextField(30);
input.setBackground(Color.BLACK);
input.setForeground(Color.GREEN);
input.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, width / 40));
input.addActionListener(this);
content = new JTextPane();
scroll = new JScrollPane(content);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setPreferredSize(new Dimension(width, height - 80));
scroll.setMinimumSize(new Dimension(640, 480));
scroll.setBorder(null);
content.setBackground(Color.BLACK);
content.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, width / 40));
content.setEditable(false);
DefaultCaret caret = (DefaultCaret) content.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
panel.setLayout(new BorderLayout());
panel.add(scroll, BorderLayout.NORTH);
panel.add(input, BorderLayout.SOUTH);
panel.setBackground(Color.BLACK);
getContentPane().add(panel);
setVisible(true);
是否有可能的解决方案,或者这是 Java AWT 的限制?
I've attempted reapplying the caret update policy every time text is appended but that didn't work.
您还需要将插入符号重置为文档末尾:
textPane.getDocument().insertString(...);
textPane.setCaretPosition(textArea.getDocument().getLength());