私はモデルビューコントローラアルゴリズムでaを作成しています。私のコントローラクラスでJButtonのインスタンスが定義されている場合たとえば、リセット、終了、元に戻す、やり直し、および設定のためのボタンがあります。リセットボタンJava
私はactionListenersを実装しました。私のControllerクラスでは、actionPerformed()メソッドの場所です。私は5つのボタンすべてを実装します。現在、ボタンは有効に設定されています。しかし、やり直しと元に戻すことができなくなり、リセットボタンを押すと、それらは無効のままです。やり直しボタンと元に戻すボタンの状態をリセットするにはどうしたらいいですか?
これは、具体的にactionPerformed私GameControllerクラスです:
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof DotButton) {
try {
undo.push((GameModel) gameModel.clone());
} catch (CloneNotSupportedException g) {
g.printStackTrace();
}
if(gameModel.getNumberOfSteps()==0){
int row = ((DotButton)(e.getSource())).getRow();
int column = ((DotButton)(e.getSource())).getColumn();
gameModel.capture(row,column);
gameModel.step();
}
selectColor(((DotButton)(e.getSource())).getColor());
} else if (e.getSource() instanceof JButton) {
JButton clicked = (JButton)(e.getSource());
if (clicked.getText().equals("Quit")) {
/*try {
serialization.serialize(gameModel, "savedGame.ser");
} catch (IOException f) {
f.printStackTrace();
}*/
System.exit(0);
} else if (clicked.getText().equals("Reset")){
reset();
if (clicked.getText().equals("Redo")) {
clicked.setEnabled(true);
}
if (clicked.getText().equals("Undo")) {
clicked.setEnabled(true);
}
} else if (clicked.getText().equals("Redo")) {
if (redo.isEmpty()) {
clicked.setEnabled(false);
}
try {
redo();
} catch (CloneNotSupportedException g) {
g.printStackTrace();
}
}
else if (clicked.getText().equals("Undo")) {
if (undo.isEmpty()) {
clicked.setEnabled(false);
}
try {
undo();
} catch (CloneNotSupportedException g) {
g.printStackTrace();
}
}
else if (clicked.getText().equals("Settings")) {
gameView.settingsMenu();
}
} else if (e.getSource() instanceof JRadioButton) {
JRadioButton clickedR = (JRadioButton)(e.getSource());
if (clickedR.getText().equals("Torus")) {
setting1 = true;
}
if (clickedR.getText().equals("Diagonal")) {
setting2 = true;
}
}
これは、ボタンを作成し、私のGameViewクラスです:
public GameView(GameModel model, GameController gameController){
super("Flood it -- the ITI 1121 version");
this.gameModel = model;
this.gameController = gameController;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(Color.WHITE);
JPanel panel = new JPanel();
panel.setBackground(Color.WHITE);
panel.setLayout(new GridLayout(gameModel.getSize(), gameModel.getSize()));
panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 0, 20));
board = new DotButton[gameModel.getSize()][gameModel.getSize()];
for (int row = 0; row < gameModel.getSize(); row++) {
for (int column = 0; column < gameModel.getSize(); column++) {
board[row][column] = new DotButton(row, column, gameModel.getColor(row,column),
(gameModel.getSize() < 26 ? DotButton.MEDIUM_SIZE : DotButton.SMALL_SIZE));
board[row][column].addActionListener(gameController);
panel.add(board[row][column]);
}
}
add(panel, BorderLayout.CENTER);
buttons = new JButton[5];
buttons[0] = new JButton("Reset");
buttons[1] = new JButton("Quit");
buttons[2] = new JButton("Undo");
buttons[3] = new JButton("Redo");
buttons[4] = new JButton("Settings");
for (JButton button : buttons) {
button.setFocusPainted(false);
button.addActionListener(gameController);
}
JPanel settingsPanel = new JPanel();
settingsPanel.setBackground(Color.WHITE);
settingsPanel.add(buttons[2]);
settingsPanel.add(buttons[3]);
settingsPanel.add(buttons[4]);
JPanel control = new JPanel();
control.setBackground(Color.WHITE);
scoreLabel = new JLabel();
control.add(scoreLabel);
control.add(buttons[0]);
control.add(buttons[1]);
add(settingsPanel, BorderLayout.NORTH);
add(control, BorderLayout.SOUTH);
pack();
setVisible(true);
}
は、私は私のGameControllerクラスならびにGameViewでのJButtonのインスタンスを作成する必要があります。
になることはありません
を満たされるために、私はあなたのMVCは少し歪んだと思うことは不可能ですコントローラは特定の契約に従うもの、つまり特定のイベントを生成して特定のデータへのアクセスを提供するUIまたはモデルが実装されている方法を気にするべきではありません。言いましたが、私はボタンと 'ActionListener'の正しい場所が実際にビューにあると主張します。コントローラーとビューの間の接触の機能に不可欠でない限り、コントローラーはボタンがアクティブであるかどうか気にしませんが、モデルは – MadProgrammer
です。そのためには、コントローラーはビューで通知される必要があります状態が変わると、コントローラはモデルに通知し、モデルは何らかの決定を下すべきです。モデルがその特定の情報を気にかけず、ビューをより自立させることができると考えることはおそらく合理的ですが、それはユースケースごとに決定する必要があります – MadProgrammer