- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一款文本冒险游戏,但遇到了一个问题,我无法按照我想要的方式显示一些文本。当输入一些单词时,玩家可以开始引入一个新房间。我希望这个介绍具有“打字机”效果。该事件需要在我的程序 ActionPerformed 方法中发生。例如,当用户键入“Move”然后按 Enter 键时,我希望生成的文本一次打印一个字符。
这是我在 ActionPerformed 之外使用的当前方法来实现此效果:
public void slowPrint(String message, long millisPerChar)
{
//makes it so that the player cannot input while the text is being displayed
userinput.setEditable(false);
String o;
for (int i = 0; i < message.length(); i++)
{
//adds each letter one-by-one
o = "" + message.charAt(i);
output.append(o);
//moves the JTextArea to the bottom
output.setCaretPosition (output.getDocument ().getLength ());
//delay so that you can see each letter being added
try {
Thread.sleep(millisPerChar);;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace(); }
}
//after the text is displayed allow the user to input again
userinput.setEditable(true);
}
如何获得类似的方法来使用 Timer 类,这样它就不会阻止我的线程更新 GUI。
这是我使用相关方法执行的操作:
public void actionPerformed(ActionEvent e)
{
s = userinput.getText();
output.append("\n");
userinput.setText("");
for (int i = 0; i<whatRoom.length; i++)
{
if (whatRoom[i])
{
switch(i)
{
case 0:
controlOne(s);
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
}
}
}
}
以及“controlOne()”方法:
private void controlOne(String s)
{
if(one.getHasLight() == true)
{
if(!one.getPushYes())
{
output.append(one.dealWithLight(s, output));
}
else output.append(one.commands(s));
}
else output.append(one.commands(s));
}
我不想一直追加,而是想使用类似于我的“slowPrint”方法的方法。
希望这是有道理的,任何帮助将不胜感激。
最佳答案
Timer
的两个基本值是
ActionListener
如果您在 slowPrint
方法中创建计时器,则保留对其的引用非常重要,以便稍后可以停止它。因此您必须稍后添加 ActionListener
。在 ActionListener 内,您可以通过附加变量来跟踪当前文本位置(忘记 for 循环)。然后您只需手动检查所有文本是否已打印,并相应地停止计时器。
如果您希望最初也发生延迟,只需调用timer.setInitialDelay(delay)
即可。
修改 slowPrint
方法的示例:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.Timer;
public class Foo {
private JTextField input;
private JTextArea output;
public static void main(String[] args) throws IOException {
EventQueue.invokeLater(() -> new Foo().createAndShowGUI());
}
public void createAndShowGUI() {
output = new JTextArea();
output.setEditable(false);
input = new JTextField();
input.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
slowPrint("This is a test answer.\n", 100);
}
});
JPanel contentPane = new JPanel(new BorderLayout(5, 5));
contentPane.add(input, BorderLayout.NORTH);
contentPane.add(output);
JFrame frame = new JFrame("Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(contentPane);
frame.setSize(800, 600);
frame.setVisible(true);
}
public void slowPrint(String message, int millisPerChar) {
input.setEditable(false);
input.setFocusable(false);
Timer timer = new Timer(millisPerChar, null);
timer.addActionListener(new ActionListener() {
int counter = 0;
@Override
public void actionPerformed(ActionEvent e) {
output.append(String.valueOf(message.charAt(counter++)));
output.setCaretPosition(output.getDocument().getLength());
if (counter >= message.length()) {
timer.stop();
input.setEditable(true);
input.setFocusable(true);
input.requestFocusInWindow();
}
}
});
timer.start();
}
}
关于java - 在JTextArea中使用Timer实现打字机效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39283063/
我有一个简单的 GUI,其中有一个 JTextArea。我创建了一个方法来从用户获取消息,另一个方法将文本附加到文本区域,如下所示 Message m = new Message(); ... pri
我正在使用 JList,并且尝试对单元格使用 JTextAreas(实现 ListCellRenderer)。它不起作用。这些单元格仅显示 ListCellRenderer.toString() 而不
此代码计算 JTextArea 的每一行并添加行数 左 JTextPane import java.awt.BorderLayout; import java.awt.Color; import ja
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
我必须将一个jtextarea的内容复制到另一个jtextarea。怎么办?我已经做了以下操作:但是这个程序正在将一个jtext区域的文本逐个字符写入另一个jtext区域。我希望它在用户按下回车键时复
在我的 GUI 中,JScrollPane 中有一个附加到容器的 JTextArea。 ta = new JTextArea(); jsp = new JScrollP
我正在编写一些测试代码来练习 OOP,我想将 JTextArea 从“writeToArea”附加到定义和初始化 JTextArea 的“initialize”方法。我已经尝试直接调用“输出”变量,但
JComboBox cBox = new JComboBox(); cBox.addItem("Food"); String x = "Food"; cBox.addItem("Shi
我正在尝试将一个 JTextArea 放在 GUI 中的另一个 JTextArea 旁边 我正在为数据库编写 GUI,并希望将每列的数据放在不同的 JTextArea 中。这将使我的 GUI 看起来更
这是一个带有 JTextarea 的弹出 Jpanel,但我有一个问题。当我将鼠标移到 JTextarea 上时,它会闪烁。为什么会出现这种情况? 在 Debug模式下,鼠标移动会生成 mouseEx
我有一个类将输出显示到 JTextArea 中。意味着成功运行后,它将在文本区域中显示输出。 我还有一个主类,它将类与几个按钮组合在一起,以启动特定类中代码的执行。这个主类创建了一个带有几个按钮的 G
我的问题在于我的 DocumentLister AreaListener。我似乎无法弄清楚如何将用户输入的文本传递到一个 JTextArea 中进行转换,并将其返回到另一个 JTextArea。 该程
我有一个对象ReminderGUI其中有 JTextArea field 。 ReminderGUI代表一个可以保存和显示提醒的应用程序。当getReminderButton单击我希望应用程序找到之前
我目前正在使用 Swing 开发控制台窗口。它基于 JTextArea 并且像普通命令行一样工作。您在一行中键入一条命令,然后按回车键。在下一行中,显示了输出,在该输出下,您可以编写下一条命令。 现在
我开发了一个 Swing GUI,其中我尝试使用按钮使用另一个文本区域中的文本填充文本区域。 代码: private void jButton1ActionPerformed(java.awt.eve
当我制作时,我有一个包含 JPanel 的小型 GUI,其中有 JTextArea 和 JLabel panel1.setLayout(null); 我可以完成所需的位置,但 JTextArea 消失
我在 JDialog 框中有一个 JTabbedPane,它在 Pane 中包含的所有 JPanels 上使用 GridBagLayout 。在显示的第一个面板上有一个 JTextArea (desc
我搜索了答案,但我找到的只是解决方法,而不是原因,所以我问这个问题: 我是 GUI 编程的新手。在练习一些有关关键事件处理的代码时,我遇到了一个示例,该示例在 JFrame 中包含一个 JTextAr
将有 91 个文本区域,每个区域都显示一个值。我试图找到一种更有效的方法来解决这个问题,而不是只是盲目地添加 JTextAreas 并尝试管理 91 个文本区域中每个区域的实例化名称。 我愿意接受有关
private JPanel contentPane; public Driver() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
我是一名优秀的程序员,十分优秀!