Java Label Timer and Saving

前端 未结 2 838
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-07 06:20

Let me explain what I am trying to do.

I have two classes extending JFrame, the StartJFrame and TestingJFrame. In the main met

相关标签:
2条回答
  • 2020-12-07 06:50

    The first part (show the count down and stopping the timer) is relatively easy...

    public class TimerTest01 {
    
        public static void main(String[] args) {
            new TimerTest01();
        }
    
        public TimerTest01() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (Exception ex) {
                    }
    
                    JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
    
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            private JLabel label;
            private Timer timer;
            private long startTime;
    
            public TestPane() {
                setLayout(new GridLayout(0, 1));
                label = new JLabel("...");
                label.setHorizontalAlignment(JLabel.CENTER);
                final JButton btn = new JButton("Start");
                btn.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if (timer.isRunning()) {
                            timer.stop();
                            btn.setText("Start");
                        } else {
                            startTime = System.currentTimeMillis();
                            timer.restart();
                            btn.setText("Stop");
                        }
                        repaint();
                    }
                });
                add(label);
                add(btn);
    
                timer = new Timer(250, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        long endTime = (startTime + 45000);
                        long timeLeft = endTime - System.currentTimeMillis();
                        if (timeLeft < 0) {
                            timer.stop();
                            label.setText("Expired");
                            btn.setText("Start");
                        } else {
                            label.setText(Long.toString(timeLeft / 1000));
                        }
                        repaint();
                    }
                });
            }
        }
    }
    

    Take a look at Swing Timer for more info

    The second part of you question is to vague to reliably provide you with an answer. Where is the data coming from? How is collected??

    0 讨论(0)
  • 2020-12-07 07:08

    You can use javax.swing.Timer to setup your timer. You can have a look at the official tutorial too.

    0 讨论(0)
提交回复
热议问题