Java JFrame突然发疯了
我正在制作一个小程序,其中有3个计时器,当你按下开始按钮时,它会给出一个进度条,直到时间结束,但JFrame在我按下开始按钮后决定这样做: 这是我的密码:Java JFrame突然发疯了,java,swing,jframe,Java,Swing,Jframe,我正在制作一个小程序,其中有3个计时器,当你按下开始按钮时,它会给出一个进度条,直到时间结束,但JFrame在我按下开始按钮后决定这样做: 这是我的密码: package com.company; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Main { static
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", button1Text, 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", button2Text, 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", button3Text, 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 在开始测试之前,不要编写整个应用程序。1) 仅使用按钮显示框架。2) 然后单击其中一个按钮查看发生了什么。3) 当你看到黑屏的时候,你知道你刚才添加了什么代码,所以你知道要修复什么。我猜你的问题是while循环。阅读上的Swing教程,以获得工作示例和更好的结构化代码。您不应该使用静态方法。您可以从
ActionListener
调用startTimer()
方法。这意味着startTimer()