標籤:計算機 public import 文字框 java
下個周六又要參加自考實踐上機考試了,時間過的好快,天冷了人也變懶惰了,有時候什麼也不想幹,今晚剛好有時間就抽空把JAVA的試題拿出來再複習複習,看書比較睏乏索性就敲敲代碼吧,說實話我對JAVA不是很熟,如果不是因為考試要考,我也沒時間接觸它,畢竟做營運的,我更喜歡shell,PYTHON之類的。算了,還是把剛敲的代碼放這裡儲存下,省的以後又找不到了。剛入門也就這樣了。
題目:
編寫一個計算機類比程式。介面採用4行3列布局,介面設有3個文字標籤(運算數1、運算數2、計算結果)、3個文字框和3個加、減、乘按鈕,3個文字標籤分別位於3個文字框的左邊,標籤上的文字是它右邊文字框作用的說明。前兩個文字框用於輸入運算數1和運算數2,第三個文字框用於顯示計算結果。點擊相應按鈕後,程式從前兩個文字框中讀入運算數,將結果輸出在第3個文字框中
結果如
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="計算機.jpg" alt="wKioL1gjHKqioBKeAAAs0pKXeGw992.jpg-wh_50" />
下面是代碼:
import java.util.*;import java.applet.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;public 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("運算子1");label2 = new JLabel("運算子2");label3 = new JLabel("運算結果");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);}}}
本文出自 “明鏡亦非台” 部落格,請務必保留此出處http://kk876435928.blog.51cto.com/3530246/1871237
Java寫的一個計算機類比小程式