⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exercise14_1.java

📁 一款用java编写的小型数据库管理系统
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;

public class Exercise14_1 extends JApplet {
  private char whoseTurn = 'X';
  private Cell[][] cells =  new Cell[3][3];
  private JLabel jlblStatus = new JLabel("X's turn to play");
  public Exercise14_1() {
    JPanel jp=new JPanel();
    JButton jb=new JButton("New Game");
    jp.setLayout(new GridLayout(1,1));
    jp.add(jb);
    JPanel p = new JPanel(new GridLayout(3, 3, 0, 0));
    for (int i = 0; i < 3; i++)
      for (int j = 0; j < 3; j++)
        p.add(cells[i][j] = new Cell());
    p.setBorder(new LineBorder(Color.red, 1));
    jlblStatus.setBorder(new LineBorder(Color.yellow, 1));
    jb.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e){
			for (int i = 0; i < 3; i++)
      			for (int j = 0; j < 3; j++)
      			cells[i][j].setToken(' ');
			}
		});
    add(jp,BorderLayout.NORTH);
    add(p, BorderLayout.CENTER);
    add(jlblStatus, BorderLayout.SOUTH);
  }
  public boolean isFull() {
    for (int i = 0; i < 3; i++)
      for (int j = 0; j < 3; j++)
        if (cells[i][j].getToken() == ' ')
          return false;

    return true;
  }
  public boolean isWon(char token) {
    for (int i = 0; i < 3; i++)
      if ((cells[i][0].getToken() == token)
          && (cells[i][1].getToken() == token)
          && (cells[i][2].getToken() == token)) {
        return true;
      }
    for (int j = 0; j < 3; j++)
      if ((cells[0][j].getToken() ==  token)
          && (cells[1][j].getToken() == token)
          && (cells[2][j].getToken() == token)) {
        return true;
      }
    if ((cells[0][0].getToken() == token)
        && (cells[1][1].getToken() == token)
        && (cells[2][2].getToken() == token)) {
      return true;
    }
    if ((cells[0][2].getToken() == token)
        && (cells[1][1].getToken() == token)
        && (cells[2][0].getToken() == token)) {
      return true;
    }
    return false;
  }
  public class Cell extends JPanel {
    private char token = ' ';
    public Cell() {
      setBorder(new LineBorder(Color.black, 1));
      addMouseListener(new MouseListener());
    }
    public char getToken() {
      return token;
    }
    public void setToken(char c) {
      token = c;
      repaint();
    }
    protected void paintComponent(Graphics g) {
      super.paintComponent(g);

      if (token == 'X') {
        g.drawLine(10, 10, getWidth() - 10, getHeight() - 10);
        g.drawLine(getWidth() - 10, 10, 10, getHeight() - 10);
      }
      else if (token == 'O') {
        g.drawOval(10, 10, getWidth() - 20, getHeight() - 20);
      }
    }
    private class MouseListener extends MouseAdapter {
      public void mouseClicked(MouseEvent e) {
        if (token == ' ' && whoseTurn != ' ') {
          setToken(whoseTurn);
          if (isWon(whoseTurn)) {
            jlblStatus.setText(whoseTurn + " won! The game is over");
            whoseTurn = ' ';
          }
          else if (isFull()) {
            jlblStatus.setText("Draw! The game is over");
            whoseTurn = ' ';
          }
          else {
            whoseTurn = (whoseTurn == 'X') ? 'O': 'X';
            jlblStatus.setText(whoseTurn + "'s turn");
          }
        }
      }
    }
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame("TicTacToe");
    Exercise14_1 applet = new Exercise14_1();
    frame.add(applet, BorderLayout.CENTER);
    frame.setSize(300, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -