【问题标题】:Java timer issueJava计时器问题
【发布时间】:2011-11-27 17:19:15
【问题描述】:

我有一个模拟咖啡自动售货机的小程序。我正在尝试让文本准备好显示 5 秒。但这似乎不起作用。谁能告诉我我的计时器出了什么问题?

类 qn1 是 JApplet 的类。 将调用 init() 来实例化变量。

我将 actionlistener 添加到 2 个按钮,以便在我单击它们时它们会做出反应。

    import java.awt.*;
    import java.awt.event.*;

    import javax.swing.*;
    import javax.swing.event.*;

    public class qn1 extends JApplet implements ActionListener {

        private static int FULL = 4;
        private int coffees = FULL;
        private JPanel p;
        private JButton jbtw, jbtb;
        private Dispenser dis;
        private JLabel jlbl;
        private Timer timer;

        public void init() {
            setLayout(new BorderLayout(5, 5));
            setSize(400, 500);

            p = new JPanel(new GridLayout(1, 2));
            jbtw = new JButton("White");
            jbtb = new JButton("Black");
            jbtw.addActionListener(this);
            jbtb.addActionListener(this);
            p.add(jbtw);
            p.add(jbtb);

            add(p, BorderLayout.NORTH);

            dis = new Dispenser();
            add(dis, BorderLayout.CENTER);

            jlbl = new JLabel("Select", SwingConstants.CENTER);
            add(jlbl, BorderLayout.SOUTH);

            timer = new Timer(50, this);
        }

        public void actionPerformed(ActionEvent e) {
            if (e.getSource().equals(jbtb) | e.getSource().equals(jbtw)) {
                if (coffees - 1 >= 0) {
                    coffees--;
                    jlbl.setText("Ready " + coffees);               
                }

                timer.setInitialDelay(5000);
                timer.start();  

                if (coffees == 0) {
                    jlbl.setText("Empty");
                } else {
                    jlbl.setText("Select" + coffees);
                }
            }
        }
    }

    class Dispenser extends JPanel {
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawLine(0, 0, 25, 50);
            g.drawLine(25, 50, 55, 50);
            g.drawLine(55, 50, 80, 0);
        }
    }

【问题讨论】:

    标签: java swing timer actionlistener


    【解决方案1】:

    您的计时器被调用,但如果源不是您的按钮之一,则您不执行任何操作:

        if (e.getSource().equals(jbtb) | e.getSource().equals(jbtw)) {
    

    要回答你的 cmets,你可能想要这样的东西:

        public void actionPerformed(ActionEvent e) {
            if (e.getSource().equals(jbtb) | e.getSource().equals(jbtw)) {
                timer.setInitialDelay(5000);
                timer.start();  
    
                if (coffees == 0) {
                    jlbl.setText("Empty");
                } else {
                    jlbl.setText("Select " + coffees);
                }
            }
            else {
                if (coffees - 1 >= 0) {
                    coffees--;
                    jlbl.setText("Ready " + coffees);               
                }
            }
        }
    

    【讨论】:

    • @xinwei 在调试模式下单步执行会帮助你找到这个——你可能想习惯它(如果你不使用 Eclipse 或 Netbeans,开始吧!)
    • 我尝试删除 if (e.getSource().equals(jbtb) | e.getSource().equals(jbtw)) { 但它不起作用。我添加该语句是因为按钮和计时器都共享相同的源侦听器,所以我应该区分代码中的源。
    • 你应该。但是想想如果计时器调用了监听器,你想做什么?另外,请注意您在将标签中的文本设置为ready 之后立即设置它。
    • 我尝试在调试模式下,它确实进入了语句 jlbl.setText("Ready " + coffees);但它没有停留 5 秒
    • 我添加语句 if (e.getSource().equals(timer)) { jlbl.setText("Ready " + coffees); } 但它不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-25
    • 1970-01-01
    • 1970-01-01
    • 2012-05-04
    • 2021-11-10
    • 2016-11-13
    相关资源
    最近更新 更多