30 秒倒计时器 "Who wants to be a millionaire" netbeans

30 second countdown timer "Who wants to be a millionaire" netbeans

所以我正致力于在 netbeans 中制作 "Who what to be a millionaire" 的一个版本,但我遇到了计时器问题。我的工作代码基本上在 11 秒后将数字的颜色更改为红色,并在 1 秒后消失(变为白色)。我想要做的是让数字在 5、4、3、2、1 闪烁的第二个 6 之后闪烁。但我找不到实现这一目标的方法。我试过改变

Thread.sleep(1000);

所以我可以写一个更详细的 if 语句,比如

if (counter < 5.75 )
  g.setColor(Color.WHITE);
if (counter < 5.25 )
  g.setColor(Color.BLACK);

但是没用..

这是我到目前为止所做的:

package timer2;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;



 import javax.swing.JFrame;
 import javax.swing.JPanel;

public class thread  extends JPanel implements Runnable {
private static Object ga;


 int counter;
 Thread cd;
 public void start() { 
 counter =30 ; 
 cd = new Thread(this);

 cd.start();

 }

 public void stop()
 {
     cd = null;
 }

public void run() { 
     while (counter>0 && cd!=null) {
     try
     {
         Thread.sleep(1000);
     }
     catch (InterruptedException e)
     {

     }
         --counter; 


               }

             } 


     public void paintComponent(   Graphics g)
    {

        repaint();
        super.paintComponent(g);
              g.setColor(Color.BLACK);

              if (counter < 1 )
                  g.setColor(Color.WHITE);
              g.setFont(new Font("Times New Roman",Font.BOLD,35));


              if (counter < 11)
                  g.setColor(Color.RED);
              if (counter < 1 )
                  g.setColor(Color.WHITE);







              g.setFont(new Font("Times New Roman",Font.BOLD,100));

              g.drawString(String.valueOf(counter),600,600);

    }
 public static void main(String[] args) {
     JFrame j=new JFrame();
     thread t=new thread();
     t.setBackground(Color.WHITE);
     t.start();

     j.add(t);


     j.setVisible(true);
        j.getContentPane().setBackground( Color.WHITE );
        j.setBounds(-8,-8,500,500); 
        j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        j.setExtendedState(JFrame.MAXIMIZED_BOTH);    
 }

}
  1. 首先也是最重要的 -- 从 paintComponent 中获取 repaint()。这永远不应该从那里调用。
  2. 您想从计时器代码本身调用重绘。
  3. 为方便起见,请避免直接使用后台线程和 while (true) 循环,而是希望使用更简单的 Swing Timer 来进行计数。
  4. 我在那里没有看到任何闪烁的代码 -- 你是怎么做到的?
  5. 此外,我建议您尝试改进此处 post 代码的格式以及一般代码。良好的格式设置(包括使用统一一致的缩进样式)将帮助其他人(我们!)更好地理解您的代码,更重要的是,它将帮助 以更好地理解您的代码,从而修复您自己的错误。它还表明您愿意付出额外的努力,让这里的志愿者更容易帮助您,这种努力非常值得赞赏。

  6. 要闪光 -- 使用周期较短的定时器,例如 200 毫秒,并更改定时器内的颜色。

例如,

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Formatter;

import javax.swing.*;

@SuppressWarnings("serial")
public class CountDownTimer extends JPanel {
    private static final String FORMAT = "%02d";
    private static final Color[] TIMER_COLORS = {Color.BLACK, Color.WHITE};
    private static final int LABEL_PTS = 90;
    private static final Font TIMER_LABEL_FONT = new Font("Times New Roman", Font.BOLD, LABEL_PTS);
    private static final int PREF_W = 600;
    private static final int PREF_H = PREF_W;
    private static final int TIMER_DELAY = 100;
    public static final int FLASH_TIME = 6;
    private JLabel timerLabel = new JLabel("");
    private Timer timer;
    private int timerColorIndex = 0;

    public CountDownTimer(int seconds) {
        setTimerCount(seconds);
        setLayout(new GridBagLayout());
        add(timerLabel);

        timer  = new Timer(TIMER_DELAY, new TimerListener(seconds));

        timer.start();
    }

    public final void setTimerCount(int count) {
        String text = String.format(FORMAT, count);
        timerLabel.setText(text);
        timerLabel.setFont(TIMER_LABEL_FONT);
    }

    public void flash() {
        timerColorIndex++;
        timerColorIndex %= TIMER_COLORS.length;
        timerLabel.setForeground(TIMER_COLORS[timerColorIndex]);
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }

    private class TimerListener implements ActionListener {
        private int mSeconds;

        public TimerListener(int secondsLeft) {
            this.mSeconds = 1000 * secondsLeft;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            mSeconds -= TIMER_DELAY;
            int seconds = (mSeconds + 999) / 1000;
            if (seconds < FLASH_TIME) {
                flash();
            }
            setTimerCount(seconds);
            if (seconds == 0) {
                ((Timer) e.getSource()).stop();
            }
        }
    }

    private static void createAndShowGui() {
        int seconds = 20;
        CountDownTimer mainPanel = new CountDownTimer(20);

        JFrame frame = new JFrame("CountDownTimer");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            createAndShowGui();
        });
    }
}