- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想刷新我的 ColorPane 定义如下:
public class ColorPane extends JTextPane{
/* FYI, The function below allows me to add text with ANSI Coloring */
public void appendANSI(String s) {
// ...
}
// ...
}
在下一个 while
语句中,我在控制台样式窗口中写入 line
。因此,作为控制台,我希望在将新行添加到 textPane
时在底部刷新并自动向下滚动。
我想通过按 mainButton
启动以下循环。
public class mainButton implements ActionListener {
public void actionPerformed (ActionEvent e) {
ColorPane textPane = new ColorPane();
/* FYI, in is a BufferedReader */
while ((line = in.readLine()) != null) {
/* Add automatically in the console window */
textPane.appendANSI(line+"\n");
/* Last possible workaround I tried
Which as the same effect than commenting these lines.. */
JScrollBar vertical = scrollPane.getVerticalScrollBar();
vertical.setValue(vertical.getMaximum());
/* I also tried : */
// textPane.setCaretPosition(textPane.getDocument().getLength());
/* Or this : */
// Rectangle rec = GenerationWindow.textPane.getVisibleRect();
// rec.setLocation((int) (rec.getX() + 1000), (int) rec.getY());
// textPane.scrollRectToVisible(rec);
/* Refresh the display */
textPane.update(textPane.getGraphics());
}
}
}
问题是,我的窗口向下滚动,但仅在退出actionPerformed
时才滚动。为什么?
一方面,我可以看到文本在每次循环时都会更新,但另一方面,JScrollPanescrollPane
在函数末尾向下滚动...
我想我怀念这里的一些挥杆哲学..
我已经在 Oracle 文档和 StackO 主题上漫游了几个小时,尝试不同的“解决方案”,但我现在有点绝望......
不自动向下滚动的控制台是..好吧..不是控制台..
最佳答案
textPane.update(textPane.getGraphics());
不要在组件上手动调用 update(...)。 Swing 将确定组件何时需要重新绘制。
The thing is, my window is scrolled down, but only when exiting actionPerformed. Why ?
您的代码正在循环中执行,该循环在事件调度线程(即绘制 GUI 的线程)上执行。在循环完成之前,GUI 无法重新绘制自身。
长时间运行的代码需要在单独的线程上执行,然后当您更新文本 Pane 时,GUI 可以自由地重新绘制自身。
我可能会为此线程使用 Swing Worker
,然后您可以在结果可用时“发布”结果。阅读 Swing 教程中关于 Concurrency 的部分了解使用 SwingWorker 的更多信息和示例。
关于java - JScrollPane 不会向下滚动以正确显示从 ActionListener 调用的 JTextPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31726678/
在另一个 ActionListener 中嵌入一个 ActionListener 更好,还是将它们作为单独的 ActionListener 更好?我有 JComboBox1、JComboBox2 和
我为我的程序制作了一个面板。它仅由 RadioButtons 组成。选择单选按钮后,我想在其他代码中设置一个 boolean 值。此面板将用作更大面板或框架的组件,该面板或框架也应该能够监听此面板内发
我想知道(如果可能的话)如何更新 JButton 的 ActionListener 执行的操作。我需要在整个程序中保留一个按钮,但根据当前情况更改它的功能。 最佳答案 首先使用removeAction
我的程序的先前版本旨在显示多个系列的魔术图像,所有这些图像都使用相同类型的 ActionListener。例如,在显示每幅图像之间设置时间的所有技巧时,我通过使用扩展 ActionListener 的
也许我做错了。让我知道使用 Swing 和 AWT,我在框架上设置了几个按钮,每个按钮都有一个与其特定功能 I.E 相对应的 ActionListener。 JButton foo_button =
我想把第一个ActionListener(About)改成第二个ActionListener(About2)如果不将第一个复制到第二个,有什么办法可以做到这一点吗? About.addActionLi
我想在单击按钮 b 后检查选中了哪些复选框,但是我的复选框是在我单击按钮 b1 之后声明的 我的意思是,我应该全局声明 checkbox[] 吗?我该怎么做? b1.addActionListener
我有一个这样的方法,它被赋予一个 JButton 数组,并在它们被按下时返回它们的文本: public static String foo(JButton[] buttons) { for (
这个问题在这里已经有了答案: Detect enter press in JTextField (10 个答案) 关闭 7 年前。 我有一个具有三个功能的程序;读取文件、写入文件以及在文件中搜索特定
我有一个 actionlistener,当我单击一个按钮时触发,让我们称之为框,我有另一个 actionlistener 用于另一个按钮调用它重新启动。我想要做的是,当我单击一个框按钮时,除了在该 a
我正在想办法如何让一个类(class)听另一个类(class)的课。这就是我们的想法。 我有一个 MainFrame 类,它只是一个容器类,即 JFrame 容器,它采用 JPanel 类型的参数。基
实现 java.awt.event.ActionListener 接口(interface)的最佳方法是什么? 让您的类实现 ActionListener 并将其添加为 ActionListener:
我有一个框架(此处名为“MainApplication”),它主要有一个 JPanel 来根据上下文显示信息。 启动时,MainApplication 有一个空的 JPanel。 然后它创建一个“Lo
我发现已经有人提出了一些关于这个主题的问题,但我还没有找到答案。我正在编写一个代码,其中用户在 JTextField 中键入内容,然后单击按钮后,他的单词将被替换为与他的单词具有相同数量的字符数的星号
我将稍微解释一下我的代码: 我有一个带有项目列表的 JComboBox当按下 JButton“Select”时,它会注册 JComboBox 中最后一个选定项目的最后一个索引。 现在我需要访问主目录中
我收到错误,“AbstractButton 类型中的方法 addActionListener(ActionListener) 不适用于此代码的参数 (new ActionListener(){})”:
哪种方式实现 ActionListener 更正确?性能上有什么重大差异吗? 在类中实现 ActionListener: public class MainFrame implements Actio
在 Java 中,我对 JButton 数组使用 ActionListener。我希望 ActionListener 的较早部分将新的 ImageIcon 设置为 JButton,该更改将立即显示,然
这里有新海报;如果我违反了任何规则/搞砸了任何事情,请告诉我,我会删除该帖子。 我有一个 MainMenu JFrame,里面有一个 JPanel (mainMenu)。 mainMenu 有一个按钮
我在一个主类中有两个 Action Listener 内部类。每一个都对应于它自己的按钮。其中一个 Action 监听器被编码为生成数组列表。另一个只是将该数组列表写入文本字段。 我的问题是如何从其他
我是一名优秀的程序员,十分优秀!