按钮单击没有Java应用程序后,程序冻结(无响应)

时间:2014-05-12 12:19:09

标签: java arrays for-loop

好的,所以我正在建立向学生展示循环如何通过数组,我添加了2个图像来帮助解释和代码,第一个是我点击之后得到的结果然后冻结。第二个图像是我在开始时输入值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


 }

3 个答案:

答案 0 :(得分:2)

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

我相信您希望ivarStep同步,因此请将您的循环更改为:

for (int i = varStart; i<varStop; i += varStep)
    // stuff inside loop

答案 1 :(得分:1)

看看这个循环。

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

它在i >= varStop时结束,但ivarStop都不会因其执行而发生变化,因此它永远不会停止。您只需递增varStep

我认为您希望在每次迭代时将i增加varStep,即i += varStep

答案 2 :(得分:1)

您在for循环中使用varStep ++。我想你打算做i + varStep。 应用程序冻结,因为你永远不会增加i,导致无限循环。