Java 当我尝试在eclipse中运行这个类时,会执行另一个类

Java 当我尝试在eclipse中运行这个类时,会执行另一个类,java,eclipse,Java,Eclipse,我有一门课叫deepToe_2。每当我点击run的快捷键时,eclipse就会运行另一个类,名为TicTacToe,这是我以前运行过的。 我尝试过改变运行cofig,但它似乎仍然不起作用。 这是我的密码: package deepToe; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.*; import j

我有一门课叫deepToe_2。每当我点击run的快捷键时,eclipse就会运行另一个类,名为TicTacToe,这是我以前运行过的。 我尝试过改变运行cofig,但它似乎仍然不起作用。 这是我的密码:

package deepToe;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;
import deepToe.deepToe_2.buttonListener;

public class deepToe_2 extends JPanel {

JButton buttons[] = new JButton[9]; 
int alternate = 0;
String MotherBoard[] = {"","","","","","","","",""};
String board[];

public static void main(String[] args) {

    JFrame window = new JFrame("Tic Tac Toe");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.getContentPane().add(new TicTacToe());
    window.setBounds(300,200,300,300);
    window.setVisible(true);
}

public deepToe_2()
{
  setLayout(new GridLayout(3,3));
  initializebuttons();
  toss();
}

public void toss(){
    Random ran = new Random();
    int r = ran.nextInt(2);
    if(r == 1){
        JOptionPane.showMessageDialog(null, "deepToe goes First.");
        alternate++;
        deepToe();
    }
    else {
        JOptionPane.showMessageDialog(null, "deepToe goes First.");
        alternate++;
        deepToe();
    }
}

public void deepToe() {
    if(alternate == 1) {
        buttons[4].setText("O");
    }else {
        int moves[] = {-1000,-1000,-1000,-1000,-1000,-1000,-1000,-1000,-1000};
        for(int i = 0; i < 9; i++) {
            board = MotherBoard;
            if(board[i] == "") {
                board[i] = "O";
                moves[i] = evaluateBoard();
            }
        }
        int high = -999, final_move = -1;
        for(int j = 0; j < 9; j++) {
            if(moves[j] > high){
                high = moves[j];
                final_move = j;
            }
        }
        buttons[final_move].setText("O");
    }
    updateMotherBoard();
    if(!(checkResults())) alternate++;
}

public int evaluateBoard() {
    int x = 0;
    if(canSomeoneWin("O") != -1) x += 2;
    if(canSomeoneWin("X") != -1) x -= 2;
    return x;
}

public void updateMotherBoard() {
    for(int i = 0; i < 9; i++) {
        MotherBoard[i] = buttons[i].getText();
    }
}

public int canSomeoneWin(String c) {
    int x = 0, y = 1, z = 2;
    for(int j = 0; j < 3; j++){
        if((board[y] == board[z])&&(board[y] == c)&&(board[x] == "")) return x;
        if((board[x] == board[z])&&(board[x] == c)&&(board[y] == "")) return y;
        if((board[x] == board[y])&&(board[x] == c)&&(board[z] == "")) return z;
        x += 3;
        y += 3;
        z += 3;
    }
    x = 0; y = 3; z = 6;
    for(int j = 0; j < 3; j++){
        if((board[y] == board[z])&&(board[y] == c)&&(board[x] == "")) return x;
        if((board[x] == board[z])&&(board[x] == c)&&(board[y] == "")) return y;
        if((board[x] == board[y])&&(board[x] == c)&&(board[z] == "")) return z;
        x++;
        y++;
        z++;
    }
    x = 0; y = 4; z = 8;
    for(int j = 0; j < 2; j++){
        if((board[y] == board[z])&&(board[y] == c)&&(board[x] == "")) return x;
        if((board[x] == board[z])&&(board[x] == c)&&(board[y] == "")) return y;
        if((board[x] == board[y])&&(board[x] == c)&&(board[z] == "")) return z;
        x = 2; z = 6;
    }
    return -1;

}

public void initializebuttons()
{
    for(int i = 0; i <= 8; i++)
    {
        buttons[i] = new JButton();
        buttons[i].setText("");
        buttons[i].addActionListener(new buttonListener());

        add(buttons[i]);           
    }
    buttons[5].setText("X");

}

public class buttonListener implements ActionListener
{

    public void actionPerformed(ActionEvent e) 
    {

        JButton buttonClicked = (JButton)e.getSource(); 

        if((alternate%2 == 0)&&(buttonClicked.getText().equals(""))){
            buttonClicked.setText("X");
            updateMotherBoard();
            if(!(checkResults())) {
                alternate++;
                deepToe();
            }
        }

    }
}

    public boolean checkResults(){
        if(checkForWin())
        {
            if(alternate%2 == 0)
                JOptionPane.showConfirmDialog(null, "Human wins. Great Job!!!");
            else
                JOptionPane.showConfirmDialog(null, "deepToe wins.");  
            return true;
        }else{
            if(checkForDraw()){
                JOptionPane.showConfirmDialog(null, "Draw. Good game.");
                return true;
            }
        }
        return false;

    }

    public boolean checkForDraw() {
        boolean yes = true;
        for(int j = 0; j < 9; j++){
            if(buttons[j].getText().equals("")) yes = false;
        }
        return yes;
    }

