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

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

问题描述

纯粹的初学者问题。
我正在修改代码,现在我仍然坚持以下问题;

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?

如果我想为一个新的actionlistener添加一个按钮停止计数,
我把它放在上面或下面的课程中?

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 来控制计时器。正如之前的建议,您使用日历来最小化计时器漂移。和你一样,我在低分辨率的应用程序中放弃了这种方法。

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语句和新的actionlisteners?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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