Tag: 计时器

可以用Swing Timer以更优雅的方式完成吗?

Bellow是最简单的GUI倒计时的代码。 使用Swing计时器可以以更短更优雅的方式完成同样的工作吗? import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingUtilities; public class CountdownNew { static JLabel label; // Method which defines the appearance of the window. public static void showGUI() { JFrame frame = new JFrame(“Simple Countdown”); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); label = new JLabel(“Some Text”); frame.add(label); frame.pack(); frame.setVisible(true); } // Define a new thread in which the countdown is […]

如何从其他类及其TextView中触发计时器并分别在另一个xml和类中实现?

我正在研究Android测验,并希望在我的每个问答页面上都有计时器。 我的测验和播放按钮中有菜单页面以开始游戏。 我希望这个计时器在我点击播放按钮时被触发。 为此,我必须创建代表我的菜单页面的问题XML TextView。 并在QuestionActivity类中实现,代表我的第一个问题页面。 我也发布了WelcomeActivity类,尽管它在这个问题上没有任何作用。 播放按钮布局 问题表示TextView for Timer的XML QuestionActivity我实现了定时器代码 public class QuestionActivity extends Activity implements OnClickListener{ private Question currentQ; private GamePlay currentGame; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.question); /** * Configure current game and get question */ currentGame = ((CYKApplication)getApplication()).getCurrentGame(); currentQ = currentGame.getNextQuestion(); Button nextBtn1 = (Button) findViewById(R.id.answer1); nextBtn1.setOnClickListener(this); Button […]

在Java中并发执行计划任务

我有一个TimerTask ,旨在以特定间隔收集指标。 但是,任务执行的时间段可能小于任务执行的时间(有时会出现超时和延迟的情况)。 有没有办法同时执行多个TimerTasks或Runnables,线程等,而无需等待上一个任务完成? 我知道Timer使用单个线程,而ScheduledThreadPoolExecutor将延迟执行而不管速率如何。 谢谢。

如何在Swing中更新JLabel?

我正在尝试使用Swing Timer,我想从一个非常简单的程序开始。 我有一个带文字的窗口:“你有n秒”,其中n每秒从10变为0。 我知道如何生成一个带文本的窗口。 我理解Timer如何工作(它会定期启动一个动作)。 但我无法弄清楚如何梳理这两件事。 我应该使用它: JLabel label = new JLabel(myMessage); 然后用计时器我需要更新“myMessage”变量? 但我认为我需要“强制”我的窗口“更新”自己(显示存储在“myMessage”中的新值)。

用于游戏循环的Java TimerTick事件

我尝试使用java.util.Timer中的Timer在Java中进行游戏循环。 在计时器滴答声期间,我无法让我的游戏循环执行。 以下是此问题的示例。 我试图在游戏循环期间移动按钮,但它没有继续计时器滴答事件。 import java.util.Timer; import java.util.TimerTask; import javax.swing.JFrame; import javax.swing.JButton; public class Window extends JFrame { private static final long serialVersionUID = -2545695383117923190L; private static Timer timer; private static JButton button; public Window(int x, int y, int width, int height, String title) { this.setSize(width, height); this.setLocation(x, y); this.setTitle(title); this.setLayout(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); timer = […]

试图对计时器进行编程,以便用户只能在一定时间内输入单词

我正在尝试制作一个只能输入10秒钟的单词的游戏。 我试图创建一个multithreading解决方案,但它无法正常工作。 class timer extends Thread{//thread public void run(){ for(int i=10;i>=0;i–){ System.out.print(i+” “); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } 主要方法: timer t=new timer(); t.start(); while () {//not sure what to put in my while statement System.out.print(“Guess a word on the board! “); if(test.CheckGame(scan.next())==true){ […]

java Swing计时器一个接一个地执行几个任务

我完全清楚很少有类似的问题。 我试图实施提供的解决方案 – 徒劳无功。 …我面临的问题是在另一个之后闪烁按钮。 我可以做一个,但是当按顺序闪烁时 – 一切都会中断。 任何对Java新人的帮助都表示赞赏。 PS我不允许使用Threads。 我现在拥有的是: Timer colorButton = new Timer(1000, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { for (int i = 0; i < pcArray.length; i++) { playSquare = pcArray[i]; System.out.println("PlaySquare " + playSquare); if (playSquare == 1) { if (alreadyColoredRed) { colorBack.start(); colorButton.stop(); } else { […]

Java时钟在Swing中不算数

我正在尝试使用swing制作秒表,但它无法正常工作。 这是我的代码。 Jlabel时钟始终显示-1,只有在它停止时才会发生。 我正确使用了invokelater吗? import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class sidePanel extends JApplet implements ActionListener{ JPanel pane; JLabel clock; JButton toggle; Timer timer; StopWatch stopWatch; public void init() { pane = new JPanel(); pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS)); clock = new JLabel(“00:00”); toggle = new JButton(“Start/Stop”); toggle.addActionListener(this); pane.add(clock); pane.add(toggle); timer = new Timer(500, this); timer.setRepeats(true); […]

使用用户指定的计时器更新gui中的JPanel和属性?

所以我有这门课。 我们称之为ProcessBox。 我有另一个名为Process的类,它有一个ProcessBox作为成员变量。 然后,在我的main方法中,我有一个Process对象的ArrayList(每个都有自己的ProcessBox)。 我将如何制作它以便我的ProcessBox每x ms更新一次(其中x是用户指定的。我已经有了输入x的监听器)。 下面是我的ProcessBox类。 这是我希望每x毫秒重绘一次(除了它们的整个列表)。 import java.awt.*; import javax.swing.*; import javax.swing.border.LineBorder; import java.util.*; public class ProcessBox extends JPanel { GridBagLayout gbc_panel; public ProcessBox(Process p) { super(new GridBagLayout()); gbc_panel = new GridBagLayout(); gbc_panel.columnWidths = new int[] { 0, 0, 0 }; gbc_panel.rowHeights = new int[] { 0, 0, 0, 0, 0, 0 }; […]

摇摆计时器不停止

我正在使用swing计时器在Netbeans中制作一个倒计时时钟: public void startTimer() { System.out.println(right + “value”); ActionListener listener = new ActionListener() { @Override public void actionPerformed(ActionEvent event) { System.out.println(“action”); timerLabel.setText(“” + seconds); –seconds; System.out.println(seconds); if (seconds == -1 && seconds < 0) { System.out.print("zero"); //displayTimer.stop(); wrong(); dispose(); } } }; displayTimer = new Timer(1000, listener); displayTimer.setInitialDelay(100); displayTimer.start(); if (right == null) { System.out.println("null"); […]