- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经在这个问题上苦苦挣扎了一段时间,所以我想我应该寻求一些帮助。
我正在为一个游戏创建一个应用程序,它需要我制作一个动态倒计时器。我所说的动态是指能够以消费者的身份放入您想要的倒计时器。我遇到的问题是让我的代码等待 1000 毫秒才能在正确的时间执行更新代码。我正在尝试使用 sleep
功能来执行此操作...
这不是我正在制作的应用程序,这只是为了尽可能地为愿意帮助我的人简化我的问题。这里的所有内容都直接来自 Eclipse IDE 中的 WindowBuilder。我遇到的问题是让 Thread thread = new Thread();
与 Thread.sleep(1000);
一起工作以实现完整的 1 秒延迟。
package test;
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import javax.swing.SwingConstants;
import java.awt.Font;
public class test {
private JFrame frame;
/**
*
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
test window = new test();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public test() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(Color.RED);
JLabel lblNewLabel = new JLabel("Test");
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 62));
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
frame.getContentPane().add(lblNewLabel, BorderLayout.CENTER);
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Thread thread = new Thread();
for(int i = 60;i>=0;i--){
thread.sleep(500);
lblNewLabel.setText("Test" + i);
}
}
}
如果您将此代码放入 IDE 中,则会出现Unhandled exception InterruptedException
错误。如果我添加 throws 声明,代码就会把一切搞乱,说实话我不知道问题出在哪里。
如何修复或解决此问题?
最佳答案
这是另一种方法。请注意此代码未经测试。
private void initialize() {
...
new Thread() {
int counter = 10;
public void run() {
while(counter >= 0) {
lblNewLabel.setText("Test" + (counter--));
try{
Thread.sleep(1000);
} catch(Exception e) {}
}
}
}.start();
}
关于java - 在 JFrame 内创建倒计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38175659/
我想在 android 中扫描黑底白字条码。我使用过 zxing,它允许我只扫描白底黑字。我如何扫描和倒置条形码或使用哪个库?感谢您的帮助。 最佳答案 如果您仍在引用 journeyapps 嵌入式
所以我在 youtube 上观看了一些介绍性类(class)以学习 OpenGL 的基础知识并学习了诸如制作三角形和简单相机类等内容。我一直想尝试制作体素引擎,这显然是第一个我想做的是一个我最终可以复
这个问题在这里已经有了答案: Div with cut out edges, border and transparent background (6 个答案) 关闭 8 年前。
我有一张图片,我正在查看用 HTML 创建的小型网站的基本定制。 我知道您可以对图像进行倒 Angular 处理,如 this question here 中所示,这给出了 45 度切割。 我希望每个
我必须在 iOS 上创建一个自定义形状(倒 T)边框的 Uiview。我附上下面的截图。我进行了很多研究,找到了一种使用 here 中的 UIBezierPath 的方法. 但我不知道如何将我的 Vi
我是一名优秀的程序员,十分优秀!