2014-05-12 234 views
-1

嗨,大家好我建立了這個應用程序,向學生展示一個循環如何以圖形方式工作,雖然工作正常,但我稍後會在一個問題中解決這個問題。我不知道我對小程序做了什麼,但現在我得到這個運行時錯誤。我該如何解決?先謝謝了!運行時錯誤Java應用程序

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 

}

+0

讀取異常消息:'Checkerboard.class不公開或者沒有公共構造函數。' –

回答

1

使構造public

public Checkerboard() { 
    ... 
} 

出現此錯誤是因爲構造函數是包私有的,例如,你不能在它的包之外實例化這個類。

+0

非常感謝,那個排序異常。現在我得到java.lang.ClassCastException:Checkerboard不能轉換爲java.applet.Applet。你也許知道原因? – Reign

+0

呃...是的。你的類必須擴展'Applet'類才能被'appletviewer'工具執行。 –

+0

認爲它不適合,而是將它作爲擴展框架的應用程序使用。謝謝,我明白了! – Reign