Java GUI graphical user interface arithmetic testing software

Source: Internet
Author: User

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;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.