简体   繁体   中英

Java Swing; Two classes, where to put if statements and new actionlisteners?

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

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

Whenever i add an if statement like;

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

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

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

  2. 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?

Code here

Thanks in advance

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(); }

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();
            }
        });
    }
}

Its scope resolution. Make the t public(not advisable) or make another timer in the second class which you will pass through the constructer of the second class when you invoke it. It will reference the original timer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM