30秒倒数计时器“谁想成为百万富翁”的netbeans

时间:2016-03-25 12:12:17

标签: java netbeans timer counter countdown

所以我正在努力制作一个netbeans中的“谁将成为百万富翁”的版本,我遇到了计时器的问题。我的工作代码基本上在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 个答案:

答案 0 :(得分:0)

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

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

  7. 如,

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