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



所以我正在netbeans中制作一个版本的"谁是百万富翁",但我的计时器有问题。我的工作代码基本上在11秒后将数字的颜色更改为红色,并在1秒后消失(变为白色)。我想做的是让数字在第6秒后从5,4,3,2,1闪烁。但我找不到让它发生的方法。我试过更换

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. 此外,我建议您尝试改进您在这里发布的代码和您的代码的格式。良好的格式设置,包括使用统一一致的缩进样式,将帮助其他人(us!)更好地理解您的代码,更重要的是,它将帮助更好地理解代码,从而修复您自己的错误。这也表明你愿意付出额外的努力,让这里的志愿者更容易帮助你,我们非常感谢你的努力。

  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();
        });
    }
}

相关内容

最新更新