运行时错误Java应用程序

时间:2014-05-12 11:01:21

标签: java

大家好,我建立了这个应用程序,向学生展示循环如何以图形方式工作,虽然工作不正确,但我稍后会在一个问题中解决这个问题。我不确定我对applet做了什么,但现在我得到了这个运行时错误。我该如何解决?谢谢你提前!

enter image description here

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();


 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]);
    }



    //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

}

1 个答案:

答案 0 :(得分:1)

制作构造函数public

public Checkerboard() {
   ...
}

出现错误是因为构造函数是包私有的,例如你不能在它的包之外实例化。