我正在用 Java 创建一个有趣的游戏,我正在尝试决定如何为 GUI 组织我的类。到目前为止,所有只有摆动组件和布局(没有逻辑)的类都在一个名为“ui”的包中。我现在需要向组件(即按钮)添加侦听器(即 ActionListener)。侦听器需要与 Game 类进行通信。
目前我有: Game.java - 创建框架添加面板
import javax.swing.*;
import ui.*;
public class Game {
private JFrame frame;
Main main;
Rules rules;
Game() {
rules = new Rules();
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main = new Main();
frame.setContentPane(main.getContentPane());
show();
}
void show() {
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) { new Game(); }
}
Rules.java - 游戏逻辑
ui 包 - 所有类都创建新面板以与主框架的内容窗格交换 Main.java(主菜单) - 创建一个带有组件的面板
我现在在哪里放置 Main 类的功能?在游戏课上?单独上课?还是整个组织都错了?
谢谢