The next Saturday to take part in test practice on the machine exam, time too fast, cold days people also become lazy, and sometimes do not want to do, tonight just have time to take out the Java test Questions to review, read more sleepy simply knock code it, to tell the truth I am not very familiar with Java, If it is not for the exam to test, I do not have time to contact it, after all, to do operations, I prefer Shell,python or the like. Forget it, or just knock the code here to save, the province will not find the later. Just getting started is the way it is.
Topic:
Write a calculator simulation program. The interface uses 4 rows and 3 columns, the interface has 3 text labels (operand 1, operand 2, calculation results), 3 text boxes and 3 Add, subtract, multiply buttons, 3 text labels are on the left side of 3 text boxes, and the text on the label is the caption of the text box on its right. The first two text boxes are used for input operand 1 and operand 2, and the third text box is used to display the calculation results. After clicking the button, the program reads the operand from the previous two text boxes and outputs the result in the 3rd text box
Results such as
650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/89/FB/wKioL1gjHKqioBKeAAAs0pKXeGw992.jpg-wh_500x0-wm_3 -wmp_4-s_3365194058.jpg "title=" calculator. jpg "alt=" wkiol1gjhkqiobkeaaas0pkxegw992.jpg-wh_50 "/>
Here's the code:
Import java.util.*;import java.applet.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;p ublic class example7_2 extends applet implements actionlistener{jtextfield texta,textb,textc; Jbutton b1,b2,b3;public void init () {setSize (250,150); Jlabel label1,label2,label3;texta = new jtextfield (); Textb = new jtextfield (); Textc = new jtextfield (); B1 = new jbutton ("+");b2 = new JButton ("-"); B3 = new jbutton ("*"); Label1 = new jlabel ("operator 1"); label2 = new jlabel ("Operator 2"), Label3 = new jlabel ("Operation result"), SetLayout (New gridlayout (4,3)); Add (Label1), add (texta), add (B1), add (Label2), add (TEXTB), add (B2), add (LABEL3), add (TEXTC); add (B3); B1.addactionlistener (This), B2.addactionlistener (This), B3.addactionlistener (this); setvisible (true); Public void actionperformed (actionevent e) {if (E.getsource () == b1) {string s1 = texta.gettext (); String s2 = textb.gettext (); Int num1 = integer.parseint (S1); int num2 = integer.parseint (S2); Int sum = num1 + num2;textc.settext ("" +sum); if (E.getsource () == b2) {string s1 = texta.gettext (); String s2 = textb.gettext (); Int num1 = integer.parseint (S1); int num2 = integer.parseint (S2); Int minus = num1 - num2;textc.settext ("" +minus); if (E.getsource () == b3) {string s1 = texta.gettext (); String s2 = textb.gettext (); Int num1 = integer.parseint (S1); int num2 = integer.parseint (S2); Int mult = num1 * num2;textc.settext ("" +mult);}}
This article is from the "mirror is not Taiwan" blog, please be sure to keep this source http://kk876435928.blog.51cto.com/3530246/1871237
Java written by a calculator simulation small program