是否可以通过单击其他课程中的按钮来启动计时器?

时间:2019-04-08 12:00:31

标签: java timer

我正在尝试制作一个程序,该程序将在一个JFrame中显示一个倒数计时器,同时在另一个JFrame中镜像一个倒数计时器。很简单,我的主JFrame将具有启动,停止和重置按钮。而第二只将具有倒数计时器。有没有一种方法可以使用主JFrame(按钮)启动和停止计时器,从而转换为在第二个JFrame中启动和停止计时器?

我到处都看过,但似乎找不到解决方法?!

这是我的主班的代码:

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import javax.swing.JLabel;
import java.awt.Font;

import javax.swing.SwingConstants;
import javax.swing.Timer;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Main extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private JPanel contentPane;

    /**
     * Launch the application.
     *
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Main frame = new Main();
                    frame.setLocationRelativeTo(null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }

    /**
     * Create the frame.
     * @return 
     */

    public static int time = 30;

    public Main() {
        setResizable(false);
        setVisible(true);

        Second second = new Second();
        second.setVisible(true);
        second.setLocationRelativeTo(null);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 1288, 720);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel Seconds = new JLabel("30");
        Seconds.setHorizontalAlignment(SwingConstants.CENTER);
        Seconds.setFont(new Font("Arial", Font.BOLD, 199));
        Seconds.setBounds(445, 96, 343, 202);
        contentPane.add(Seconds);

        Timer tm = new Timer(1000, new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                Seconds.setText(Integer.toString(time));
                time--;
                if (time < 0) {
                    Seconds.setText("0");
                }
            }
        });

        JButton Start = new JButton("Start");
        Start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                tm.setInitialDelay(0);
                tm.start();
            }
        });
        Start.setFont(new Font("Arial", Font.PLAIN, 20));
        Start.setBounds(445, 309, 140, 50);
        contentPane.add(Start);

        JButton Stop = new JButton("Stop");
        Stop.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                tm.stop();
            }
        });
        Stop.setFont(new Font("Arial", Font.PLAIN, 20));
        Stop.setBounds(648, 309, 140, 50);
        contentPane.add(Stop);

        JButton Restart = new JButton("Reset");
        Restart.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                time = 30;
                Seconds.setText("30");
                tm.stop();
            }
        });
        Restart.setFont(new Font("Arial", Font.PLAIN, 20));
        Restart.setBounds(548, 370, 140, 50);
        contentPane.add(Restart);

    }
}

这是我的第二堂课的代码:

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import javax.swing.JLabel;
import java.awt.Font;

import javax.swing.SwingConstants;
import javax.swing.Timer;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Second extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public JPanel contentPane;

    /**
     * Launch the application.
     *
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Second frame = new Second();
                    frame.setLocationRelativeTo(null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    int time = 30;

    /**
     * Create the frame.
     */

    public Second() {
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 1288, 720);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);

        JLabel Seconds = new JLabel(Integer.toString(Main.time));
        Seconds.setBounds(445, 96, 343, 202);
        Seconds.setHorizontalAlignment(SwingConstants.CENTER);
        Seconds.setFont(new Font("Arial", Font.BOLD, 199));
        contentPane.setLayout(null);
        contentPane.add(Seconds);

        Timer tm = new Timer(1000, new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                Seconds.setText(Integer.toString(time));
                time--;
                if (time < 0) {
                    Seconds.setText("0");
                }
            }
        });

    }   
}

同样,我要寻找的是在Main类中按下开始按钮时,它也在Second类中启动计时器。停止和重置按钮分别相同。

0 个答案:

没有答案