Rya*_*ull 1 java swing jtextarea
我正在为单人作业制作扑克游戏,我想知道是否有任何一种方法可以执行以下操作(注意:非常粗略地编写了代码)
JTextArea textArea = new JTextArea();
public void printer(String s){
//I want to delay here, for 2 seconds each time i print to the jtextarea
textArea.append(s);
}
public void runGame(){
printer("Dealing cards...");
//I want to delay to add to an effect of the time it takes to actually deal the cards
pokerHand.setVisibility(true);
printer("What you like to do?");
//
//Code here containing running the game
//
printer("Daniel Negreanu folds");
//I want to have a delay here for the time it takes to make a decision.
printer("Phil Hellmuth folds");
Run Code Online (Sandbox Code Playgroud)
我想在整个程序中使用更多实例,只是想知道是否有任何方法可以做到这一点。
提前致谢
编辑:不希望使用Thread.sleep(),因为它与gui不能很好地配合。EDIT2:我希望pokerHand.setVisibility(true)和我代码中的其他方法在延迟之后执行(使用计时器不会执行此操作)。
不希望使用Thread.sleep(),因为它不适用于gui。
好,使用一个Swing Timer代替
我希望pokerHand.setVisibility(true)和我代码中的其他方法在延迟后执行(使用计时器不会执行此操作)。
是的,的确如此,您只是没有正确使用它,但是由于您没有提供任何实际的代码,我不能说“如何”表示您没有正确地使用它,仅凭其声音就可以了。
首先查看如何使用Swing计时器以了解更多详细信息
以下是一个非常简单的示例,它使用a Timer来计算和打印您单击按钮之间的时间间隔。
该示例在Timer启动后更新UI ,但是直到Timer完成后,才生成计算和结果
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.Duration;
import java.time.LocalTime;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JTextArea ta;
private JButton btn;
private Timer timer;
private LocalTime startTime, endTime;
public TestPane() {
setLayout(new BorderLayout());
ta = new JTextArea(10, 20);
add(new JScrollPane(ta));
btn = new JButton("Click");
add(btn, BorderLayout.SOUTH);
timer = new Timer(2000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
endTime = LocalTime.now();
Duration duration = Duration.between(startTime, endTime);
ta.append("Ended @ " + endTime + "\n");
ta.append("Took " + (duration.toMillis() / 1000) + " seconds\n");
}
});
timer.setRepeats(false);
ta.setEditable(false);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
timer.start();
startTime = LocalTime.now();
btn.setEnabled(false);
ta.append("Start @ " + startTime + "\n");
}
});
}
}
}
Run Code Online (Sandbox Code Playgroud)