我正在使用Eclipse在Java中进行tic tac toe游戏。我的源代码都已完成但由于某种原因,它不起作用,并且菜单没有出现.Eclipse没有显示任何错误。有人可以帮助我吗?
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
public class Jogo extends JFrame {
private static final long serialVersionUID = 1L;
private JButton b1,b2,b3,b4,b5,b6,b7,b8,b9;
int qtde;
int jogador;
int mat[][] = new int [3][3];
JButton b[] = new JButton[9];
String ganhador;
String jogador1;
String jogador2;
public Jogo() {
setTitle("Jogo da Velha");
setBounds(190,100,300,400);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setBackground(new Color(197,197,197));
setLayout(null);
b1 = new JButton();
b1.setBounds(25,50,60,70);
this.add(b1);
b2 = new JButton();
b2.setBounds(115,50,60,70);
this.add(b2);
b3 = new JButton();
b3.setBounds(205,50,60,70);
this.add(b3);
b4 = new JButton();
b4.setBounds(25,140,60,70);
this.add(b4);
b5 = new JButton();
b5.setBounds(115,140,60,70);
this.add(b5);
b6 = new JButton();
b6.setBounds(205,140,60,70);
this.add(b6);
b7 = new JButton();
b7.setBounds(25,230,60,70);
this.add(b7);
b8 = new JButton();
b8.setBounds(115,230,60,70);
this.add(b8);
b9 = new JButton();
b9.setBounds(205,230,60,70);
this.add(b9);
qtde = 1;
jogador = 1;
b[0] = b1;
b[1] = b2;
b[2] = b3;
b[3] = b4;
b[4] = b5;
b[5] = b6;
b[6] = b7;
b[7] = b8;
b[8] = b9;
}
public void jogada(JButton b,int x, int y){
b.setEnabled(false);
if (jogador == 1){
mat [x][y] = 1;
b.setText("X");
jogador = 2;
ganhador = jogador1;
checarjogada(1);
} else {
mat [x][y] = 2;
b.setText("O");
jogador =1;
ganhador = jogador2;
checarjogada(2);
}
qtde++;
}
public void checarjogada(int x){
if (vitoria(x)== true){
JOptionPane.showMessageDialog(null,"Jogador:"+ganhador+""+"Venceu", "Vitória!",JOptionPane.INFORMATION_MESSAGE);
fimdojogo();
}
}
public boolean vitoria (int x){
for (int i = 0; i < mat.length;i++){
if(mat[i][0] == x && mat [i][1] == x && mat [i][2] == x){
return true;
}
if (mat[0][i]== x && mat [1][i] == x && mat [2][i] == x){
return true;
}
}
if(mat[0][0] == x && mat [1][1] == x && mat[2][2] == x){
return true;
}
if(mat[0][2] == x && mat [1][1] == x && mat [2][0] == x){
return true;
}
return false;
}
public void fimdojogo(){
for(int i =0; i<9;i++){
b[i].setEnabled(false);
}
}
public void limpar(){
for(int i=0;i < 9; i++){
b[i].setEnabled(true);
b[i].setText("");
}
for(int x=0; x < 3; x++){
for(int y=0;y < 3;y++){
mat[x][y] = 0;
}
}
jogador = 1;
jogador1 = "";
jogador2 = "";
ganhador = "";
jMenuBar1 = new JMenuBar();
jMenu1 = new JMenu();
jMenuItem1 = new JMenuItem();
jMenu2 = new JMenu();
b1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b1ActionPerformed(evt);
}
});
b2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b2ActionPerformed(evt);
}
});
b3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b3ActionPerformed(evt);
}
});
b4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b4ActionPerformed(evt);
}
});
b5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b5ActionPerformed(evt);
}
});
b6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b6ActionPerformed(evt);
}
});
b7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b7ActionPerformed(evt);
}
});
b8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b8ActionPerformed(evt);
}
});
b9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b9ActionPerformed(evt);
}
});
jMenu1.setMnemonic('N');
jMenu1.setText("Opções");
jMenu1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenu1ActionPerformed(evt);
}
});
jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F8, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem1.setMnemonic('N');
jMenuItem1.setText("Novo Jogo");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem1);
jMenuBar1.add(jMenu1);
jMenu2.setText("Sair");
jMenu2.addMenuListener(new javax.swing.event.MenuListener() {
public void menuCanceled(javax.swing.event.MenuEvent evt) {
}
public void menuDeselected(javax.swing.event.MenuEvent evt) {
}
public void menuSelected(javax.swing.event.MenuEvent evt) {
jMenu2MenuSelected(evt);
}
});
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
}
private void jMenuItem1ActionPerformed(ActionEvent evt) {
}
private void b1ActionPerformed(ActionEvent evt) {
jogada(b1,0,0);
}
private void b2ActionPerformed(ActionEvent evt) {
jogada(b2,0,1);
}
private void b3ActionPerformed(ActionEvent evt) {
jogada(b3,0,2);
}
private void b4ActionPerformed(ActionEvent evt) {
jogada(b4,1,0);
}
private void b5ActionPerformed(ActionEvent evt) {
jogada(b5,1,1);
}
private void b6ActionPerformed(ActionEvent evt) {
jogada(b6,1,2);
}
private void b7ActionPerformed(ActionEvent evt) {
jogada(b7,2,0);
}
private void b8ActionPerformed(ActionEvent evt) {
jogada(b8,2,1);
}
private void b9ActionPerformed(ActionEvent evt) {
jogada(b9,2,2);
}
private void jMenu2MenuSelected(MenuEvent evt) {
System.exit(0);
}
private void jMenu1ActionPerformed(ActionEvent evt) {
limpar();
jogador1 = JOptionPane.showInputDialog("Digite o nome do Jogador 1");
jogador2 = JOptionPane.showInputDialog("Digite o nome do Jogador 2");
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Jogo().setVisible(true);
}
});
}
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
}
答案 0 :(得分:2)
听起来你在问为什么菜单栏没有出现? 这是您的代码的简化版本:
public class Jogo extends JFrame {
public Jogo() {
...
//Adding buttons
}
private void jMenu1ActionPerformed(ActionEvent evt) {
limpar();
...
}
public void limpar(){
...
jMenuBar1 = new JMenuBar();
...
setJMenuBar(jMenuBar1);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Jogo().setVisible(true);
}
});
}
它没有显示,因为你没有创建菜单栏。
您只在调用limpar()
方法时创建菜单,但只在jMenu1
执行操作时才调用此方法。所以它永远不会发生,因为你无法在尚未创建的菜单上执行操作!您需要在类的构造函数中创建JMenuBar
。
答案 1 :(得分:0)
为什么不使用循环来创建按钮? 这是我的:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.*;
public class TicTacToe extends JFrame {
JPanel p = new JPanel();
XOButton button[] = new XOButton[9];
JButton clear = new JButton("CLEAR!");
public static void main(String[] args) {
new TicTacToe();
}
public TicTacToe() {
setTitle("Tic Tac Toe!");
setSize(600, 800);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(true);
p.setLayout(new GridLayout(4,3));
for (int i=0; i<9; i++) {
button[i] = new XOButton();
p.add(button[i]);
clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
button[0].setIcon(null);
button[1].setIcon(null);
button[2].setIcon(null);
button[3].setIcon(null);
button[4].setIcon(null);
button[5].setIcon(null);
button[6].setIcon(null);
button[7].setIcon(null);
button[8].setIcon(null);
}
});
p.add(clear);
add(p);
setVisible(true);
}
}
}
==== IOButton的CLASS ==========
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class XOButton extends JButton implements ActionListener {
ImageIcon X,O;
byte value=0;
public XOButton() {
X = new ImageIcon(this.getClass().getResource("X.gif"));
O = new ImageIcon(this.getClass().getResource("O.png"));
this.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
value++;
value%=3;
switch(value) {
case 0:
setIcon(null);
break;
case 1:
setIcon(X);
break;
case 2:
setIcon(O);
break;
}
}
}
有看了