不能使用JTextField中的getText()

时间:2013-04-16 11:53:54

标签: java swing model-view-controller jtextfield

我坚持学习Java,在那里我正在使用MVC设计模式构建TicTacToe游戏。 我有三个类(模型,视图,控制器),我是最终产品的最后一步。问题是:

我的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;

}

}

2 个答案:

答案 0 :(得分:1)

你的代码真的很长,分析它很难......

但我想我可以给出一些概念提示......

您可以将其架构更改为此类

未经过测试

class View extends ... implements ...
{
  private Controller controller =new Controller(this);

  View(){...}

  protected JTextField getTextField0(){return this.jTextField0;}
  protected JTextField getTextField1(){return this.jTextField1;}
}

class Controller implements ... 
{

  private Model model=new Model();

  private View view;
  protected Controller(View view){this.view=view;}

  protected void setNames()
  {
    this.getModel().setNames(this.getView().getTextField0().getText(),this.getView().getTextField1().getText())
  }


  private Model getModel(){return this.model;}
  private View getView(){return this.view;}
}

class Model implements ...
{
  ...

  protected void setNames(String one,String two){...}
}

P.S。我将代码编写为基本概念,因此您必须根据自己的情况进行调整

我希望它能帮到你

答案 1 :(得分:0)

发现了这个问题。这是我身边的一次重大失败。

  • 当一个新视图被制作出来时,旧视图被丢弃了,它也扔掉了我试图触及的“文本”。

在抛弃视图之前制作了getText时问题解决了!...