我正在使用 java 制作一个简单的 java 反应游戏。Display 是我的初始化 gui 的类:
公共类 Display 扩展 JFrame 实现 Gui{
//Connect gui to controller
//(This method will be called before ANY other methods)
public void connect(Controller controller){
}
//Initialise the gui
public Display(){
JPanel panel = new JPanel();
JButton coin = new JButton();
JButton goStop = new JButton();
JLabel prompt = new JLabel("Insert Coin", JLabel.CENTER);
setTitle("Reaction Game");
setContentPane(panel);
coin.setIcon(new ImageIcon("coin.png"));
goStop.setIcon(new ImageIcon("GoButton.png"));
//setting layout of panel
panel.setLayout(new BorderLayout(100, 20));
//adding buttons to panel
panel.add(prompt, BorderLayout.PAGE_START);
panel.add(coin, BorderLayout.LINE_START);
panel.add(goStop, BorderLayout.LINE_END);
setSize(400,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
按钮的实际作用是在 2 个不同的类中实现的,Controller1 和 Controller2(它们实现了一个控制器类),每个类都根据我们决定使用哪一个来做不同的事情。
控制器1:
公共类 Controller1 实现 Controller{
//Creating a constructor
public Controller1(){
}
//Connect controller to gui
//(This method will be called before ANY other methods)
public void connect(Gui gui, Random rng){
}
}
控制器2:
公共类 Controller2 实现 Controller{
//Creating a constructor
public Controller2(){
}
//Connect controller to gui
//(This method will be called before ANY other methods)
public void connect(Gui gui, Random rng){
}
}
我只是想知道如何连接 gui 和控制器。