    public boolean checkForWin()
    {
        if( checkAdjacent(0,1) && checkAdjacent(1,2) ) //no need to put " == true" because the default check is for true
            return true;
        else if( checkAdjacent(3,4) && checkAdjacent(4,5) )
            return true;
        else if ( checkAdjacent(6,7) && checkAdjacent(7,8))
            return true;
        else if ( checkAdjacent(0,3) && checkAdjacent(3,6))
            return true;  
        else if ( checkAdjacent(1,4) && checkAdjacent(4,7))
            return true;
        else if ( checkAdjacent(2,5) && checkAdjacent(5,8))
            return true;
        else if ( checkAdjacent(0,4) && checkAdjacent(4,8))
            return true;  
        else if ( checkAdjacent(2,4) && checkAdjacent(4,6))
            return true;
        else 
            return false;


    }

    public boolean checkAdjacent(int a, int b)
    {
        if ( buttons[a].getText().equals(buttons[b].getText()) && !buttons[a].getText().equals("") )
            return true;
        else
            return false;
    }

}
package-deepToe;
导入java.awt.GridLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.*;
导入javax.swing.*;
导入deepToe.deepToe_2.buttonListener;
公共类deepToe_2扩展JPanel{
JButton按钮[]=新JButton[9];
int alternate=0;
字符串主板[]={“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”};
线板[];
公共静态void main(字符串[]args){
JFrame窗口=新JFrame(“Tic Tac Toe”);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(新的TicTacToe());
窗.立根(300300300);
window.setVisible(true);
}
公共深水桋2()
{
setLayout(新网格布局(3,3));
初始化按钮();
投掷();
}
公众投票{
Random ran=新的Random();
int r=ran.nextInt(2);
如果(r==1){
showMessageDialog(null,“deepToe优先”);
交替++;
深脚趾();
}
否则{
showMessageDialog(null,“deepToe优先”);
交替++;
深脚趾();
}
}
公共空间{
如果(备选==1){
按钮[4]。设置文本(“O”);
}否则{
int移动[]={-1000,-1000,-1000,-1000,-1000,-1000,-1000,-1000,-1000};
对于(int i=0;i<9;i++){
主板=主板;
如果(板[i]==“”){
板[i]=“O”;
移动[i]=评估板();
}
}
int高=-999,最后移动=-1;
对于(int j=0;j<9;j++){
如果(移动[j]>高){
高=移动[j];
最终移动=j;
}
}
按钮[最后移动].setText(“O”);
}
更新主板();
如果(!(checkResults())替换++;
}
公共int评估板(){
int x=0;
如果(canSomeoneWin(“O”)!=-1)x+=2;
如果(canSomeoneWin(“X”)!=-1)X-=2;
返回x;
}
public void updatemoreboard(){
对于(int i=0;i<9;i++){
主板[i]=按钮[i].getText();
}
}
公共int-canSomeoneWin(字符串c){
int x=0,y=1,z=2;
对于(int j=0;j<3;j++){
如果((线路板[y]==线路板[z])&&&(线路板[y]==c)&&(线路板[x]==”)返回x;
如果((board[x]==board[z])&&(board[x]==c)&(board[y]==“”)返回y;
如果((board[x]==board[y])&&(board[x]==c)&(board[z]==“”)返回z;
x+=3;
y+=3;
z+=3;
}
x=0;y=3;z=6;
对于(int j=0;j<3;j++){
如果((线路板[y]==线路板[z])&&&(线路板[y]==c)&&(线路板[x]==”)返回x;
如果((board[x]==board[z])&&(board[x]==c)&(board[y]==“”)返回y;
如果((board[x]==board[y])&&(board[x]==c)&(board[z]==“”)返回z;
x++;
y++;
z++;
}
x=0;y=4;z=8;
对于(int j=0;j<2;j++){
如果((线路板[y]==线路板[z])&&&(线路板[y]==c)&&(线路板[x]==”)返回x;
如果((board[x]==board[z])&&(board[x]==c)&(board[y]==“”)返回y;
如果((board[x]==board[y])&&(board[x]==c)&(board[z]==“”)返回z;
x=2;z=6;
}
返回-1;
}
public void initializebuttons()
{

对于(int i=0;i在类文件或类本身中单击鼠标右键,然后单击“运行方式”->“Java应用程序”

,您只需双击您的类,然后从任意位置运行程序。您可以按照kkflf的建议单击鼠标右键,也可以像以前一样从快捷键运行程序。
希望这会有所帮助。

它可能正在运行此类,但您在框架中插入了错误的面板:

window.getContentPane().add(new TicTacToe());
它应该是
deepToe_2
类似于

window.getContentPane().add(new deepToe_2());
因为在偏序代码中没有对该类的其他引用

为了确保更改框架的标题:

JFrame window = new JFrame("Deep Toe 2");  // or so

我还鼓励您(学习)使用调试器,b是否有助于识别此类问题…

使用“运行方式…”你怎么知道它运行的是另一个类而不是这个?试过了。它不起作用。它运行的是以前的程序本身。请帮助我。谢谢你的尝试,你很棒。尝试清理你的项目你是什么意思?我不知道这是否能帮你找到解决方案,但在类名下面有一条黄线。我将我的类重命名为deepToe2并将其放在另一个名为deepToe2的包中。然后,当我运行它时,我得到一个错误:线程“main”java.lang中出现异常。错误:未解决的编译问题:在deepToe2.deepToe2.main(deepToe2.java:21)尝试过。它不起作用。它本身运行了上一个程序。请帮助我。谢谢你的尝试,你很棒。我不知道这是否能帮助你找到解决方案,但类名下面有一条黄线。@DeepCraft黄线表示有警告…将鼠标悬停在上面阅读它或打开/查找“问题”查看(菜单:Window->Show view…->Problems)-这可能是因为“串行版本ID”丢失并且可以忽略(目前)您确定这是另一个正在运行的示例吗?因为从我看到的情况来看,您的主方法中标题为Tic Tac Toe的帧: