Java方法调用另一个类的方法

Java方法调用另一个类的方法,java,Java,这是主要课程: public class Sudoku extends JFrame { MenuBar Bar; MenuItem itemFile[]; MenuItem itemHelp[]; Menu file; Menu help; public Sudoku() { super("Sudoku"); setLayout(new FlowLayout()); setResizable(false); itemFile =new MenuItem[

这是主要课程:

public class Sudoku extends JFrame {
    MenuBar Bar;
MenuItem itemFile[];
MenuItem itemHelp[];
Menu file;
Menu help;
public Sudoku() {
    super("Sudoku");
    setLayout(new FlowLayout());
    setResizable(false);
    itemFile =new MenuItem[2];
    itemHelp =new MenuItem[2];
    itemFile[0]=new MenuItem("New");
    itemFile[1]=new MenuItem("Exit");
    itemHelp[0]= new MenuItem("Rules");
    itemHelp[1]= new MenuItem("Creators");
    file=new Menu("File");
    help=new Menu("Help");
    file.add(itemFile[0]);
    help.add(itemHelp[0]);
    file.add(itemFile[1]);
    help.add(itemHelp[1]);
    Bar =new MenuBar();
    Bar.add(file);
    Bar.add(help);
    setMenuBar(Bar);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    getContentPane().setLayout(new BorderLayout());

    Game game = new Game();

    ButtonController buttonController = new ButtonController(game);
    ButtonPanel buttonPanel = new ButtonPanel();
    buttonPanel.setController(buttonController);
    add(buttonPanel, BorderLayout.EAST);

    SudokuPanel sudokuPanel = new SudokuPanel();
    SudokuController sudokuController = new SudokuController(sudokuPanel, game);
    sudokuPanel.setGame(game);
    sudokuPanel.setController(sudokuController);
    add(sudokuPanel, BorderLayout.CENTER);

    game.addObserver(buttonPanel);
    game.addObserver(sudokuPanel);

    pack();
    setLocationRelativeTo(null);
    setVisible(true);
}

    public boolean action(Event evt, Object arg){
    if(evt.target instanceof MenuItem){
        String text=(String)arg;
        System.out.println("Selected="+arg);
        if(text.equals("Exit"))
            System.exit(0);
        else if(text.equals("New"))
            // Get newGame() of Game class to run here.
    }

    return true;
}
这是游戏类:

package sudoku.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Observable;

public class Game extends Observable {
private int[][] solution;      
private int[][] game;           
private boolean[][] check;      
private int selectedNumber;     
private boolean help;          


public Game() {
    newGame();
    check = new boolean[9][9];
    help = true;
}

public void newGame() {
    solution = generateSolution(new int[9][9], 0);
    game = generateGame(copy(solution));
    setChanged();
    notifyObservers(UpdateAction.NEW_GAME);
}

public void checkGame() {
    selectedNumber = 0;
    for (int y = 0; y < 9; y++) {
        for (int x = 0; x < 9; x++)
            check[y][x] = game[y][x] == solution[y][x];
    }
    setChanged();
    notifyObservers(UpdateAction.CHECK);
}

public void setHelp(boolean help) {
    this.help = help;
    setChanged();
    notifyObservers(UpdateAction.HELP);
}

public void setSelectedNumber(int selectedNumber) {
    this.selectedNumber = selectedNumber;
    setChanged();
    notifyObservers(UpdateAction.SELECTED_NUMBER);
}

public int getSelectedNumber() {
    return selectedNumber;
}

public boolean isHelp() {
    return help;
}

public boolean isSelectedNumberCandidate(int x, int y) {
    return game[y][x] == 0 && isPossibleX(game, y, selectedNumber)
            && isPossibleY(game, x, selectedNumber) && isPossibleBlock(game, x, y, selectedNumber);
}

public void setNumber(int x, int y, int number) {
    game[y][x] = number;
}

public int getNumber(int x, int y) {
    return game[y][x];
}

public boolean isCheckValid(int x, int y) {
    return check[y][x];
}

private boolean isPossibleX(int[][] game, int y, int number) {
    for (int x = 0; x < 9; x++) {
        if (game[y][x] == number)
            return false;
    }
    return true;
}

private boolean isPossibleY(int[][] game, int x, int number) {
    for (int y = 0; y < 9; y++) {
        if (game[y][x] == number)
            return false;
    }
    return true;
}

private boolean isPossibleBlock(int[][] game, int x, int y, int number) {
    int x1 = x < 3 ? 0 : x < 6 ? 3 : 6;
    int y1 = y < 3 ? 0 : y < 6 ? 3 : 6;
    for (int yy = y1; yy < y1 + 3; yy++) {
        for (int xx = x1; xx < x1 + 3; xx++) {
            if (game[yy][xx] == number)
                return false;
        }
    }
    return true;
}

private int getNextPossibleNumber(int[][] game, int x, int y, List<Integer> numbers) {
    while (numbers.size() > 0) {
        int number = numbers.remove(0);
        if (isPossibleX(game, y, number) && isPossibleY(game, x, number) && isPossibleBlock(game, x, y, number))
            return number;
    }
    return -1;
}

private int[][] generateSolution(int[][] game, int index) {
    if (index > 80)
        return game;

    int x = index % 9;
    int y = index / 9;

    List<Integer> numbers = new ArrayList<Integer>();
    for (int i = 1; i <= 9; i++) numbers.add(i);
    Collections.shuffle(numbers);

    while (numbers.size() > 0) {
        int number = getNextPossibleNumber(game, x, y, numbers);
        if (number == -1)
            return null;

        game[y][x] = number;
        int[][] tmpGame = generateSolution(game, index + 1);
        if (tmpGame != null)
            return tmpGame;
        game[y][x] = 0;
    }

    return null;
}

private int[][] generateGame(int[][] game) {
    List<Integer> positions = new ArrayList<Integer>();
    for (int i = 0; i < 81; i++)
        positions.add(i);
    Collections.shuffle(positions);
    return generateGame(game, positions);
}

private int[][] generateGame(int[][] game, List<Integer> positions) {
    while (positions.size() > 0) {
        int position = positions.remove(0);
        int x = position % 9;
        int y = position / 9;
        int temp = game[y][x];
        game[y][x] = 0;

        if (!isValid(game))
            game[y][x] = temp;
    }

    return game;
}

private boolean isValid(int[][] game) {
    return isValid(game, 0, new int[] { 0 });
}

private boolean isValid(int[][] game, int index, int[] numberOfSolutions) {
    if (index > 80)
        return ++numberOfSolutions[0] == 1;

    int x = index % 9;
    int y = index / 9;

    if (game[y][x] == 0) {
        List<Integer> numbers = new ArrayList<Integer>();
        for (int i = 1; i <= 9; i++)
            numbers.add(i);

        while (numbers.size() > 0) {
            int number = getNextPossibleNumber(game, x, y, numbers);
            if (number == -1)
                break;
            game[y][x] = number;

            if (!isValid(game, index + 1, numberOfSolutions)) {
                game[y][x] = 0;
                return false;
            }
            game[y][x] = 0;
        }
    } else if (!isValid(game, index + 1, numberOfSolutions))
        return false;

    return true;
}

private int[][] copy(int[][] game) {
    int[][] copy = new int[9][9];
    for (int y = 0; y < 9; y++) {
        for (int x = 0; x < 9; x++)
            copy[y][x] = game[y][x];
    }
    return copy;
}
}
package sudoku.model;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.List;
导入java.util.Observable;
可观察的公共类博弈{
私有int[][]解决方案;
私人int[][]游戏;
私有布尔[][]检查;
私人整数选择号码;
私人布尔帮助;
公共游戏(){
新游戏();
检查=新布尔值[9][9];
帮助=真实;
}
公共游戏{
解决方案=生成解决方案(新整数[9][9],0);
游戏=生成游戏(复制(解决方案));
setChanged();
notifyObservers(UpdateAction.NEW_GAME);
}
公开作废支票游戏(){
selectedNumber=0;
对于(int y=0;y<9;y++){
对于(int x=0;x<9;x++)
检查[y][x]=游戏[y][x]==解决方案[y][x];
}
setChanged();
NotifyObserver(UpdateAction.CHECK);
}
公共void setHelp(布尔帮助){
this.help=帮助;
setChanged();
notifyObservers(UpdateAction.HELP);
}
public void setSelectedNumber(int selectedNumber){
this.selectedNumber=selectedNumber;
setChanged();
NotifyObservators(UpdateAction.SELECTED_NUMBER);
}
public int getSelectedNumber(){
返回所选号码;
}
公共布尔isHelp(){
回报帮助;
}
公共布尔值isSelectedNumberCandidate(整数x,整数y){
返回游戏[y][x]==0&&isPossibleX(游戏,y,所选号码)
&&isPossibleY(游戏,x,所选号码)和&isPossibleBlock(游戏,x,y,所选号码);
}
公共无效集合编号(整数x、整数y、整数编号){
游戏[y][x]=数字;
}
公共整数getNumber(整数x,整数y){
返回游戏[y][x];
}
公共布尔值isCheckValid(int x,int y){
退货支票[y][x];
}
私有布尔值isPossibleX(int[][]游戏,int y,int编号){
对于(int x=0;x<9;x++){
如果(游戏[y][x]==数字)
返回false;
}
返回true;
}
私有布尔值isPossibleY(int[][]游戏,int x,int编号){
对于(int y=0;y<9;y++){
如果(游戏[y][x]==数字)
返回false;
}
返回true;
}
私有布尔isPossibleBlock(int[][]游戏,int x,int y,int number){
intx1=x<3?0:x<6?3:6;
inty1=y<3?0:y<6?3:6;
对于(int-yy=y1;yy0){
整数=数字。删除(0);
if(isPossibleX(游戏,y,数字)和isPossibleY(游戏,x,数字)和isPossibleBlock(游戏,x,y,数字))
返回号码;
}
返回-1;
}
私有int[][]生成解决方案(int[][]游戏,int索引){
如果(索引>80)
回归游戏;
int x=索引%9;
int y=指数/9;
列表编号=新的ArrayList();
对于(int i=1;i 0){
int number=getnextPossiblenNumber(游戏,x,y,数字);
如果(数字==-1)
返回null;
游戏[y][x]=数字;
int[][]tmpGame=生成解决方案(游戏,索引+1);
如果(tmpGame!=null)
返回tmpGame;
博弈[y][x]=0;
}
返回null;
}
私有int[][]生成游戏(int[][]游戏){
列表位置=新的ArrayList();
对于(int i=0;i<81;i++)
增加(i);
集合。洗牌(位置);
返回生成器名称(游戏、位置);
}
私有int[][]生成游戏(int[][]游戏,列表位置){
while(positions.size()>0){
int位置=位置。移除(0);
int x=位置%9;
int y=位置/9;
int temp=游戏[y][x];
博弈[y][x]=0;
如果(!isValid(游戏))
游戏[y][x]=临时;
}
回归游戏;
}
私有布尔值有效(int[][]游戏){
返回值是有效的(游戏,0,新int[]{0});
}
私有布尔值有效(int[][]游戏,int索引,int[]numberOfSolutions){
如果(索引>80)
return++numberOfSolutions[0]==1;
int x=索引%9;
int y=指数/9;
如果(博弈[y][x]==0){
列表编号=新的ArrayList();
对于(int i=1;i 0){
int number=getnextPossiblenNumber(游戏,x,y,数字);
如果(数字==-1)
打破
游戏[y][x]=数字;
如果(!isValid(游戏,索引+1,numberOfSolutions)){
博弈[y][x]=0;
返回false;
}
博弈[y][x]=0;
}
}如果(!isValid(游戏,索引+1,numberOfSolutions))则为else
返回false;
返回true;
}
私有int[][]复制(int[][]游戏){
int[][]复制=新int[9][9];
对于(int y=0;y<9;y++){
对于(int x=0;x<9;x++)
复制[y][x]=游戏[y][x];
}
返回副本;
}
}
当我在菜单栏中单击“新建”时(它的操作在主类中),我想要newGame();要执行的游戏类的方法。我试过很多方法,但都失败了。。真让人头痛


有什么想法吗?

在构造函数中,您创建了游戏对象的一个实例,但它在方法末尾超出了范围。您应该在数独类中保留一个引用:

public class Sudoku extends JFrame {
    ...
    private Game game;

    public Sudoku() {
        ...
        game = new Game();
然后:

添加一个“游戏”对象作为主类的成员类,然后您可以在类中的任何位置访问其方法:

public class Sudoku extends JFrame {
     private Game game;
     // rest of your Sudoku class code as it currently is, but change:
     Game game = new Game();
     // to:
     this.game = new Game();
     // In your 'action' method, you can now call like this:
     this.game.newGame();

我很抱歉没有选择正确的答案,但我先用了杰森的方法,效果很好。你也是对的,但遗憾的是,我只能选择一个正确答案=/几秒钟后就被忍者击倒了,看起来是:P
public class Sudoku extends JFrame {
     private Game game;
     // rest of your Sudoku class code as it currently is, but change:
     Game game = new Game();
     // to:
     this.game = new Game();
     // In your 'action' method, you can now call like this:
     this.game.newGame();