我希望在达到某个值(即 100)时关闭 jFrame 窗口。框架只是一个进度条,显示以演示计算的距离。当百分比 = 100 时如何退出?我在下面有我的代码。
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.border.Border;
import java.util.Scanner;
public class PicCalc
{
public static void main(String args[])
{
Scanner keyboard = new Scanner(System.in);
long i = 0;
int x = 0;
int itinerations = 1;
int percent = 100/itinerations;
System.out.println("How many itinerations?");
itinerations = keyboard.nextInt();
for(i = 0; i <= itinerations; i++)
{
x = x + 5; //Will be replaced with more complicated stuff later
}
JFrame f = new JFrame("Loading...");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = f.getContentPane();
JProgressBar progressBar = new JProgressBar();
progressBar.setValue(percent);
progressBar.setStringPainted(true);
Border border = BorderFactory.createTitledBorder("Calcualating...");
progressBar.setBorder(border);
content.add(progressBar, BorderLayout.NORTH);
f.setSize(300, 100);
f.setVisible(true);
System.out.println(x);
}
}