import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.LineBorder;public class TicTacToe extends JApplet implements ActionListener
{
private JMenuItem choieMenuItem,exitMenuItem,wfMenuItem,bfMenuItem,newMenuItem,replayMenuItem,fiveMenuItem,tenMenuItem;
private JMenu choieMenu,fileMenu,exitMenu;
private char whoseTurn = 'B';
private Cell[][] cell = new Cell[5][5];
private JLabel jlblStatus = new JLabel("轮到白棋走"); public void init() {
JPanel p = new JPanel();
// p.setForeground(Color.red);
p.setLayout(new GridLayout(5, 5, 0, 0));
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
p.add(cell[i][j] = new Cell());
JMenuBar jmb=new JMenuBar();
fileMenu=new JMenu("游戏");
exitMenu=new JMenu("退出");
jmb.add(fileMenu);
jmb.add(exitMenu);
newMenuItem=new JMenuItem("新游戏 Ctrl+F2");
replayMenuItem=new JMenuItem("撤销 Ctrl +Z ");
bfMenuItem=new JMenuItem("黑棋先走 Ctrl+B");
wfMenuItem=new JMenuItem("白棋先走 Ctrl+W");
choieMenu=new JMenu("选择游戏 Ctrl+F5");
fiveMenuItem=new JMenuItem("5*5格式");
tenMenuItem=new JMenuItem("10*10格式");
choieMenu.add(fiveMenuItem);
choieMenu.add(tenMenuItem);
exitMenuItem=new JMenuItem("exit Ctrl+E");
fileMenu.add(newMenuItem);
fileMenu.add(replayMenuItem);
fileMenu.add(bfMenuItem);
fileMenu.add(wfMenuItem);
fileMenu.add(choieMenu);
exitMenu.add(exitMenuItem);
//创建快捷方式
newMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F2,ActionEvent.CTRL_MASK));
replayMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,ActionEvent.CTRL_MASK));
bfMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B,ActionEvent.CTRL_MASK));
wfMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,ActionEvent.CTRL_MASK));
exitMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,ActionEvent.CTRL_MASK));
//监听器
newMenuItem.addActionListener(this);
replayMenuItem.addActionListener(this);
bfMenuItem.addActionListener(this);
wfMenuItem.addActionListener(this);
exitMenuItem.addActionListener(this);
fiveMenuItem.addActionListener(this);
tenMenuItem.addActionListener(this);
// p.setBorder(new LineBorder(Color.red, 1));
// jlblStatus.setBorder(new LineBorder(Color.yellow, 1));
this.getContentPane().add(jmb,BorderLayout.NORTH);
this.getContentPane().add(p, BorderLayout.CENTER);
this.getContentPane().add(jlblStatus, BorderLayout.SOUTH); }
public void actionPerformed(ActionEvent e)
{
String actionCommand=e.getActionCommand();
if(e.getSource() instanceof JMenuItem)
{
if("新游戏".equals(actionCommand))
newGame();
if("撤销".equals(actionCommand))
rePlay();
if("黑棋先".equals(actionCommand))
bFirst();
if("白棋先".equals(actionCommand))
wFirst();
if("5*5".equals(actionCommand))
fivePlay();
if("10*10".equals(actionCommand))
tenPlay();
else if("exit".equals(actionCommand))
exitProcess();
}
}
public void newGame()
{ }public void rePlay()
{
}
public void bFirst()
{
}
public void wFirst()
{
}
public void fivePlay()
{
}
public void tenPlay()
{
}
public void exitProcess()
{
}
public static void main(String[] args) {
JFrame frame = new JFrame("Tic Tac Toe");
TicTacToe applet = new TicTacToe();
frame.getContentPane().add(applet, BorderLayout.CENTER); applet.init();
applet.start(); frame.setSize(300, 300);
frame.setVisible(true);
} public boolean isFull() {
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
if (cell[i][j].getToken() == ' ')
return false;
return true;
} public boolean isWon(char token) {
for (int i = 0; i < 5; i++)
if (cell[i][0].getToken() == token
&& (cell[i][1].getToken() == token)
&& (cell[i][2].getToken() == token)
&&(cell[i][3].getToken()==token)
&&(cell[i][4].getToken()==token))
{
return true;
} for (int j = 0; j < 5; j++)
if ((cell[0][j].getToken() == token)
&& (cell[1][j].getToken() == token)
&& (cell[2][j].getToken() == token)
&&(cell[3][j].getToken()==token)
&&(cell[4][j].getToken()==token))
{
return true;
} if ((cell[0][0].getToken() == token)
&& (cell[1][1].getToken() == token)
&& (cell[2][2].getToken() == token)
&&(cell[3][3].getToken()==token)
&&(cell[4][4].getToken()==token))
{
return true;
} if ((cell[0][4].getToken() == token)
&& (cell[1][3].getToken() == token)
&& (cell[2][2].getToken() == token)
&&(cell[3][1].getToken()==token)
&&(cell[4][0].getToken()==token))
{
return true;
} return false; } public class Cell extends JPanel implements MouseListener {
private char token = ' '; public Cell() {
setBorder(new LineBorder(Color.black, 1));
addMouseListener(this);
} public char getToken() {
return token;
} public void setToken(char c) {
token = c;
repaint();
} @Override
public void paint(Graphics g) {
super.paint(g);
if (token == 'B') {
g.setColor(Color.black);
g.fillOval(10, 10, getSize().width - 16, getSize().height - 16);
} else if (token == 'W') {
g.setColor(Color.yellow);
g.fillOval(10, 10, getSize().width - 16, getSize().height - 16);
}
} public void mouseClicked(MouseEvent e) {
if (token == ' ') {
if (whoseTurn == 'B') {
setToken('B');
whoseTurn = 'W';
jlblStatus.setText("轮到白棋走");
if (isWon('B'))
jlblStatus.setText("黑棋赢!The game is over");
else if (isFull())
jlblStatus.setText("平局!The game is over");
} else if (whoseTurn == 'W') {
setToken('W');
whoseTurn = 'B';
jlblStatus.setText("轮到黑棋走");
if (isWon('W'))
jlblStatus.setText("白棋赢!The game is over");
else if (isFull())
jlblStatus.setText("平局!The game is over");
}
}
} public void mousePressed(MouseEvent e) {
} public void mouseReleased(MouseEvent e) {
} public void mouseEntered(MouseEvent e) {
} public void mouseExited(MouseEvent e) {
}
}
}
选择了判断e.getActionCommand()
这个要和你的按钮名称完全一致
改成这样,才能触发
如果想问具体里面的功能怎么实现的话
这个就要你自己设计了,因为这是你自己的程序 public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
if (e.getSource() instanceof JMenuItem) {
if ("新游戏 Ctrl+F2".equals(actionCommand))
newGame();
if ("撤销 Ctrl +Z ".equals(actionCommand))
rePlay();
if ("黑棋先走 Ctrl+B".equals(actionCommand))
bFirst();
if ("白棋先走 Ctrl+W".equals(actionCommand))
wFirst();
if ("5*5格式".equals(actionCommand))
fivePlay();
if ("10*10格式".equals(actionCommand))
tenPlay();
else if ("exit Ctrl+E".equals(actionCommand))
exitProcess();
}
}
String actionCommand = e.getActionCommand();
if (e.getSource() instanceof JMenuItem) {
if ("新游戏 Ctrl+F2".equals(actionCommand))
newGame();
if ("撤销 Ctrl +Z ".equals(actionCommand))
rePlay();
if ("黑棋先走 Ctrl+B".equals(actionCommand))
bFirst();
if ("白棋先走 Ctrl+W".equals(actionCommand))
wFirst();
if ("5*5格式".equals(actionCommand))
fivePlay();
if ("10*10格式".equals(actionCommand))
tenPlay();
else if ("exit Ctrl+E".equals(actionCommand))
exitProcess();
}
}
{ }public void rePlay()
{
}
public void bFirst()
{
}
public void wFirst()
{
}
public void fivePlay()
{
}
public void tenPlay()
{
}
public void exitProcess()
{
}怎么写,实现一个也好啊……就教教我吧……
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.LineBorder;public class TicTacToe extends JApplet implements ActionListener
{
private JMenuItem choieMenuItem,exitMenuItem,wfMenuItem,bfMenuItem,newMenuItem,replayMenuItem,fiveMenuItem,tenMenuItem;
private JMenu choieMenu,fileMenu,exitMenu;
private char whoseTurn = 'B';
private Cell[][] cell = new Cell[10][10];
private JLabel jlblStatus = new JLabel("轮到白棋走");
private JPasswordField jpfPassword; public void init() {
JPanel p = new JPanel();
// p.setForeground(Color.red);
p.setLayout(new GridLayout(10, 10, 0, 0));
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
p.add(cell[i][j] = new Cell());
JMenuBar jmb=new JMenuBar();
fileMenu=new JMenu("游戏");
exitMenu=new JMenu("退出");
jmb.add(fileMenu);
jmb.add(exitMenu);
newMenuItem=new JMenuItem("新游戏 ");
replayMenuItem=new JMenuItem("撤销 ");
bfMenuItem=new JMenuItem("黑棋先走 ");
wfMenuItem=new JMenuItem("白棋先走 ");
choieMenu=new JMenu("选择游戏");
fiveMenuItem=new JMenuItem("5*5格式");
tenMenuItem=new JMenuItem("10*10格式");
choieMenu.add(fiveMenuItem);
choieMenu.add(tenMenuItem);
exitMenuItem=new JMenuItem("exit");
fileMenu.add(newMenuItem);
fileMenu.add(replayMenuItem);
fileMenu.add(bfMenuItem);
fileMenu.add(wfMenuItem);
fileMenu.add(choieMenu);
exitMenu.add(exitMenuItem);
//创建快捷方式
newMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F2,ActionEvent.CTRL_MASK));
replayMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,ActionEvent.CTRL_MASK));
bfMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B,ActionEvent.CTRL_MASK));
wfMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,ActionEvent.CTRL_MASK));
exitMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,ActionEvent.CTRL_MASK));
//监听器
newMenuItem.addActionListener(this);
replayMenuItem.addActionListener(this);
bfMenuItem.addActionListener(this);
wfMenuItem.addActionListener(this);
exitMenuItem.addActionListener(this);
fiveMenuItem.addActionListener(this);
tenMenuItem.addActionListener(this);
// p.setBorder(new LineBorder(Color.red, 1));
// jlblStatus.setBorder(new LineBorder(Color.yellow, 1));
this.getContentPane().add(jmb,BorderLayout.NORTH);
this.getContentPane().add(p, BorderLayout.CENTER);
this.getContentPane().add(jlblStatus, BorderLayout.SOUTH); }
public void actionPerformed(ActionEvent e)
{
String actionCommand=e.getActionCommand();
if(e.getSource() instanceof JMenuItem)
{
if("新游戏".equals(actionCommand))
newGame();
if("撤销".equals(actionCommand))
rePlay();
if("黑棋先".equals(actionCommand))
bFirst();
if("白棋先".equals(actionCommand))
wFirst();
if("5*5".equals(actionCommand))
fivePlay();
if("10*10".equals(actionCommand))
tenPlay();
else if("exit".equals(actionCommand))
exitProcess();
}
}
public void newGame()
{
}public void rePlay()
{
}
public void bFirst()
{
}
public void wFirst()
{
}
public void fivePlay()
{
}
public void tenPlay()
{
}
public void exitProcess()
{
}
public static void main(String[] args) {
JFrame frame = new JFrame("Tic Tac Toe");
TicTacToe applet = new TicTacToe();
frame.getContentPane().add(applet, BorderLayout.CENTER); applet.init();
applet.start(); frame.setSize(600,600);
frame.setVisible(true);
} public boolean isFull() {
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
if (cell[i][j].getToken() == ' ')
return false;
return true;
} public boolean isWon(char token) {
for (int i = 0; i < 10; i++)
{
for(int j=0;j<10;j++)
if (cell[i][j].getToken() == token
&& (cell[i][j+1].getToken() == token)
&& (cell[i][j+2].getToken() == token)
&&(cell[i][j+3].getToken()==token)
&&(cell[i][j+4].getToken()==token))
{
return true;
}
}
for (int j = 0; j < 10; j++)
{
for(int i=0;i<10;i++)
if ((cell[i][j].getToken() == token)
&& (cell[i+1][j].getToken() == token)
&& (cell[i+2][j].getToken() == token)
&&(cell[i+3][j].getToken()==token)
&&(cell[i+4][j].getToken()==token))
{
return true;
}
}
for(int i=0;i<10;i++)
{
if ((cell[i][i].getToken() == token)
&& (cell[i+1][i+1].getToken() == token)
&& (cell[i+2][i+2].getToken() == token)
&&(cell[i+3][i+3].getToken()==token)
&&(cell[i+4][i+4].getToken()==token))
{
return true;
}
}
for(int i=0;i<10;i++)
{
int j=9-i;
if ((cell[i][j].getToken() == token)
&& (cell[i+1][j-1].getToken() == token)
&& (cell[i+2][j-2].getToken() == token)
&& (cell[i+3][j-3].getToken()==token)
&& (cell[i+4][j-4].getToken()==token))
{
return true;
}
}
return false; }
public class Cell extends JPanel implements MouseListener {
private char token = ' '; public Cell() {
setBorder(new LineBorder(Color.black, 1));
addMouseListener(this);
} public char getToken() {
return token;
} public void setToken(char c) {
token = c;
repaint();
} @Override
public void paint(Graphics g) {
super.paint(g);
if (token == 'B') {
g.setColor(Color.black);
g.fillOval(10, 10, getSize().width - 16, getSize().height - 20);
} else if (token == 'W') {
g.setColor(Color.yellow);
g.fillOval(10, 10, getSize().width - 16, getSize().height - 20);
}
} public void mouseClicked(MouseEvent e) {
if (token == ' ') {
if (whoseTurn == 'B') {
setToken('B');
whoseTurn = 'W';
jlblStatus.setText("轮到白棋走");
if (isWon('B'))
jlblStatus.setText("黑棋赢!The game is over");
else if (isFull())
jlblStatus.setText("平局!The game is over");
} else if (whoseTurn == 'W') {
setToken('W');
whoseTurn = 'B';
jlblStatus.setText("轮到黑棋走");
if (isWon('W'))
jlblStatus.setText("白棋赢!The game is over");
else if (isFull())
jlblStatus.setText("平局!The game is over");
}
}
} public void mousePressed(MouseEvent e) {
} public void mouseReleased(MouseEvent e) {
} public void mouseEntered(MouseEvent e) {
} public void mouseExited(MouseEvent e) {
}
}
}
还是没思路……还有要怎么让赢输或者和棋时弹出一个对话框呢?
可以实现弹出白棋赢的对话框!