首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无视觉显示

无视觉显示
EN

Stack Overflow用户
提问于 2013-12-09 18:48:18
回答 1查看 78关注 0票数 0

我正在为课堂开发一个java游戏,在那里我们实现了一个nim游戏。我以为我快完成了,但当我运行这个程序时,什么也没有出现。它只是说成功的建造和退出。我从来没看过比赛也没玩过。下面是我的应用程序代码。

代码语言:javascript
复制
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,但事实并非如此。任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-09 18:53:32

代码语言:javascript
复制
public void Nim(){

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

代码语言:javascript
复制
Nim NIM = new Nim();

但是默认的构造函数并没有达到预期的效果,所以您看不到任何东西。若要解决此问题,请通过删除void将上述方法定义更改为构造函数定义。

代码语言:javascript
复制
public Nim() {
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20478266

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档