在文本字段上显示的MVC java随机数

时间:2017-06-15 07:21:33

标签: java model-view-controller

我是C.S.S.E.本科学生,我正面临与学生项目管理应用程序相关的Java MVC设计中的一些问题。我必须在学生ID文本字段上显示一些自动生成的(我猜的随机数)。

StudentAddView.java不显示并返回TextField上的随机数。

public class StudentAddView extends View{

  private JTextField studentNumberField = new JTextField("", SIZE);
  }

  public StudentAddView(Model StudentList, Controller StudentController){

    super(StudentList,StudentController);

    add(studentNumberField);
  }

然后对于模型下的student.java,它包含方法。

public class Student{
  public int studentNumber;

student.java下的构造函数

public void setstudentNumber(int studentNumber)//setter {

  Random random = new Random();

  studentNumber = random.nextInt(1000);

  this.studentNumber = studentNumber;
}

public int getstudentNumber()//getter{

  return studentNumber;}

最后,student.java中的stringbuffer

public String StringBuffer (String str){

  str = "student[";

  str = str+"student number"= +studentNumber+","; }

还是因为我必须创建一个类来返回随机数并存储为数据以返回文本字段中的值?

1 个答案:

答案 0 :(得分:0)

You're supposed to update your registered view(s) after changing a value in the model, i advise you to use a model observer and interfaces as it will make things clearer for real cases in the future. I strongly advise you to read some example of application of this pattern before trying to use it. It will make it way easier to implement and visualize its purpose and pros. However i won't do it here, as smarter and better people have already done it elsewhere, just google it. (here is a practical example for starters https://www.tutorialspoint.com/design_pattern/mvc_pattern.htm)

Good luck in the great world of patterns !