我正在做第一个包含GUI的Java项目。我知道程序的逻辑应该与程序的视图(GUI)分开。我的问题是,在游戏逻辑发生某些变化之后,我不了解如何更新GUI,而没有对其进行两次编码,一次在GUI中,一次在逻辑中。
这是我尝试执行的操作的非常简化的示例:
逻辑类:
public class Logic {
private int importantVariable;
public Logic(int importantVariable){
this.importantVariable = importantVariable;
}
public int getImportantVariable() {
return importantVariable;
}
public void increaseImportantVariable() {
this.importantVariable++;
}
}
GUI的类
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GUI extends JFrame {
private Logic logic;
public GUI(Logic logic){
this.logic = logic;
this.setTitle("GUI");
this.setSize(1200, 800);
this.setLocationRelativeTo(null);//set the location to the middle of the display
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel guiPanel = new JPanel();
//create label
JLabel showVariableLabel = new JLabel(String.valueOf(logic.getImportantVariable()));
guiPanel.add(showVariableLabel);
//create Button
JButton button = new JButton();
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
logic.increaseImportantVariable();
}
});
guiPanel.add(button);
this.add(guiPanel);
}
}
主类:
public class Main{
public static void main(String[] args){
Logic logic = new Logic(0);
GUI gui = new GUI(logic);
gui.setVisible(true);
}
}
在此示例中,GUI具有一个按钮和一个显示数字的标签。如果按下该按钮,数字将增加。但是,如何使标签显示增加的数字?我认为更改ActionListener来更新JLabel和逻辑是不好的,因为该程序几乎已经知道它必须知道的一切:逻辑如何工作以及如何显示逻辑。这意味着我的感觉是将ActionListener实现为
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
logic.increaseImportantVariable();
showVariableLabel.setText(String.valueOf(logic.getImportantVariable()));
}
});
即使可行,这也不是最好的方法(我在这里错了吗?)。我认为这是因为真实的项目会更加复杂,并且逻辑中可能会更改许多变量,从而使更新整个GUI而不是仅更改已更改的组件变得更加容易。
因此,我寻找的只是更改逻辑并不断更新整个GUI(它知道如何显示整个逻辑),或者在逻辑中发生更改时调用gui.update()之类的东西来更新整个逻辑GUI。但是对于这两种方式,我都不知道该怎么做。我希望我的问题是可以理解的,并且在思考如何以最佳方式分离逻辑和GUI方面没有大的错误。
要收听模型更改,请引入一个侦听器界面:
interface ModelObserver {
void modelChanged();
}
修改Logic
,使其可以接受侦听器并使用它:
class Logic {
ModelObserver observer;
private int importantVariable;
public Logic(int importantVariable, ModelObserver observer){
this.importantVariable = importantVariable;
this.observer = observer;
}
public int getImportantVariable() {
return importantVariable;
}
public void increaseImportantVariable() {
importantVariable++;
observer.modelChanged();
}
}
Main
类充当控制器,因此让其将观察者添加到Model
并响应模型更改:
public class Main implements ModelObserver{
private final GUI gui;
Main(){
Logic logic = new Logic(0, this);
gui = new GUI(logic);
gui.setVisible(true);
}
@Override
public void modelChanged() {
gui.refresh();
}
public static void main(String[] args){
new Main();
}
}
将所有内容放在一起(以使整个代码可以复制并粘贴到Main.java
中并运行):
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class Main implements ModelObserver{
private final GUI gui;
Main(){
Logic logic = new Logic(0, this);
gui = new GUI(logic);
gui.setVisible(true);
}
@Override
public void modelChanged() {
gui.refresh();
}
public static void main(String[] args){
new Main();
}
}
interface ModelObserver {
void modelChanged();
}
class GUI extends JFrame {
private final Logic logic;
private final JLabel showVariableLabel;
public GUI(Logic logic){
this.logic = logic;
this.setTitle("GUI");
this.setLocationRelativeTo(null);//set the location to the middle of the display
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel guiPanel = new JPanel();
//create label
showVariableLabel = new JLabel(String.valueOf(logic.getImportantVariable()));
guiPanel.add(showVariableLabel);
//create Button
JButton button = new JButton("Increment");
button.addActionListener(e -> logic.increaseImportantVariable());
guiPanel.add(button);
this.add(guiPanel);
pack();
}
public void refresh() {
showVariableLabel.setText(String.valueOf(logic.getImportantVariable()));
}
}
class Logic {
ModelObserver observer;
private int importantVariable;
public Logic(int importantVariable, ModelObserver observer){
this.importantVariable = importantVariable;
this.observer = observer;
}
public int getImportantVariable() {
return importantVariable;
}
public void increaseImportantVariable() {
importantVariable++;
observer.modelChanged();
}
}