我要改成这样 增加一个面板,文本区,按钮,但是程序有问题,各位帮忙改下 import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.LineBorder;public class NChess extends JApplet { // Indicate which player has a turn, initially it is the X player private char whoseTurn = 'X'; // Create and initialize cells private Cell[][] cell = new Cell[N][N];
private int N;
private JLabel lbl=new JLabel("请输入棋盘行数"); private JTextField text=new JTextField(); private JButton button=new JButton("开始"); // Create and initialize a status label private JLabel jlblStatus = new JLabel("X's turn to play"); // Initialize UI public void init() { // Panel p to hold cells JPanel p = new JPanel(); 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());
JPanel p1=new JPanel(); p1.setLayout(new FlowLayout()); p1.add(lbl); p1.add(text); p1.add(button); button.addActionListener(this); public void actionPerformed(ActionEvent e) { N=getText(); } // Set line borders on the cells panel and the status label p.setBorder(new LineBorder(Color.red, 1)); jlblStatus.setBorder(new LineBorder(Color.yellow, 1));
// Place the panel and the label to the applet this.getContentPane().add(p, BorderLayout.CENTER); this.getContentPane().add(jlblStatus, BorderLayout.SOUTH); this.getContentPane().add(p1, BorderLayout.NORTH); } // This main method enables the applet to run as an application public static void main(String[] args) { // Create a frame JFrame frame = new JFrame("TicTacToe"); // Create an instance of the applet NChess applet = new NChess(); // Add the applet instance to the frame frame.getContentPane().add(applet, BorderLayout.CENTER); // Invoke init() and start() applet.init(); applet.start(); // Display the frame frame.setSize(300, 300); // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 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<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; } // 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; } } }
增加一个面板,文本区,按钮,但是程序有问题,各位帮忙改下
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;public class NChess extends JApplet
{
// Indicate which player has a turn, initially it is the X player
private char whoseTurn = 'X'; // Create and initialize cells
private Cell[][] cell = new Cell[N][N];
private int N;
private JLabel lbl=new JLabel("请输入棋盘行数");
private JTextField text=new JTextField();
private JButton button=new JButton("开始");
// Create and initialize a status label
private JLabel jlblStatus = new JLabel("X's turn to play"); // Initialize UI
public void init()
{
// Panel p to hold cells
JPanel p = new JPanel();
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());
JPanel p1=new JPanel();
p1.setLayout(new FlowLayout());
p1.add(lbl);
p1.add(text);
p1.add(button);
button.addActionListener(this);
public void actionPerformed(ActionEvent e)
{
N=getText();
} // Set line borders on the cells panel and the status label
p.setBorder(new LineBorder(Color.red, 1));
jlblStatus.setBorder(new LineBorder(Color.yellow, 1));
// Place the panel and the label to the applet
this.getContentPane().add(p, BorderLayout.CENTER);
this.getContentPane().add(jlblStatus, BorderLayout.SOUTH);
this.getContentPane().add(p1, BorderLayout.NORTH);
} // This main method enables the applet to run as an application
public static void main(String[] args)
{
// Create a frame
JFrame frame = new JFrame("TicTacToe"); // Create an instance of the applet
NChess applet = new NChess(); // Add the applet instance to the frame
frame.getContentPane().add(applet, BorderLayout.CENTER); // Invoke init() and start()
applet.init();
applet.start(); // Display the frame
frame.setSize(300, 300);
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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<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;
} // 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;
}
}
}