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

摇摆计时器无法正常工作

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

摇摆计时器无法正常工作

使用Swing计时器将动作暂停xxx秒的示例:

import java.awt.*;import java.awt.event.*;import javax.swing.*;@SuppressWarnings("serial")public class QuickTimerEg extends JPanel {   private static final int TIMER_DELAY = 2000;   private boolean buttonsWorking = true;   private JButton btn1 = null;   private JButton btn2 = null;   private Timer swingTimer;   public QuickTimerEg() {      ActionListener btnListener = new ActionListener() {         public void actionPerformed(ActionEvent e) { btnActionPerformed(e);         }      };      setLayout(new GridLayout(4, 4));      for (int i = 0; i < 4; i++) {         for (int j = 0; j < 4; j++) { JButton button = new JButton("   "); button.addActionListener(btnListener); add(button);         }      }   }   private void btnActionPerformed(ActionEvent e) {      if (!buttonsWorking) {         return;      }      JButton button = (JButton)e.getSource();      button.setBackground(Color.blue);      button.setEnabled(false);      if (btn1 == null) {         btn1 = button;      } else {         buttonsWorking = false;         btn2 = button;         swingTimer = new Timer(TIMER_DELAY, new ActionListener() { public void actionPerformed(ActionEvent e) {    btn1.setBackground(null);    btn2.setBackground(null);    btn1.setEnabled(true);    btn2.setEnabled(true);    btn1 = null;    btn2 = null;    buttonsWorking = true;     }         });         swingTimer.setRepeats(false);         swingTimer.start();      }   }   private static void createAndShowGui() {      Jframe frame = new Jframe("QuickTimerEg");      frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);      frame.getContentPane().add(new QuickTimerEg());      frame.pack();      frame.setLocationRelativeTo(null);      frame.setVisible(true);   }   public static void main(String[] args) {      SwingUtilities.invokeLater(new Runnable() {         public void run() { createAndShowGui();         }      });   }}


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

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

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