我被困在 Java 中的学校工作中,我正在使用 MVC 设计模式构建一个井字游戏。我有三个类(模型、视图、控制器),我正处于最终产品的最后一步。问题是:
我的 JTextFields 不会保留用户输入的值。我想要得到的是用户选择的名称将被存储并能够通过控制器检索回模型。我尝试了不同的方法,向人们寻求帮助并搜索了几个论坛。
我将 Observable 接口与这些类一起使用,并且在控制器中有 ActionEvent。
请看一下,让我知道你的想法!
OBS!正如您将看到的,我尝试了几种不同的方法来解决这个问题,这就是为什么代码看起来像 s*it....
/*View Class */
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.text.ParseException;
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.Border;
public class View extends JFrame implements Observer{
private JLabel _titleLabel, _westLabel, _eastLabel, _southLabel, _centerLabel;
private JPanel titlePanel, eastPanel, westPanel, southPanel, centerPanel, center2Panel, south2Panel;
private BufferedImage[] images;
private JButton _startButton;
private Model _model;
private Controller _controller;
public JTextField _player1, _player2;
private JButton[] button = new JButton[9];
private String path;
public String _player1Name, _player2Name;
// The title window (NORTH)
public View(Model aModel, Controller aController) {
_model = aModel;
_controller = aController;
for (int i = 0 ; i<9; i++)
button[i] = new JButton();
// pre for path: Requires that pictures are in the same map as the core folder.
path = "./";
// Creating the main background for the game.
this.setContentPane(new JLabel (new ImageIcon(path + "GameFrame.png")));
this.setLayout(new BorderLayout());
// Adding all the initiated panels for the main BorderLayout.
this.add(titlePanel, BorderLayout.NORTH);
this.add(westPanel, BorderLayout.WEST);
this.add(eastPanel, BorderLayout.EAST);
this.add(southPanel, BorderLayout.SOUTH);
this.add(centerPanel, BorderLayout.CENTER);
// Setup for the main frame/game.
this.setTitle("Tic Tac Toe Game");
this.setSize(600,600);
this.setVisible(true);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setResizable(false);
}
public void update(Observable aModel, Object arg) {
if (_model.getStart()) {
enterName();
if (_model.getGameStart()) {
System.out.println("Booting up");
System.out.println(getField1() + getField2());
_controller.setNames(getField1(), getField2());
}
}
}
public void enterName(){
//A form to enter the names of the players
centerPanel.remove(center2Panel);
validate();
center2Panel = new JPanel ();
centerPanel.add(center2Panel);
center2Panel.setOpaque(false);
// Copied code, works the same way and keeps the same pattern.
center2Panel.setLayout(new GridBagLayout ());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(1, 1, 1, 1);
c.gridwidth = GridBagConstraints.REMAINDER;
center2Panel.add(new JLabel("First enter your names: "), c);
_player1 = new JTextField("Skriv namn");
_player1.setColumns(20);
_player2 = new JTextField(_model.getPlayerName(2));
_player2.setColumns(20);
//_player2.setText(getField1());
center2Panel.add(_player1, c);
center2Panel.add(_player2, c);
_startButton = new JButton ("Start Game");
center2Panel.add(_startButton, c);
_startButton.addActionListener(_controller);
_startButton.setActionCommand("start_button");
validate();
_player1Name = _player1.getText();
}
public String getField1(){
System.out.println(_player1Name);
return _player1.getText();
}
public String getField2(){
return _player2.getText();
}
}
/* Controller class */
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.event.MouseInputAdapter;
public class Controller extends MouseInputAdapter implements ActionListener{
Model _model;
public Controller(Model amodel) {
_model = amodel;
}
public void setNames (String One, String Two) {
System.out.println(One+Two);
_model.setPlayerNames(One, Two);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("start")){
_model.isStart();
}
if (e.getActionCommand().equals("start_button")){
System.out.println("startbutton");
_model.namesEntred();
_model.isGameStart();
}
if (e.getActionCommand().equals("exit")){
System.exit(0);
}
}
}
/* Model Class */
import java.util.Observable;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
public class Model extends Observable {
private String _player1Name, _player2Name;
private int _player1Score, _player2Score, _player = 1, _turns;
private String[] _gridList;
private boolean _namesEntered;
private boolean _tie, _win, _victory, _start, _startButton;
private View _view;
public Model(){
_namesEntered = false;
_gridList = new String[9];
_player1Name = "Player1";
_player2Name = "Player2";
_tie = false;
_win = false;
_victory = false;
_start = false;
_startButton = false;
}
public String getPlayerName(int player){
if(player == 1)
return _player1Name;
else
return _player2Name;
}
public void setPlayerNames(String One, String Two) {
_player1Name = One;
_player2Name = Two;
System.out.println(_player1Name + _player2Name);
}
public void newTurn(){
_turns = _turns + 1;
if(_player == 1)
_player = 2;
else
_player = 1;
}
public int getTurn(){
return _player;
}
public String getPlayer () {
if (_player == 1) {
return _player1Name;
}
else {
return _player2Name;
}
}
public String getGridBrick(int i){
return _gridList[i];
}
public static void main(String[] args) {
Model _model = new Model();
Controller _controller = new Controller(_model);
View _view = new View(_model, _controller);
_model.addObserver(_view);
}
public void isStart () {
_start = true;
this.setChanged();
this.notifyObservers();
System.out.println("isStart");
}
public boolean getStart () {
System.out.println("getStart");
return _start;
}
public void isGameStart () {
_startButton = true;
this.setChanged();
this.notifyObservers();
}
public boolean getGameStart () {
return _startButton;
}
public void namesEntred() {
_namesEntered = true;
this.setChanged();
this.notifyObservers();
}
public boolean getNames () {
return _namesEntered;
}
}