对这个Java循环如此困惑的输入
多年来,我一直在尝试让这个程序工作,每次我尝试添加一个循环,增加进度计数器并刷新主框架,它要么不会显示,要么保持在0,我输入的每个输入都不会影响程序
public class Progress2
{
private JPanel mainPanel = new JPanel();
int progress = 0;
int prog = 100;
int count;
Scanner input = new Scanner(System.in);
String command = "";
public Progress2()
{
JPanel labelPanel = new JPanel(new GridLayout(0, 11)); // holds the JLabels in a grid
Label label1 = new Label("0%");
Label label2 = new Label("10%");
Label label3 = new Label("20%");
Label label4 = new Label("30%");
Label label5 = new Label("40%");
Label label6 = new Label("50%");
Label label7 = new Label("60%");
Label label8 = new Label("70%");
Label label9 = new Label("80%");
Label label10 = new Label("90%");
Label label11 = new Label("100%");
Label label12 = new Label(prog +"%");
labelPanel.add(label1);
labelPanel.add(label2);
labelPanel.add(label3);
labelPanel.add(label4);
labelPanel.add(label5);
labelPanel.add(label6);
labelPanel.add(label7);
labelPanel.add(label8);
labelPanel.add(label9);
labelPanel.add(label10);
labelPanel.add(label11);
labelPanel.add(label12);
label1.setForeground(Color.green);
label2.setForeground(Color.red);
label3.setForeground(Color.red);
label4.setForeground(Color.red);
label5.setForeground(Color.red);
label6.setForeground(Color.red);
label7.setForeground(Color.red);
label8.setForeground(Color.red);
label9.setForeground(Color.red);
label10.setForeground(Color.red);
label11.setForeground(Color.red);
mainPanel.add(labelPanel, BorderLayout.LINE_START);
if(progress>=1){
label2.setForeground(Color.green);
}
if(progress>=2){
label3.setForeground(Color.green);
prog=-10;
}
if(progress>=3){
label4.setForeground(Color.green);
prog=-10;
}
if(progress>=4){
label5.setForeground(Color.green);
prog=-10;
}
if(progress>=5){
label6.setForeground(Color.green);
prog=-10;
}
if(progress>=6){
label7.setForeground(Color.green);
prog=-10;
}
if(progress>=7){
label8.setForeground(Color.green);
prog=-10;
}
if(progress>=8){
label9.setForeground(Color.green);
prog=-10;
}
if(progress>=9){
label10.setForeground(Color.green);
prog=-10;
}
if(progress>=10){
label10.setForeground(Color.green);
prog=-10;
}
if(progress>=11){
label11.setForeground(Color.green);
prog=-10;
}
while(true){
for(count =0; count<11; count++){
Scanner scanner = new Scanner(System.in);
System.out.println("Do you want to increment?");
String scan = scanner.nextLine();
progress++;
}
}
}
public JComponent getComponent()
{
return mainPanel;
}
private static void createAndShowUI()
{
JFrame frame = new JFrame("Progress");
frame.getContentPane().add(new Progress2().getComponent());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args)
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
# 1 楼答案
遵循以下流程:
Progress2()
类初始化constructor
中的面板和标签以及附加组件。直到mainPanel.add(labelPanel, BorderLayout.LINE_START);
创建函数
updateUI()
函数void updateUI() { \获取面板 \从面板获取标签 \`正如上面blahfunk提到的那样,该函数中的for`循环。 }此处的学习点:如何从该面板获取面板和标签。为了方便起见,请检查是否可以将面板和标签声明为
Progress2()
类的类属性,然后可以定义类方法以返回这些值(getter)main()
函数中的createAndShowUI();
之后调用updateUI()
函数李>为什么GUI在我按enter键11次后才出现强>
这就是调用更新代码的地方
frame.getContentPane().add(new Progress2().getComponent());
在new Progress2()
内,它是类Progress2()
的构造函数frame.setVisible(true);
负责显示在上述语句之后调用的UI要查看正在运行的代码,请按照上面的工作流程查看应该执行的代码
# 2 楼答案
我相信您的印象是,每次更新进度变量时,所有这些if语句都将被测试,GUI也将相应地更新。事实并非如此。按照如下方式重新编写循环,您将看到预期结果: