n子琪问题,按钮监听,输入N时有点问题

时间:2022-12-04 22:38:46
n子琪问题,按钮监听,输入N时有点问题
程序要弄一个n子琪。
现在程序编译没问题,也能运行。但在文本区输入整数n,按按钮“开始”时,面板p不能分配格子。报了一堆错误
下面的方法isFull(),iswon(),class cell没问题,主要是帮忙看看public static void main(String[] args)
TicTacToe()
 
  public void actionPerformed(ActionEvent e)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at TicTacToe.actionPerformed(TicTacToe.java:53)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)



// TicTacToe.java: Play the TicTacToe game
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;

public class TicTacToe extends JApplet implements ActionListener
{
private char whoseTurn = 'X';
private int N;

  // Create and initialize cells
  private Cell[][] cell =  new Cell[9][9];
//本来是用N定义,但N还没输入,就用9代替,也就是这个只能是9子琪以内的棋了

  // Create and initialize a status label
  private JLabel jlblStatus ;
  private JPanel p;
    private JPanel p1;
  private JLabel lbl;
  private JTextField text;
  private JButton butn;
TicTacToe()
{
text=new JTextField(10);
butn=new JButton("开始");
lbl=new JLabel("请输入棋盘行数");
jlblStatus = new JLabel("");
JPanel p = new JPanel(); 
        JPanel p1 = new JPanel();
        
         
         text.addActionListener(this);
         butn.addActionListener(this);
         
     p1.setLayout(new FlowLayout());
      p1.add(lbl);
      p1.add(text);
      p1.add(butn);
  
    this.getContentPane().add(p, BorderLayout.CENTER);
    this.getContentPane().add(p1, BorderLayout.NORTH);
    this.getContentPane().add(jlblStatus, BorderLayout.SOUTH);
}
  
  public void actionPerformed(ActionEvent e)
  {
   if(e.getSource()==butn)
   N=Integer.parseInt(text.getText());    
         p.setLayout(new GridLayout(N,N));
          for (int i=0; i<N; i++)
           for (int j=0; j<N; j++)
           p.add(cell[i][j] = new Cell());
  }
  

  // This main method enables the applet to run as an application
  public static void main(String[] args)
  {
    // Create a frame
    JFrame frame = new JFrame("Tic Tac Toe");
    frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);

    // Create an instance of the applet
    TicTacToe applet = new TicTacToe();

    frame.getContentPane().add(applet, BorderLayout.CENTER);

    // Invoke start()
    applet.start();

