JLabel显示倒计时,java

我在一个类(名为Welcome)中有一个“status”JLabel,在另一个类中有一个定时器(名为Timer)。 现在,第一个显示“状态”一词,第二个显示倒计时。 我希望它的方式,但不知道如何 – 显示10,9,8,7 … 0(然后转到下一个窗口)。 我到目前为止的尝试:

// class Welcome setLayout(new BorderLayout()); JPanel area = new JPanel(); JLabel status = new JLabel("status"); area.setBackground(Color.darkGray); Font font2 = new Font("SansSerif", Font.BOLD, 25); status.setFont(font2); status.setForeground(Color.green); area.add(status, BorderLayout.EAST); // can I put it in the bottom-right corner? this.add(area); 

和计时器:

  public class Timer implements Runnable { // public void runThread() { // new Thread(this).start(); // } public void setText(final String text) { SwingUtilities.invokeLater(new Runnable() { public void run() { setText(text); // link to status here I guess } }); } public void run() { for (int i = 10; i > 0; i--) { // set the label final String text = "(" + i + ") seconds left"; setText(text); // // sleep for 1 second // try { // Thread.currentThread(); // Thread.sleep(1000); // } catch (Exception ex) { // } } // go to the next window UsedBefore window2 = new UsedBefore(); window2.setVisible(true); } public static void main(String[] args) { // TODO Auto-generated method stub // runThread(); } } // end class 

我同意您应该考虑按照Anh Pham使用“Java”计时器,但实际上,有几个Timer类可用,并且为了您的目的,Anh建议的Swing Timer而不是java.util.Timer适合您的目的最好。

至于你的问题,它只不过是一个简单的引用问题。 为类赋予标签一个公共方法,比如setCountDownLabelText(String text) ,然后从包含计时器的类中调用该方法。 您需要在另一个类中使用计时器JLabel引用GUI类。

例如:

 import java.awt.BorderLayout; import java.awt.CardLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class Welcome extends JPanel { private static final String INTRO = "intro"; private static final String USED_BEFORE = "used before"; private CardLayout cardLayout = new CardLayout(); private JLabel countDownLabel = new JLabel("", SwingConstants.CENTER); public Welcome() { JPanel introSouthPanel = new JPanel(); introSouthPanel.add(new JLabel("Status:")); introSouthPanel.add(countDownLabel); JPanel introPanel = new JPanel(); introPanel.setPreferredSize(new Dimension(400, 300)); introPanel.setLayout(new BorderLayout()); introPanel.add(new JLabel("WELCOME", SwingConstants.CENTER), BorderLayout.CENTER); introPanel.add(introSouthPanel, BorderLayout.SOUTH); JPanel usedBeforePanel = new JPanel(new BorderLayout()); usedBeforePanel.setBackground(Color.pink); usedBeforePanel.add(new JLabel("Used Before", SwingConstants.CENTER)); setLayout(cardLayout); add(introPanel, INTRO); add(usedBeforePanel, USED_BEFORE); new HurdlerTimer(this).start(); } private static void createAndShowUI() { JFrame frame = new JFrame("Welcome"); frame.getContentPane().add(new Welcome()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } public void setCountDownLabelText(String text) { countDownLabel.setText(text); } public void showNextPanel() { cardLayout.next(this); } } class HurdlerTimer { private static final int TIMER_PERIOD = 1000; protected static final int MAX_COUNT = 10; private Welcome welcome; // holds a reference to the Welcome class private int count; public HurdlerTimer(Welcome welcome) { this.welcome = welcome; // initializes the reference to the Welcome class. String text = "(" + (MAX_COUNT - count) + ") seconds left"; welcome.setCountDownLabelText(text); } public void start() { new Timer(TIMER_PERIOD, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (count < MAX_COUNT) { count++; String text = "(" + (MAX_COUNT - count) + ") seconds left"; welcome.setCountDownLabelText(text); // uses the reference to Welcome } else { ((Timer) e.getSource()).stop(); welcome.showNextPanel(); } } }).start(); } } 

由于您使用的是Swing,因此应使用javax.swing.Timer ,而不是java.util.Timer。 您可以将计时器设置为以1秒(1000毫秒)的间隔触发,并让您的监听器进行更新。 由于Swing更新必须在事件派发线程中进行,因此您的侦听器是status.setText的理想位置。

java中已有一个Timer类: http : //www.exampledepot.com/egs/java.util/ScheduleRepeat.html

为什么不把setText方法放在welcome类中,只做’status.setText(text)’?

您可以尝试使用BorderLayout.SOUTH或.PAGE END或.LINE END来获取右下角的计时器