有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Java应用程序中单击“否”按钮后,数组程序冻结(无响应)

好的,我正在构建一个循环,向学生展示一个循环是如何通过一个数组的,我添加了两个图像来帮助解释和代码,第一个是我点击go后得到的结果,然后它冻结。第二张图是我希望它在开始时输入值1,停止时输入值15,步骤中输入值3,然后单击Go按钮。然后点击清除按钮进行清除。我想他们可能有关联。有人看到问题了吗?提前谢谢

app after i click go (frozen)

the result id like

 import java.awt.*;
 import java.awt.event.*;
 import java.awt.Color;
 import javax.swing.JOptionPane;

 public class Checkerboard extends Frame implements ActionListener
 {
int[] blocksTextField = new int[15];

Panel blocksPanel = new Panel();
TextArea blocksDisplay[] = new TextArea[16];

TextField start = new TextField (3);
TextField stop = new TextField (3);
TextField step = new TextField (3);

//Colors
Color Red = new Color(255, 90, 90);
Color Green = new Color(140, 215, 40);
Color white = new Color(255,255,255);

//textField ints
int inputStart;
int inputStop;
int inputStep;

//Lables
Label custStartLabel = new Label ("Start : ");
Label custStopLabel = new Label ("Stop : ");
Label custStepLabel = new Label ("Step : ");

//Buttons
Button goButton = new Button("Go");
Button clearButton = new Button("Clear");

//panel for input textFields and lables
Panel textInputPanel = new Panel();

//Panel for buttons
Panel buttonPanel = new Panel();

public Checkerboard()
{//constructor method

    //set the 3 input textFields to 0
    inputStart = 0;
    inputStop = 0;
    inputStep = 0;

    //set Layouts for frame and three panels
    this.setLayout(new BorderLayout());
                       //grid layout   (row,col,horgap,vertgap)
    blocksPanel.setLayout(new GridLayout(4,4,10,10));

    textInputPanel.setLayout(new GridLayout(2,3,20,10));

    buttonPanel.setLayout(new FlowLayout());

    //setEditable()
    //setText()

    //add components to blocks panel
    for (int i = 0; i<16; i++)
    {
        blocksDisplay[i] = new TextArea(null,3,5,3);
        if(i<6)
            blocksDisplay[i].setText(" " +i);
        else
            blocksDisplay[i].setText(" " +i);
            blocksDisplay[i].setEditable(false);
        //  blocksDisplay[i].setBackground(Red);
            blocksPanel.add(blocksDisplay[i]);
    }//end for

    //add componets to panels
    //add text fields
    textInputPanel.add(start);
    textInputPanel.add(stop);
    textInputPanel.add(step);
    //add lables
    textInputPanel.add(custStartLabel);
    textInputPanel.add(custStopLabel);
    textInputPanel.add(custStepLabel);
    //add button to panel

    buttonPanel.add(goButton);
    buttonPanel.add(clearButton);

    //ADD ACTION LISTENRS TO BUTTONS (!IMPORTANT)
    goButton.addActionListener(this);
    clearButton.addActionListener(this);

     add(blocksPanel, BorderLayout.NORTH);
    add(textInputPanel, BorderLayout.CENTER);
    add(buttonPanel, BorderLayout.SOUTH);

    //overridding the windowcClosing() method will allow the user to clisk the Close button
            addWindowListener(
                new WindowAdapter()
                {
                    public void windowCloseing(WindowEvent e)
                    {
                        System.exit(0);
                    }
                }
    );

}//end of constructor method

public void actionPerformed(ActionEvent e)
{

    //if & else if to see what button clicked and pull user input
    if(e.getSource() == goButton) //if go clicked ...
        {
        System.out.println("go clicked");

    try{
            String inputStart = start.getText();
            int varStart = Integer.parseInt(inputStart);
            if (varStart<=0 || varStart>=15 )throw new NumberFormatException();

            System.out.println("start = " + varStart);
            //  roomDisplay[available].setBackground(lightRed);

            String inputStop = stop.getText();
            int varStop = Integer.parseInt(inputStop);
            if (varStop<=0 || varStart>=15 )throw new NumberFormatException();
            System.out.println("stop = " + varStop);

            String inputStep = step.getText();
            int varStep = Integer.parseInt(inputStep);
            if (varStep<=0 || varStep>=15 )throw new NumberFormatException();
            System.out.println("step = " + varStep);

                for (int i = varStart; i<varStop; varStep++)//ADD WHILE LOOP
                        {
                            blocksDisplay[i].setBackground(Red);
                            blocksDisplay[i].setText(" " +i);
                        }

        }

    catch (NumberFormatException ex)
        {
            JOptionPane.showMessageDialog(null, "You must enter a Start, Stop and Step value greater than 0 and less than 15",
            "Error",JOptionPane.ERROR_MESSAGE);
        }

        }
        else if(e.getSource() == clearButton ) //else if clear clicked ...
        {
        System.out.println("clear clicked");
        }

    //int available = room.bookRoom(smoking.getState());
    //if (available > 0)//Rooms is available

}//end action performed method

public static void main(String[]args)
{
    Checkerboard frame = new Checkerboard ();
        frame.setBounds(50, 100, 300, 410);//changed  size to make text feilds full charater size
        frame.setTitle("Checkerboarder Array");
        frame.setVisible(true);
}//end of main method


 }

共 (2) 个答案

  1. # 1 楼答案

    问题是你的循环:你的循环变量名是i,但是你改变了varStep变量而不是i,所以基本上循环变量永远不会改变,因此退出条件永远不会为真

    我相信你想用varStepi,所以把你的循环改成:

    for (int i = varStart; i<varStop; i += varStep)
        // stuff inside loop
    
  2. # 2 楼答案

    在for循环中使用varStep++。我想你是有意的。 应用程序会冻结,因为你永远不会增加i,从而导致无休止的循环