没有视觉出现

时间:2013-12-09 18:48:19

标签: java jframe

我正在为我们实施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();
    }
}

知道为什么没有出现的东西?我想我忘记了setVisible,但事实并非如此。任何帮助将非常感激。

1 个答案:

答案 0 :(得分:2)

public void Nim(){

这不是构造函数,它是一个void方法声明,名称非常混乱。由于您根本没有声明构造函数,因此您有一个隐式默认构造函数,在此处调用:

Nim NIM = new Nim();

但是默认构造函数没有按照您的期望执行,因此您没有看到任何内容。要解决此问题,请通过删除void

将上述方法定义更改为构造函数定义
public Nim() {