栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

JLabel显示倒计时,Java

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JLabel显示倒计时,Java

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

至于您的问题,实际上只不过是一个简单的引用问题。为带有标签的类提供一个公共方法,例如

setCountDownLabelText(Stringtext)
,然后从保存计时器的类中调用该方法。您需要使用另一个类中的计时器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();   }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/569547.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号