2013-12-09 3 views
0

나는 우리가 nim의 게임을 구현하는 클래스 용 자바 게임을하고있다. 내가 거의 다가 가고 있다고 생각했지만 프로그램을 실행했을 때 아무 것도 나타나지 않습니다. 성공과 종료 만 말합니다. 나는 게임을 보거나 플레이하지 못한다. 아래는 응용 프로그램에 대한 내 코드입니다.시각적으로 나타나지 않는다

package Nim; 

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

public class Nim extends JFrame implements ActionListener { 

    // Number of stickSpace that is needed for the sticks; 
    int rowspace = 3; 

    // Buttons for the human to click to take turns playing witht the computer. 
    private JButton humanMove, 
        computerMove; 

    // whatRow = what row the user would like to remove from 
    // howMany = how many sticks to take from each row 
    private JComboBox whatRow, 
         howMany; 

    // Creates an array of JTextFields with the rowspace as a size. 
    private JTextField[] stickSpace = new JTextField[rowspace]; 

    // Creates the game; 
    private Nimgame nimgame; 

    public void Nim(){ 

     //As requireed, we use a loop to fill the array. 
     for(int i=0; i<rowspace ;i++){ 
      stickSpace[i] = new JTextField(5); 
     } 


     // Creates pulldown menus for the user to select the whatRow s/he 
     // wants to choose from, and another for howMany s/he wants to take. 
     whatRow = new JComboBox(); 
     howMany = new JComboBox(); 

     // Add the options to the pulldown menus so the player can choose. 
     // 0-2 because the array index starts at 0. 1-3 for the amount of sticks. 

     for(int p=0; p<=3; p++){ 
      if(p<3){ 
       whatRow.addItem(p); 
      } 
      if(p>0){ 
       howMany.addItem(p); 
      } 
     } 

     // Adds the text "Human Turn" and "CPU Turn" to the buttons used for turns. 
     humanMove = new JButton("Human Turn"); 
     computerMove = new JButton("CPU Turn"); 

     // Adds a listener to the buttons to signal the game its time to act. 
     humanMove.addActionListener(this); 
     computerMove.addActionListener(this); 

     // Creates a gridlayout (3,2) with 3 rows and two columns. 
     JPanel gridpanel = new JPanel(new GridLayout(3,2)); 

     // Labels the rows so the player knows it starts at row Zero - Three. 
     gridpanel.add(new JLabel("Row Zero", JLabel.LEFT)); 
     gridpanel.add(stickSpace[0]); 
     gridpanel.add(new JLabel("Row One", JLabel.LEFT));   
     gridpanel.add(stickSpace[1]); 
     gridpanel.add(new JLabel("Row Two", JLabel.LEFT));   
     gridpanel.add(stickSpace[2]); 

     // Creates another gridlayout this time with 4 rows and 2 columns. 
     // This sill be used to add the comboboxes, labels, and buttons. 
     JPanel mainPanel = new JPanel(new GridLayout(4,2)); 
     mainPanel.add(new JLabel("Remove from Row:", JLabel.RIGHT)); 
     mainPanel.add(whatRow); 
     mainPanel.add(new JLabel("# To Remove:", JLabel.RIGHT)); 
     mainPanel.add(howMany); 
     mainPanel.add(humanMove); 
     mainPanel.add(computerMove); 

     // This adds the gridpanel and the main panel to the visual 
     // application, but they are still not visiable at this moment. 
     getContentPane().add(gridpanel, BorderLayout.NORTH); 
     getContentPane().add(mainPanel, BorderLayout.SOUTH); 

     // This sections sets the title, size, position on screen, sets it visiable, 
     // sets the background color, and makes it exit on close of the visual application. 
     setTitle("The Game of Nim"); 
     setSize(300, 250); 
     setBackground(Color.yellow); 
     setVisible(true); 

     // Creates the actual game to play. 
     nimgame = new Nimgame(); 

     // Prints out the sticks in each row. 
     for(int p=0; p<3; p++){ 
      stickSpace[p].setText(nimgame.addSticks(p)); 
     } 
    } 

    // Method to handle when an action lister find an action event 
    public void actionPerformed(ActionEvent e){ 

     // Checks if the humanMove button has been pressed 
     if(e.getSource() == humanMove){ 
      int foo = whatRow.getSelectedIndex(); 
      int bar = howMany.getSelectedIndex()+1; 
      nimgame.stickRemove(foo, bar); 

      for(int p=0; p<3; p++){ 
       stickSpace[p].setText(nimgame.addSticks(p)); 
      }    
     } 

     // Checks if the computerMove button has been pressed. 
     if(e.getSource() == computerMove){ 
      nimgame.computerRandom(); 
      for(int p=0; p<3; p++){ 
       stickSpace[p].setText(nimgame.addSticks(p)); 
      }    
     } 

     // Checks to see if the game is over or not. 
     if(nimgame.isDone()){ 
      JOptionPane.showMessageDialog(null, "Game Over Player Player" + nimgame.whoWon()); 
     } 
    } 

    public static void main(String[] args) { 

     Nim NIM = new Nim(); 
    } 
} 

왜 아무 것도 나타나지 않을 것이라고 생각하세요? 나는 진실을 잊어 버리는 것을 생각했다. 그러나 그것은 사실이 아니었다. 어떤 도움이라도 대단히 감사 할 것입니다.

답변

2
public void Nim(){ 

이, 그것은 굉장히 혼란스러운 이름을 가진 void 메소드 선언 생성자입니다하지 않습니다. 당신은 아무것도 표시되지 않도록,

Nim NIM = new Nim(); 

그러나 당신이 기대하는 것을하지 않는 기본 생성자 : 당신은 모든 생성자를 선언하지 않는 한, 당신은 암시 적 기본 여기라고 생성자가 . 이 문제를 해결하려면, void을 제거하여 생성자 정의에 상기 방법 정의를 변경 :

public Nim() { 
+1

내가 시간 이상이 응시하고있다 ... 대단히 kviiri 감사하고 나를 미치게되었다! 때때로 당신은 여분의 한 쌍의 눈이 필요하다고 생각합니다! 이렇게하면 문제가 해결되며 가능한 경우 답변으로 표시됩니다. 다시 한번 감사 드리며 좋은 하루 되세요! – NerdsRaisedHand

+0

@NerdsRaisedHand, 매우 일반적입니다. 기꺼이 도와 줬다! – kviiri

관련 문제