Java Swing;两个类,将 if 语句和新的 actionlistener 放在哪里? [英] Java Swing; Two classes, where to put if statements and new actionlisteners?

查看:24
本文介绍了Java Swing;两个类,将 if 语句和新的 actionlistener 放在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是纯初学者问题.我正在修改代码,现在被困在以下问题上;

Pure beginner question here. I'm modifying a code and am now stuck on the following questions;

我的计时器在上层阶级中被调用.但是我的 int 计数在下面的类中被调用.

My timer is called in the upper class. But my int count is being called in the class below.

每当我添加一个 if 语句时,如;

Whenever i add an if statement like;

if (count == 2) { t.stop();}

我收到错误,因为 int 在下面的类中,而 t(计时器)在上面的类中.

i get errors because the int is in the class below and the t (timer) in the class above.

  1. 当涉及两个类时,如何添加 if 语句?

  1. How can i add an if statement when there are two classes involved?

如果我想为一个按钮添加一个新的动作监听器来停止计数,我应该把它放在上面还是下面的类中?

And if i want to add a new actionlistener for a button to stop the count, do i put this in the class above or under?

代码在这里

提前致谢

推荐答案

因为 ClockListener 是一个 嵌套类(下),封闭实例(上)可以访问侦听器的私有字段.如果您有对 ClockListener 实例的引用,

Because ClockListener is a nested class (lower), the enclosing instance (upper) can access the listener's private fields. If you have a reference to an instance of ClockListener,

ClockListener cl = new ClockListener();

你可以用它来初始化你的计时器

you can use it to initialize your timer

Timer t = new Timer(1000, cl);

并且您可以在测试中使用它:

and you can use it in your test:

if (cl.count == 2) { t.stop(); }

附录:作为参考,这里有一个使用 JToggleButton 来控制计时器的程序变体.正如之前所建议的那样,您使用了Calendar 来最小化Timer 漂移.和你一样,我放弃了在低分辨率应用程序中无关紧要的方法.

Addendum: For reference, here's a variation of your program that uses a JToggleButton to control the timer. As suggested earlier, you had used Calendar to minimize Timer drift. Like you, I abandoned the approach as irrelevant in a low-resolution application.

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.Timer;

/** @see https://stackoverflow.com/questions/5528939*/
class ClockExample extends JFrame {

    private static final int N = 60;
    private static final String stop = "Stop";
    private static final String start = "Start";
    private final ClockListener cl = new ClockListener();
    private final Timer t = new Timer(1000, cl);
    private final JTextField tf = new JTextField(3);

    public ClockExample() {
        t.setInitialDelay(0);

        JPanel panel = new JPanel();
        tf.setHorizontalAlignment(JTextField.RIGHT);
        tf.setEditable(false);
        panel.add(tf);
        final JToggleButton b = new JToggleButton(stop);
        b.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                if (b.isSelected()) {
                    t.stop();
                    b.setText(start);
                } else {
                    t.start();
                    b.setText(stop);
                }
            }
        });
        panel.add(b);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.add(panel);
        this.setTitle("Timer");
        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    public void start() {
        t.start();
    }

    private class ClockListener implements ActionListener {

        private int count;

        @Override
        public void actionPerformed(ActionEvent e) {
            count %= N;
            tf.setText(String.valueOf(count));
            count++;
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                ClockExample clock = new ClockExample();
                clock.start();
            }
        });
    }
}

这篇关于Java Swing;两个类,将 if 语句和新的 actionlistener 放在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