The second time I used the mvc Architecture to write GUI java programs, I was very skilled at writing ~~ After three days of reading java, I forgot all the knowledge points.
Code and photo on
Code:
Main. java
public class Main { public static void main(String args[]){ Window window=new Window(); window.setBounds(100, 100, 500, 130); window.setVisible(true); }}
Window. java
Import java. awt. *; import java. awt. event. *; import javax. swing. *; public class Window extends JFrame implements ActionListener {// use the Window for monitoring/*****/private static final long serialVersionUID = 1L; Sum sum Sum; // model Number generateNumber = new Number (); // model JTextField text1, text2, text3; // view JButton getProblem, checkAnswer; // contrller JLabel answer; // view Window () {init (); setVisible (true); setDefa UltCloseOperation (JFrame. EXIT_ON_CLOSE);} void init () {sum = new Sum (); text1 = new JTextField (10); text2 = new JTextField (10); text1.setEditable (false ); text2.setEditable (false); text3 = new JTextField (10); answer = new JLabel ("You have not answered yet! "); GetProblem = new JButton (" obtain question "); getProblem. addActionListener (this); checkAnswer = new JButton ("Confirm answer"); checkAnswer. addActionListener (this); setLayout (new FlowLayout (); add (getProblem); add (text1); add (new JLabel ("+"); add (text2 ); add (new JLabel ("="); add (text3); add (checkAnswer); add (answer);} public void actionreceivmed (ActionEvent e) {if (e. getSource () = checkAnswer) {int a, B, c; a = Integer. parseInt (text1.get Text (); B = Integer. parseInt (text2.getText (); c = Integer. parseInt (text3.getText (); sum. set (a, B); if (c = sum. answer () {answer. setText ("correct"); text3.setText ("") ;}if (c! = Sum. answer () {answer. setText ("Incorrect answer"); text3.setText ("") ;}} if (e. getSource () = getProblem) {text1.setText (String. valueOf (generateNumber. getNumber (); text2.setText (String. valueOf (generateNumber. getNumber (); answer. setText ("You haven't answered yet! ");}}}
Number. java
public class Number { private int a;public int getNumber(){a=(int)(Math.random()*100)+1;return a;}}
Sum. java
public class Sum { private int a,b,c; public void set(int a,int b){ this.a=a; this.b=b; }public int answer(){c=a+b;return c;}}