主题:求助
一个GUI的程序 为 OneRowNim游戏设计的 运行成功了 界面也出来了 但是当输入数据 点击按键时却没反应 就是人机不能交互。高手看看,是怎么搞的,是不是GUI中最后一个方法没运行呀。可能比较长,不先谢谢各位前辈了!!!这个游戏过程就是用户和电脑轮流拿小棒,每次只能拿1,2或3个 ,拿走最后一根的为输。再次谢谢了!!!
[b]//OneRowNim类,就是这个主类,游戏[/b]
import java.util.Scanner;
public class OneRowNim
{ private int player=1;
private int nSticks=21;
public OneRowNim(int sticks)
{ nSticks=sticks;
}
public OneRowNim(int sticks,int starter)
{ nSticks=sticks;
player=starter;
}
public boolean takeSticks(int num)
{ if(num<1 || num>3) return false;
else
{ nSticks=nSticks-num;
player=3-player;
return true;
}
}
public int getSticks()
{ return nSticks;
}
public int getPlayer()
{ return player;
}
public boolean gameOver()
{ return(nSticks<1);
}
public int getWinner()
{ if(nSticks<1) return getPlayer();
else return 0;
}
public void report()
{ System.out.println("sticks is left: "+ getSticks());
System.out.println("next turn by player: "+ getPlayer());
}
}
[b]//GUI类,为游戏设计的GUI界面[/b]
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class OneRowNimGUI extends JFrame implements ActionListener
{ private JTextArea display;
private JTextField inField;
private JButton goButton;
private OneRowNim game;
public OneRowNimGUI(String title)
{ game=new OneRowNim(21);
buildGUI();
setTitle(title);
pack();
setVisible(true);
}
private void buildGUI()
{ Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
display= new JTextArea(20,30);
display.setText("let's play Take Away. There are "+game.getSticks()+" sticks.\n"+
"Pick up 1,2 or 3 at a time.\n"+"You go first.\n");
inField=new JTextField(10);
goButton=new JButton("Take Sticks");
JPanel inputPanel=new JPanel();
inputPanel.add(new JLabel("How many sticks do you take: "));
inputPanel.add(inField);
inputPanel.add(goButton);
contentPane.add("Center", display);
contentPane.add("South",inputPanel);
}
private void userMove()
{ int userTakes=Integer.parseInt(inField.getText());
if (game.takeSticks(userTakes))
display.append("You take " + userTakes + ".\n");
else display.append("You can't take " + userTakes + " . Try again\n" );
}
private void computerMove()
{ if (game.gameOver()) return;
if (game.getPlayer()==2)
game.takeSticks(1);
display.append("I take one stick.");
}
private void endGame()
{ goButton.setEnabled(false);
inField.setEnabled(false);
if(game.getWinner()==1)
display.append("Game over.You win.Nice game.\n");
else display.append("Game over.I win.Nice game.\n");
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==goButton)
{ userMove();
computerMove();
int sticksLeft = game.getSticks();
display.append("There are " + sticksLeft + "sticks left.\n");
if(game.gameOver()) endGame();
}
}
}
[b]//测试类[/b]
public class OneRowNimGUITest
{ public static void main(String args[])
{ OneRowNimGUI myGui=new OneRowNimGUI("myGUI");
}
}
[b]//OneRowNim类,就是这个主类,游戏[/b]
import java.util.Scanner;
public class OneRowNim
{ private int player=1;
private int nSticks=21;
public OneRowNim(int sticks)
{ nSticks=sticks;
}
public OneRowNim(int sticks,int starter)
{ nSticks=sticks;
player=starter;
}
public boolean takeSticks(int num)
{ if(num<1 || num>3) return false;
else
{ nSticks=nSticks-num;
player=3-player;
return true;
}
}
public int getSticks()
{ return nSticks;
}
public int getPlayer()
{ return player;
}
public boolean gameOver()
{ return(nSticks<1);
}
public int getWinner()
{ if(nSticks<1) return getPlayer();
else return 0;
}
public void report()
{ System.out.println("sticks is left: "+ getSticks());
System.out.println("next turn by player: "+ getPlayer());
}
}
[b]//GUI类,为游戏设计的GUI界面[/b]
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class OneRowNimGUI extends JFrame implements ActionListener
{ private JTextArea display;
private JTextField inField;
private JButton goButton;
private OneRowNim game;
public OneRowNimGUI(String title)
{ game=new OneRowNim(21);
buildGUI();
setTitle(title);
pack();
setVisible(true);
}
private void buildGUI()
{ Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
display= new JTextArea(20,30);
display.setText("let's play Take Away. There are "+game.getSticks()+" sticks.\n"+
"Pick up 1,2 or 3 at a time.\n"+"You go first.\n");
inField=new JTextField(10);
goButton=new JButton("Take Sticks");
JPanel inputPanel=new JPanel();
inputPanel.add(new JLabel("How many sticks do you take: "));
inputPanel.add(inField);
inputPanel.add(goButton);
contentPane.add("Center", display);
contentPane.add("South",inputPanel);
}
private void userMove()
{ int userTakes=Integer.parseInt(inField.getText());
if (game.takeSticks(userTakes))
display.append("You take " + userTakes + ".\n");
else display.append("You can't take " + userTakes + " . Try again\n" );
}
private void computerMove()
{ if (game.gameOver()) return;
if (game.getPlayer()==2)
game.takeSticks(1);
display.append("I take one stick.");
}
private void endGame()
{ goButton.setEnabled(false);
inField.setEnabled(false);
if(game.getWinner()==1)
display.append("Game over.You win.Nice game.\n");
else display.append("Game over.I win.Nice game.\n");
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==goButton)
{ userMove();
computerMove();
int sticksLeft = game.getSticks();
display.append("There are " + sticksLeft + "sticks left.\n");
if(game.gameOver()) endGame();
}
}
}
[b]//测试类[/b]
public class OneRowNimGUITest
{ public static void main(String args[])
{ OneRowNimGUI myGui=new OneRowNimGUI("myGUI");
}
}