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

如何在Swing中创建延迟

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

如何在Swing中创建延迟

好吧,下面的代码显示了一个带有Jtextarea和JButton的Jframe。单击按钮后,计时器将事件重复发送(事件之间有第二个延迟)到与按钮相关的actionListener,后者在当前时间后追加一行。

import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Calendar;import javax.swing.JButton;import javax.swing.Jframe;import javax.swing.Jtextarea;import javax.swing.Timer;public class TimerTest extends Jframe implements ActionListener{    private static final long serialVersionUID = 7416567620110237028L;    Jtextarea area;    Timer timer;    int count; // Counts the number of sendings done by the timer    boolean running; // Indicates if the timer is started (true) or stopped (false)    public TimerTest() {        super("Test");        setBounds(30,30,500,500);        setDefaultCloseOperation(EXIT_ON_CLOSE);        setLayout(null);        area = new Jtextarea();        area.setBounds(0, 0, 500, 400);        add(area);        JButton button = new JButton("Click Me!");        button.addActionListener(this);        button.setBounds(200, 400, 100, 40);        add(button);        // Initialization of the timer. 1 second delay and this class as ActionListener        timer = new Timer(1000, this);        timer.setRepeats(true); // Send events until someone stops it        count = 0; // in the beginning, 0 events sended by timer        running = false;        System.out.println(timer.isRepeats());        setVisible(true); // Shows the frame    }    public void actionPerformed(ActionEvent e) {        if (! running) { timer.start(); running = true;        }        // Writing the current time and increasing the cont times        area.append(Calendar.getInstance().getTime().toString()+"n");        count++;        if (count == 10) { timer.stop(); count = 0; running = false;        }    }    public static void main(String[] args) {        // Executing the frame with its Timer        new TimerTest();    }}

好的,此代码是如何使用javax.swig.Timer对象的示例。关于问题的具体情况。停止计时器的if语句必须更改,并且显然必须更改actionPerformed的操作。以下片段是解决方案actionPerformed的框架:

public void actionPerformed(ActionEvent e) {    if (e.getComponent() == myDealerComponent()) {    // I do this if statement because the actionPerformed can treat more components        if (! running) { timer.start(); runnig = true;        }        // Hit a card if it must be hitted        switch (getJBTable(JB.total, JB.aces > 0)) {          case 0:   JB.hit();   break;          case 1:   break done;          case 2:   JB.hit();   JB.bet *= 2;   break done;        }        if (JB.total >= 21) { // In this case we don't need count the number of times, only check the JB.total 21 reached timer.stop() running = false;        }    }}

恕我直言,这可以解决问题,现在@ user920769必须考虑将actionListener和启动/停止条件放在哪里…

@kleopatra:感谢您向我展示这个计时器类的存在,对此我一无所知,这真是太神奇了,它可以将很多任务分配到一个swing应用程序中:)



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

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

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