糯米文學吧

位置:首頁 > 計算機 > java語言

java編譯計算器

java語言2.26W

 這個計算器的界面模仿Windows自帶的簡易計算器。

java編譯計算器

  思路

不考慮連續計算(如1+2+1)的情況,只是兩個數值進行基本運算

1、定義三個變量

2、將第一次按鈕的值賦值個第一個變量

3、將第二個按鈕的值(也就是加減乘除)賦值給第二個變量

4、將第三個按鈕的值賦值給第三個變量

5、用case或者if判斷第二個變量,判斷是那種計算,比如是+,就直接將第一個和第三個變量相加

6、將得到的結果顯示

  代碼:

import erLayout;

import r;

import Layout;

import onEvent;

import onListener;

import eIcon;

import ton;

import me;

import el;

import tField;

import gConstants;

public class Jisuanqi extends JFrame implements ActionListener {

/**

*

*/

private static final long serialVersionUID = 1L;

Result result = new Result(); // 定義text的面板

Number_Key number_key = new Number_Key(); // 定義按鈕面板

// 當點擊按鈕+、-、*、/時,com = true

boolean com = false;

// 當i=0時説明是我們第一次輸入,字符串text不會累加

int i = 0;

// 存放text的內容

String text = "";

// 存放點擊按鈕+、-、*、/之前的數值

double defbutton = 0;

// +、-、*、/的代號分別為1,2,3,4

int symbol = 0;

// 構造函數

Jisuanqi() {

super("WangJiao"); // 設定標題

setDefaultCloseOperation(_ON_CLOSE); // 設定關閉窗體時退出程序

JPanel pane = new JPanel(); // 定義主面板

ayout(new BorderLayout());

setBounds(380, 220, 30, 80); // 前兩個參數是在屏幕上顯示的座標,後兩個是大小

// 替換圖標

ImageIcon icon = new ImageIcon("F:1.GIF");

// Jisuanqi.class.getResource("APPLE.GIF")

// );

setIconImage(mage());

(result, H);

(number_key, ER);

(number_l, H);

number_ctionListener(this); // 對1按鈕添加監聽事件

number_ctionListener(this); // 對2按鈕添加監聽事件

number_ctionListener(this); // 對3按鈕添加監聽事件

number_ctionListener(this); // 對4按鈕添加監聽事件

number_ctionListener(this); // 對5按鈕添加監聽事件

number_ctionListener(this); // 對6按鈕添加監聽事件

number_ctionListener(this); // 對7按鈕添加監聽事件

number_ctionListener(this); // 對8按鈕添加監聽事件

number_ctionListener(this); // 對9按鈕添加監聽事件

number_ctionListener(this); // 對0按鈕添加監聽事件

number_ctionListener(this); // 對置零按鈕添加監聽事件

number_ctionListener(this); // 對+按鈕添加監聽事件

number_ctionListener(this); // 對=按鈕添加監聽事件

number_ctionListener(this); // 對-按鈕添加監聽事件

number_ctionListener(this); // 對*按鈕添加監聽事件

number_ctionListener(this); // 對/按鈕添加監聽事件

number_ctionListener(this); // 對.按鈕添加監聽事件

setContentPane(pane);

pack(); // 初始化窗體大小為正好盛放所有按鈕

}

// 各個按鈕觸發的事件

public void actionPerformed(ActionEvent e) {

/*

* 如果是點擊數字按鈕那麼先要判斷是否在此之前點擊了+、-、*、/、=,如果是那麼com=true 如果沒有com=

* false;或者是否點擊數字鍵,如果是i = 1,如果沒有 i = 0;

*/

if (ource() == number_) {

if (com || i == 0) {

ext("1");

com = false;

i = 1;

} else {

text = ext();

ext(text + "1");

}

} else if (ource() == number_) {

if (com || i == 0) {

ext("2");

com = false;

i = 1;

} else {

text = ext();

ext(text + "2");

}

} else if (ource() == number_e) {

if (com || i == 0) {

標籤:JAVA 計算器 編譯