    // Display the frame
    frame.setSize(300, 300);
    frame.setVisible(true);
  }

  // Determine if the cells are all occupied
  public boolean isFull()
  {
    for (int i=0; i<3; i++)
      for (int j=0; j<3; j++)
        if (cell[i][j].getToken() == ' ')
          return false;

    return true;
  }

  // Determine if the player with the specified token wins
  public boolean isWon(char token)
  {
    for (int i=0; i<3; i++)
      if ((cell[i][0].getToken() == token)
          && (cell[i][1].getToken() == token)
          && (cell[i][2].getToken() == token))
      {
        return true;
      }

    for (int j=0; j<3; j++)
      if ((cell[0][j].getToken() ==  token)
          && (cell[1][j].getToken() == token)
          && (cell[2][j].getToken() == token))
      {
        return true;
      }

    if ((cell[0][0].getToken() == token)
        && (cell[1][1].getToken() == token)
        && (cell[2][2].getToken() == token))
    {
      return true;
    }

    if ((cell[0][2].getToken() == token)
        && (cell[1][1].getToken() == token)
        && (cell[2][0].getToken() == token))
    {
      return true;
    }

    return false;
  }

  // An inner class for a cell
  public class Cell extends JPanel implements MouseListener
  {
    // Token used for this cell
    private char token = ' ';

    public Cell()
    {
      setBorder(new LineBorder(Color.black, 1)); // Set cell's border
      addMouseListener(this);  // Register listener
    }

    // The getter method for token
    public char getToken()
    {
      return token;
    }

    // The setter method for token
    public void setToken(char c)
    {
      token = c;
      repaint();
    }

    // Paint the cell
    public void paintComponent(Graphics g)
    {
      super.paintComponent(g);

      if (token == 'X')
      {
        g.drawLine(10, 10, getSize().width-10, getSize().height-10);
        g.drawLine(getSize().width-10, 10, 10, getSize().height-10);
      }
      else if (token == 'O')
      {
        g.drawOval(10, 10, getSize().width-20, getSize().height-20);
      }
    }

    // Handle mouse click on a cell
    public void mouseClicked(MouseEvent e)
    {
      if (token == ' ') // If cell is not occupied
      {
        if (whoseTurn == 'X')  // If it is the X player's turn
        {
          setToken('X');  // Set token in the cell
          whoseTurn = 'O';  // Change the turn
          jlblStatus.setText("O's turn");  // Display status
          if (isWon('X'))
            jlblStatus.setText("X won! The game is over");
          else if (isFull())
            jlblStatus.setText("Draw! The game is over");
        }
        else if (whoseTurn == 'O') // If it is the O player's turn
        {
          setToken('O'); // Set token in the cell
          whoseTurn = 'X';  // Change the turn
          jlblStatus.setText("X's turn"); // Display status
          if (isWon('O'))
            jlblStatus.setText("O won! The game is over");
          else if (isFull())
            jlblStatus.setText("Draw! The game is over");
        }
      }
    }

  public void mousePressed(MouseEvent e)
    {
      // TODO: implement this java.awt.event.MouseListener method;
    }

    public void mouseReleased(MouseEvent e)
    {
      // TODO: implement this java.awt.event.MouseListener method;
    }

    public void mouseEntered(MouseEvent e)
    {
      // TODO: implement this java.awt.event.MouseListener method;
    }

    public void mouseExited(MouseEvent e)
    {
      // TODO: implement this java.awt.event.MouseListener method;
    }
  }
}  


19 个解决方案

#1


大家帮忙看一下啊,哪里出问题了

#2


构造函数里
JPanel p = new JPanel(); 
JPanel p1 = new JPanel();
p ,p1是局部变量,全局变量p没有赋值,所以出现空指针

#3


学习!up!

#4


这个要怎么改啊

#5


构造方法里
p = new JPanel();
p1 = new JPanel();

#6


引用 2 楼 swandragon 的回复:
构造函数里 
JPanel p = new JPanel(); 
JPanel p1 = new JPanel(); 
p ,p1是局部变量,全局变量p没有赋值,所以出现空指针 

正解

#7


改了,没有异常了,可我输入数值,按开始。他没有反应啊

#8


二楼说的对。

还有就是你的 public boolean isWon(char token)这个方法算法是不是不太对啊?
我觉得应该定义为下面这个
public boolean isWon(char token,int row,int column),然后再
判断在row行,column-2~column+2之间有没有棋子连成三个
判断在columng列,row-2~row+2之间有没有棋子连成三个,
判断左上到右下,(row-2,column-2)到(row+2,column+2)之间没有棋子连成三个
判断右上到左下,(row-2,column+2)到(row+2,column-2)之间有没有棋子连三个的。

#9



这个算法我是这样写的,开始时因为N输入不了,算法里有N,所以暂时用三子棋的算法代替了

public boolean isWon(char token)
  {
    for (int i=0; i<N; i++)//横向三个棋子
     for(int j=0;j<N-2;j++)
      if ((cell[i][j].getToken() == token)&& (cell[i][j+1].getToken() == token)&& (cell[i][j+2].getToken() == token))
      {
        return true;
      }

    for (int j=0; j<N-2; j++)//纵向三个棋子
     for(int i=0;i<N;i++)
      if ((cell[i][j].getToken() ==  token)
          && (cell[i+1][j].getToken() == token)
          && (cell[i+2][j].getToken() == token))
      {
        return true;
      }
      for(int i=0;i<N-2;i++)//斜向右下三个棋子
       for(int j=0;j<N;j++)
       if ((cell[i][j].getToken() == token)&& (cell[i+1][j+1].getToken() == token)&& (cell[i+2][j+2].getToken() == token))
       {
       return true;
       }
       for(int j=2;j<N;j++)//斜向左下三个棋子
       for(int i=0;i<N-2;i++)
       if ((cell[i][j].getToken() == token)&& (cell[i+1]j-[1].getToken() == token)&& (cell[i+2][j-2].getToken() == token))
       {
       return true;
       }

    return false;
  }

