我正在修改一些现有的 Java 代码以进行分配,但我不知道如何从 ActionListener 中调用现有对象的函数。
“myGame”只会有一个实例。
这是相关代码;
public class myGame extends JFrame {
public myGame() {
//...snip...
statsBar = new JLabel("");
add(statsBar, BorderLayout.SOUTH);
add(new Board(statsBar));
setResizable(false);
setVisible(true);
addMenubar();
}
private void addMenubar() {
JMenuBar menubar = new JMenuBar();
JMenu topMnuGame = new JMenu("File");
JMenuItem mnuSolve = new JMenuItem("Solve");
mnuSolve.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
// freshGame.solveGame();
// this is where I need to call the solveGame function
// for the "freshGame" instance.
solveGame();
}
});
topMnuGame.add(mnuSolve);
menubar.add(topMnuGame);
}
public static void main(String[] args) {
myGame freshGame = new myGame();
}
}
.
public class Board extends JPanel {
public Board(JLabel statsBar) {
this.statsBar = statsBar;
//..snip..
addMouseListener( new gameAdapter() );
}
public void solveGame() {
// .. do stuff with object ..
}
}
所以我的问题是,如何使用“freshGame”实例从“myGame”类中调用“solveGame()”?