微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

JFrame 突然吓坏了

如何解决JFrame 突然吓坏了

我正在制作一个小程序,其中有 3 个计时器,当您按下开始键时,它会显示一个进度条,直到时间结束,但 JFrame 在按下开始键后决定执行此操作:

enter image description here

这是我的代码

package com.company;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;




public class Main {
    static int getTime(JFrame frame){
        String ts = JOptionPane.showInputDialog(frame,"Set time: ","time",1);
        return Integer.parseInt(ts);

    }

    public static class globalVars{
        public static int time1 = 5;
        public static int time2 = 5;
        public static int time3 = 5;
    }
    static void startTimer(JFrame frame) {

        int time = globalVars.time1+globalVars.time2+globalVars.time3;
        int timeF = Math.toIntExact(System.currentTimeMillis() / 1000l);
        JProgressBar bar = new JProgressBar(0,time);
        frame.add(bar);
        frame.pack();

        int i=0;
        while (System.currentTimeMillis()!=timeF+time* 60L){
            bar.setValue((timeF+time*60-Math.toIntExact(System.currentTimeMillis() / 1000l))/60);
        }

    }


    public static void main(String[] args) {
        JFrame frame = new JFrame("Timer");
        frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());


        //-----------------------------------------------------------//
        Icon icon1 = new ImageIcon("placeholder.png");
        String button1Text = "Button1,";
        JButton button1 = new JButton(String.format("%s%dmin",button1Text,globalVars.time1));
        button1.setIcon(icon1);
        button1.addActionListener(new ActionListener() {
                                      public void actionPerformed(ActionEvent e) {
                                        globalVars.time1=getTime(frame);
                                          button1.setText(String.format("%s%dmin",globalVars.time1));
                                      }
                                  });

        frame.add(button1);
        //-----------------------------------------------------------//
        Icon icon2 = new ImageIcon("placeholder.png");
        String button2Text = "Button2,";
        JButton button2 = new JButton(String.format("%s%dmin",button2Text,globalVars.time2));
        button2.setIcon(icon2);
        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                globalVars.time2=getTime(frame);
                button2.setText(String.format("%s%dmin",globalVars.time2));
            }
        });

        frame.add(button2);
        //-----------------------------------------------------------//
        Icon icon3 = new ImageIcon("placeholder.png");
        String button3Text = "Button3,";
        JButton button3 = new JButton(String.format("%s%dmin",button3Text,globalVars.time3));
        button3.setIcon(icon3);
        button3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                globalVars.time3=getTime(frame);
                button3.setText(String.format("%s%dmin",globalVars.time3));
            }
        });

        frame.add(button3);
        //-----------------------------------------------------------//
        JButton buttonStart = new JButton("Start");
        buttonStart.addActionListener(new ActionListener() {
                                          public void actionPerformed(ActionEvent e) {
                                              buttonStart.removeActionListener(this);
                                             startTimer(frame);
                                          }
                                      });
        frame.add(buttonStart);
        //-----------------------------------------------------------//

        frame.pack();
        frame.setSize(400,200);
        frame.setVisible(true);


    }
}

我知道这是一种非常低效的做事方式,我会改进它,但现在我更担心黑屏。 Tnx!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。