#10


现在我的程序,输入N后,按开始按钮,没有出现想象中的吧面板p分成N*N的方格,不知道是为什么啊,也没有什么异常信息

#11


 public void actionPerformed(ActionEvent e) {
        if (e.getSource() == butn) {
            String number = text.getText();
            if (number != null && !"".equals(number.trim())) {
                N = Integer.parseInt(number);
            }
            cell = new Cell[N][N];
            p.setLayout(new GridLayout(N, N));
            for (int i = 0; i < N; i++) {
                for (int j = 0; j < N; j++) {
                    p.add(cell[i][j] = new Cell());
                }
            }
            this.getContentPane().getParent().add(p, BorderLayout.CENTER);
            this.getContentPane().getParent().repaint();
        }
    }

#12


引用 9 楼 soul2004521 的回复:
这个算法我是这样写的,开始时因为N输入不了,算法里有N,所以暂时用三子棋的算法代替了 

Java code
public boolean isWon(char token)
  {
    for (int i=0; i<N; i++)//横向三个棋子
        for(int j=0;j<N-2;j++)
      if ((cell[i][j].getToken() == token)&& (cell[i][j+1].getToken() == token)&& (cell[i][j+2].getToken() == token))
      {
        return true;
      }

    for (int j=0; j<N-2…


全盘棋判断,效率有点低。

#13


你的思路是不是有点不太对啊,N子棋,棋盘就N*N的大小?

#14


恩,ok了,谢谢你们,终于搞定了,就是输入一个数玩了一局时,还得重新开始游戏出入数字,接着输入不行。这个我自己再看看

#15


开发一个n子棋游戏,支持两个游戏者玩游戏。在游戏中,游戏者先在输入框中输入n值,n代表棋盘的行数和列数。然后游戏开始,两个游戏者在n*n网格的有效单元格中轮流作标记(一个人用X,另一个人用O)。如果一个游戏者在网格的水平、垂直或对角线方向上作了三个连续标记,游戏就以这个游戏者得胜而告终。当网络的所有单元格都标满了标记而没有产生优胜者,就出现了平局。
老师的要求,我那个判断的方法效率是低。有什么好的方法么?

#16


老师让你们做的N*N的棋盘,三子棋的规则

#17


恩。是啊,呵呵,我没表达好

#18


UP

#19


能不能把你的算法分享下啊   不胜感激    邮箱:qq363436899@163.com

#1


大家帮忙看一下啊,哪里出问题了

#2


构造函数里
JPanel p = new JPanel(); 
JPanel p1 = new JPanel();
p ,p1是局部变量,全局变量p没有赋值,所以出现空指针

#3


学习!up!

#4


这个要怎么改啊

#5


构造方法里
p = new JPanel();
p1 = new JPanel();

#6


引用 2 楼 swandragon 的回复:
构造函数里 
JPanel p = new JPanel(); 
JPanel p1 = new JPanel(); 
p ,p1是局部变量,全局变量p没有赋值,所以出现空指针 

正解

#7


改了,没有异常了,可我输入数值,按开始。他没有反应啊

#8


二楼说的对。

还有就是你的 public boolean isWon(char token)这个方法算法是不是不太对啊?
我觉得应该定义为下面这个
public boolean isWon(char token,int row,int column),然后再
判断在row行,column-2~column+2之间有没有棋子连成三个
判断在columng列,row-2~row+2之间有没有棋子连成三个,
判断左上到右下,(row-2,column-2)到(row+2,column+2)之间没有棋子连成三个
判断右上到左下,(row-2,column+2)到(row+2,column-2)之间有没有棋子连三个的。

#9



这个算法我是这样写的,开始时因为N输入不了,算法里有N,所以暂时用三子棋的算法代替了

public boolean isWon(char token)
  {
    for (int i=0; i<N; i++)//横向三个棋子
     for(int j=0;j<N-2;j++)
      if ((cell[i][j].getToken() == token)&& (cell[i][j+1].getToken() == token)&& (cell[i][j+2].getToken() == token))
      {
        return true;
      }

    for (int j=0; j<N-2; j++)//纵向三个棋子
     for(int i=0;i<N;i++)
      if ((cell[i][j].getToken() ==  token)
          && (cell[i+1][j].getToken() == token)
          && (cell[i+2][j].getToken() == token))
      {
        return true;
      }
      for(int i=0;i<N-2;i++)//斜向右下三个棋子
       for(int j=0;j<N;j++)
       if ((cell[i][j].getToken() == token)&& (cell[i+1][j+1].getToken() == token)&& (cell[i+2][j+2].getToken() == token))
       {
       return true;
       }
       for(int j=2;j<N;j++)//斜向左下三个棋子
       for(int i=0;i<N-2;i++)
       if ((cell[i][j].getToken() == token)&& (cell[i+1]j-[1].getToken() == token)&& (cell[i+2][j-2].getToken() == token))
       {
       return true;
       }

    return false;
  }

#10


现在我的程序,输入N后,按开始按钮,没有出现想象中的吧面板p分成N*N的方格,不知道是为什么啊,也没有什么异常信息

#11


 public void actionPerformed(ActionEvent e) {
        if (e.getSource() == butn) {
            String number = text.getText();
            if (number != null && !"".equals(number.trim())) {
                N = Integer.parseInt(number);
            }
            cell = new Cell[N][N];
            p.setLayout(new GridLayout(N, N));
            for (int i = 0; i < N; i++) {
                for (int j = 0; j < N; j++) {
                    p.add(cell[i][j] = new Cell());
                }
            }
            this.getContentPane().getParent().add(p, BorderLayout.CENTER);
            this.getContentPane().getParent().repaint();
        }
    }

#12


引用 9 楼 soul2004521 的回复:
这个算法我是这样写的,开始时因为N输入不了,算法里有N,所以暂时用三子棋的算法代替了 

Java code
public boolean isWon(char token)
  {
    for (int i=0; i<N; i++)//横向三个棋子
        for(int j=0;j<N-2;j++)
      if ((cell[i][j].getToken() == token)&& (cell[i][j+1].getToken() == token)&& (cell[i][j+2].getToken() == token))
      {
        return true;
      }

    for (int j=0; j<N-2…


全盘棋判断,效率有点低。

#13


你的思路是不是有点不太对啊,N子棋,棋盘就N*N的大小?

#14


恩,ok了,谢谢你们,终于搞定了,就是输入一个数玩了一局时,还得重新开始游戏出入数字,接着输入不行。这个我自己再看看

#15


开发一个n子棋游戏,支持两个游戏者玩游戏。在游戏中,游戏者先在输入框中输入n值,n代表棋盘的行数和列数。然后游戏开始,两个游戏者在n*n网格的有效单元格中轮流作标记(一个人用X,另一个人用O)。如果一个游戏者在网格的水平、垂直或对角线方向上作了三个连续标记,游戏就以这个游戏者得胜而告终。当网络的所有单元格都标满了标记而没有产生优胜者,就出现了平局。
老师的要求,我那个判断的方法效率是低。有什么好的方法么?

#16


老师让你们做的N*N的棋盘,三子棋的规则

#17


恩。是啊,呵呵,我没表达好

#18


UP

#19


能不能把你的算法分享下啊   不胜感激    邮箱:qq363436899@163.com

#20