成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

java代碼寫親戚計(jì)算器 最簡(jiǎn)單java計(jì)算器代碼

如何用JAVA語言編寫計(jì)算器小程序?

具體代碼如下:

鳳城網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),鳳城網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為鳳城1000+提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的鳳城做網(wǎng)站的公司定做!

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

public class Calculator ?extends JFrame implements ActionListener ?{

private JFrame jf;

private JButton[] allButtons;

private JButton clearButton;

private JTextField jtf;

public Calculator() {

//對(duì)圖形組件實(shí)例化

jf=new JFrame("任靜的計(jì)算器1.0:JAVA版");

jf.addWindowListener(new WindowAdapter(){

public void windowClosing(){

System.exit(0);

}

});

allButtons=new JButton[16];

clearButton=new JButton("清除");

jtf=new JTextField(25);

jtf.setEditable(false);

String str="123+456-789*0.=/";

for(int i=0;iallButtons.length;i++){

allButtons[i]=new JButton(str.substring(i,i+1));

}

}

public void init(){

//完成布局

jf.setLayout(new BorderLayout());

JPanel northPanel=new JPanel();

JPanel centerPanel=new JPanel();

JPanel southPanel=new JPanel();

northPanel.setLayout(new FlowLayout());

centerPanel.setLayout(new GridLayout(4,4));

southPanel.setLayout(new FlowLayout());

northPanel.add(jtf);

for(int i=0;i16;i++){

centerPanel.add(allButtons[i]);

}

southPanel.add(clearButton);

jf.add(northPanel,BorderLayout.NORTH);

jf.add(centerPanel,BorderLayout.CENTER);

jf.add(southPanel,BorderLayout.SOUTH);

addEventHandler();

}

//添加事件監(jiān)聽

public void addEventHandler(){

jtf.addActionListener(this);

for(int i=0;iallButtons.length;i++){

allButtons[i].addActionListener(this);

}

clearButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

Calculator.this.jtf.setText("");

}

});

}

//事件處理

public void actionPerformed(ActionEvent e) {

//在這里完成事件處理 ?使計(jì)算器可以運(yùn)行

String action=e.getActionCommand();

if(action=="+"||action=="-"||action=="*"||action=="/"){

}

}

public void setFontAndColor(){

Font f=new Font("宋體",Font.BOLD,24);

jtf.setFont(f);

jtf.setBackground(new Color(0x8f,0xa0,0xfb));

for(int i=0;i16;i++){

allButtons[i].setFont(f);

allButtons[i].setForeground(Color.RED);

}

}

public void showMe(){

init();

setFontAndColor();

jf.pack();

jf.setVisible(true);

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main(String[] args){

new Calculator().showMe();

}

}

JAVA 編寫計(jì)算器 要代碼最簡(jiǎn)單的

學(xué)java的時(shí)候自己編的,很簡(jiǎn)單,能夠連續(xù)輸入計(jì)算式后進(jìn)行計(jì)算

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.Font;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.text.NumberFormat;

import java.util.ArrayList;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextField;

/**簡(jiǎn)易計(jì)算器,能夠進(jìn)行簡(jiǎn)單的計(jì)算

*

* @see 2008.12.9

*/

public class CalculatorA

implements ActionListener{

private JFrame frame;

private JTextField field;

private JButton[] allButtons;

private JButton clearButton;

// private JButton backButton;

String result="";//保存結(jié)果

StringBuilder sb = new StringBuilder();//保存要進(jìn)行的計(jì)算式

int x = 0; //用來判斷上一次的事件類型

String str = "123+456-789*0.=/";

ArrayListString arrayList = new ArrayListString();//保存計(jì)算式,通過方法進(jìn)行運(yùn)算

public CalculatorA(){

frame = new JFrame("我的計(jì)算器v1.1");

frame.setLocation(300,300);

field = new JTextField(25);

allButtons = new JButton[16];

for(int i=0;iallButtons.length;i++){

allButtons[i]= new JButton(str.substring(i,i+1));

}

clearButton = new JButton("CLEAR");

// backButton = new JButton("——");

init();

setFondAndColor();

addEventHander();

}

public void init(){

frame.setLayout(new BorderLayout());

JPanel northPanel = new JPanel();

JPanel centerPanel = new JPanel();

JPanel southPanel = new JPanel();

northPanel.setLayout(new FlowLayout());

centerPanel.setLayout(new GridLayout(4,4));

southPanel.setLayout(new FlowLayout());

northPanel.add(field);

for(int i=0;iallButtons.length;i++){

centerPanel.add(allButtons[i]);

}

southPanel.add(clearButton);

//southPanel.add(backButton);

frame.add(northPanel,BorderLayout.NORTH);

frame.add(centerPanel,BorderLayout.CENTER);

frame.add(southPanel,BorderLayout.SOUTH);

}

//設(shè)置輸入字體

public void setFondAndColor(){

field.setFont(new Font("宋體",Font.BOLD,24));

field.setBackground(new Color(100,200,200));

field.setForeground(Color.RED);

//設(shè)置字體從右起始

field.setHorizontalAlignment(JTextField.RIGHT);

}

public void showMi(){

frame.pack();

frame.setResizable(false);

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public void addEventHander(){

for(int i=0;iallButtons.length;i++){

allButtons[i].addActionListener(this);

}

clearButton.addActionListener(this);

// backButton.addActionListener(this);

}

@Override

public void actionPerformed(ActionEvent e) {

String str = e.getActionCommand();//取得當(dāng)前事件返回的值

if("0123456789.".indexOf(str)!=-1){

if(x == 0){ //當(dāng)x為0時(shí)表示還沒有進(jìn)行輸入

result=str;

sb.append(str);

field.setText(str);

x = 1;

}

else if(x == 1){

result = result +str;

sb.append(str);

field.setText(result);

x = 1;

}

else if(x == 2){

sb.delete(0,sb.length());

result = result+str;

sb.append(str);

field.setText(result);

x = 1;

}

else if(x == 3){

result = str;

sb.delete(0,sb.length());

arrayList.clear();

field.setText(str);

sb.append(str);

field.setText(str);

x = 1;

}

else if(x == 4){

result ="";

sb.delete(0,sb.length());

arrayList.clear();

result = str;

sb.append(str);

field.setText(str);

x = 1;

}

else{

result = result +str;

sb.append(str);

field.setText(result);

x = 1;

}

}

else if("+*-/".indexOf(str)!=-1){

if(x == 0){

field.setText("");

x = 2;

}

else if(x == 1){

result = result + str;

arrayList.add(sb.toString());

arrayList.add(str);

sb.append(str);

field.setText(result);

x = 2;

}

else if(x == 2){

x = 2;

}

else if(x == 3){

field.setText(result+str);

arrayList.add(result);

arrayList.add(str);

result = result+str;

x = 2;

}

else if(x == 4){

result ="";

sb.delete(0,sb.length());

arrayList.clear();

x = 2;

}

else{

field.setText(result+str);

arrayList.add(result);

arrayList.add(str);

result = result+str;

x = 2;

}

}

else if("=".equals(str)){

if(x == 0){

field.setText("0");

arrayList.clear();

result = "0";

x = 3;

}

else if(x == 1){

try{

arrayList.add(sb.toString());

arrayList = getResult(arrayList);

result = arrayList.get(0);

field.setText(result);

arrayList.clear();

x = 3;

}catch(Exception e1){

field.setText("數(shù)據(jù)格式異常");

x = 0;

}

}

else if(x == 2){

field.setText("數(shù)據(jù)格式錯(cuò)誤.....");

arrayList.clear();

x = 0;

}

else if(x == 3){

field.setText(result);

x = 3;

}

else if(x == 4){

result ="";

sb.delete(0,sb.length());

arrayList.clear();

x = 3;

}

else {

try{

arrayList.add(sb.toString());

arrayList = getResult(arrayList);

result = arrayList.get(0);

field.setText(result);

arrayList.clear();

x = 3;

}catch(Exception e1){

field.setText("數(shù)據(jù)格式異常");

x = 0;

}

}

}

else if("CLEAR".equals(str)){

arrayList.clear();

field.setText("0");

arrayList.add("0");

x = 4;

}

else{

if(result.length()1){

result = result.substring(0,result.length()-1);

if(sb.length()0){

sb.delete(sb.length()-1,sb.length());

}

else {

sb.delete(0,1);

}

field.setText(result);

x = 5;

}

else{

result = "";

sb.delete(0,sb.length());

arrayList.clear();

field.setText("0");

x = 0;

}

}

}

public static ArrayListString getResult(ArrayListString list){

String res = null;

String[] s = {"/","*","-","+"};

int i=0;

if(list.size()1){

for(;is.length;){

if(s[i].equals("/")){

for(int j=0;jlist.size();j++){

if(list.get(j).equals(s[i])){

res = Double.toString(Double.parseDouble(list.get(j-1))/Double.parseDouble(list.get(j+1)));

//本地的數(shù)據(jù)格式

NumberFormat nf = NumberFormat.getInstance();

res = nf.format(Double.parseDouble(res));

res = getChange(res);

list.set(j-1,res);

list.remove(j);

list.remove(j);

getResult(list);

}

}

i++;

}

else if(s[i].equals("*")){

for(int j=0;jlist.size();j++){

if(list.get(j).equals(s[i])){

res = Double.toString(Double.parseDouble(list.get(j-1))*Double.parseDouble(list.get(j+1)));

NumberFormat nf = NumberFormat.getInstance();

res = nf.format(Double.parseDouble(res));

res = getChange(res);

list.set(j-1,res);

list.remove(j);

list.remove(j);

getResult(list);

}

}

i++;

}

else if(s[i].equals("-")){

for(int j=0;jlist.size();j++){

if(list.get(j).equals(s[i])){

res = Double.toString(Double.parseDouble(list.get(j-1))-Double.parseDouble(list.get(j+1)));

NumberFormat nf = NumberFormat.getNumberInstance();

res = nf.format(Double.parseDouble(res));

res = getChange(res);

list.set(j-1,res);

list.remove(j);

list.remove(j);

getResult(list);

}

}

i++;

}

else {

for(int j=0;jlist.size();j++){

if(list.get(j).equals(s[i])){

res = Double.toString(Double.parseDouble(list.get(j-1))+Double.parseDouble(list.get(j+1)));

NumberFormat nf = NumberFormat.getInstance();

res = nf.format(Double.parseDouble(res));

res = getChange(res);

list.set(j-1,res);

list.remove(j);

list.remove(j);

getResult(list);

}

}

i++;

}

}

}

return list;

}

//對(duì)數(shù)字字符串進(jìn)行排除不必要符號(hào)

public static String getChange(String res){

String s_temp = "";

char[] c = new char[res.length()];

for(int k=0;kc.length;k++){

c[k] = res.charAt(k);

}

for(int k=0;kc.length;k++){

if((c[k]= '0' c[k]= '9')|| c[k] == '.'){

s_temp += c[k];

}

}

return s_temp;

}

public static void main(String[] args){

new CalculatorA().showMi();

}

}

親戚稱呼計(jì)算器java代碼

計(jì)算器java代碼

import?java.awt.BorderLayout;

import?java.awt.Dimension;

import?java.awt.GridLayout;

import?java.awt.Toolkit;

import?java.awt.event.ActionEvent;

import?java.awt.event.ActionListener;

import?javax.swing.JButton;

import?javax.swing.JFrame;

import?javax.swing.JPanel;

import?javax.swing.SwingUtilities;

import?javax.swing.UIManager;

class?CalculatorPanel?extends?JPanel?{

private?JPanel?jp;?//?中間的面板

private?double?result;?//?計(jì)算結(jié)果

private?boolean?start;?//?用于判斷是否是首次輸入,true首次,false不是首次

private?boolean?flag;?//?用于判斷是否清空顯示區(qū)域的值,true需要,false不需要

private?String?lastCommand;?//?用于保存運(yùn)算符

JButton?display;?//?顯示區(qū)域

public?CalculatorPanel()?{

//?初始化各項(xiàng)值

result?=?0;

start?=?true;

flag?=?false;

lastCommand?=?"=";

//?設(shè)置CalculatorPanel這個(gè)面板的布局為框架布局

setLayout(new?BorderLayout());

//?這個(gè)按鈕用來顯示值的區(qū)域

display?=?new?JButton("0.0");

display.setEnabled(false);//?按鈕樣式設(shè)置為禁用樣式

//?用來初始化,清除用的

JButton?clear?=?new?JButton("clear");

clear.addActionListener(new?ActionListener()?{

@Override

public?void?actionPerformed(ActionEvent?e)?{

//?作初始化的操作

result?=?0;

start?=?true;

flag?=?false;

lastCommand?=?"=";

display.setText("0.0");

}

});

jp?=?new?JPanel();

//?將此面板布局設(shè)置為網(wǎng)格布局,4行4列

jp.setLayout(new?GridLayout(4,?4));

//?實(shí)例化監(jiān)聽器對(duì)象

NumberAction?na?=?new?NumberAction();

CommandAction?ca?=?new?CommandAction();

makeButton("7",?na);

makeButton("8",?na);

makeButton("9",?na);

makeButton("/",?ca);

makeButton("4",?na);

makeButton("5",?na);

makeButton("6",?na);

makeButton("*",?ca);

makeButton("1",?na);

makeButton("2",?na);

makeButton("3",?na);

makeButton("-",?ca);

makeButton("0",?na);

makeButton(".",?na);

makeButton("+",?ca);

makeButton("=",?ca);

//?將面板,兩個(gè)按鍵加入到我的面板,實(shí)現(xiàn)計(jì)算器的界面

add(display,?BorderLayout.NORTH);

add(jp,?BorderLayout.CENTER);

add(clear,?BorderLayout.SOUTH);

}

private?void?makeButton(String?buttonName,?ActionListener?al)?{

JButton?jb?=?new?JButton(buttonName);

jp.add(jb);

jb.addActionListener(al);

}

//?數(shù)字監(jiān)聽器

private?class?NumberAction?implements?ActionListener?{

@Override

public?void?actionPerformed(ActionEvent?e)?{

JButton?jb?=?(JButton)?e.getSource();?//?獲取事件源對(duì)象

String?input?=?jb.getText();?//?獲取事件源對(duì)象上的標(biāo)簽

if?(start)?{?//?首次輸入

//?一上就".",就什么也不做

if?(input.equals("."))?{

return;

}

//?如果是"0.0"的話,就清空

if?(display.getText().equals("0.0"))?{

display.setText("");

}

start?=?false;//?改變是否首次輸入的標(biāo)記值

}?else?{

//?判斷顯示區(qū)域的值里面是否已經(jīng)有".",如果有,輸入的又是".",就什么都不做

if?(display.getText().indexOf(".")?!=?-1)?{

if?(input.equals("."))?{

return;

}

}

//?判斷顯示區(qū)域的值里面只有"-",輸入的又是".",就什么都不做

if?(display.getText().equals("-"))?{

if?(input.equals("."))?{

return;

}

}

//?判斷顯示區(qū)域的值如果是"0",輸入的不是".",就什么也不做

if?(display.getText().equals("0"))?{

if?(!input.equals("."))?{

return;

}

}

}

//?如果我點(diǎn)擊了運(yùn)算符以后,再輸入數(shù)字的話,就要清空顯示區(qū)域的值

if?(flag)?{

display.setText("");

flag?=?false;//?還原初始值,不需要清空

}

display.setText(display.getText()?+?input);//?設(shè)置顯示區(qū)域的值

}

}

//?運(yùn)算符監(jiān)聽器

private?class?CommandAction?implements?ActionListener?{

@Override

public?void?actionPerformed(ActionEvent?e)?{

JButton?jb?=?(JButton)?e.getSource();?//?獲取事件源對(duì)象

String?inputCommand?=?jb.getText();?//?獲取事件源對(duì)象上的標(biāo)簽

if?(start)?{//?首次輸入

//?一上就是"-"

if?(inputCommand.equals("-"))?{

display.setText("-");//?顯示區(qū)域的內(nèi)容設(shè)置為"-"

start?=?false;//?改變首次輸入的標(biāo)記

}

}?else?{

if?(!flag)?{//?如果flag=false不需要清空顯示區(qū)的值,就調(diào)用方法計(jì)算

calculate(Double.parseDouble(display.getText()));//?保存顯示區(qū)域的值,并計(jì)算

}

//?保存你點(diǎn)擊的運(yùn)算符

lastCommand?=?inputCommand;

flag?=?true;//?因?yàn)槲疫@里已經(jīng)輸入過運(yùn)算符,

}

}

}

//?計(jì)算用的方法

private?void?calculate(double?x)?{

if?(lastCommand.equals("+"))?{

result?+=?x;

}?else?if?(lastCommand.equals("-"))?{

result?-=?x;

}?else?if?(lastCommand.equals("*"))?{

result?*=?x;

}?else?if?(lastCommand.equals("/"))?{

result?/=?x;

}?else?if?(lastCommand.equals("="))?{

result?=?x;

}

display.setText(""?+?result);

}

}

class?MyFrameUitl?{

public?static?void?init(JFrame?jFrame,?Dimension?frameSize,?String?title,

String?iconFileName,?boolean?resizable)?{

Toolkit?tk?=?Toolkit.getDefaultToolkit();?//?系統(tǒng)工具包,可以讀取系統(tǒng)信息

Dimension?screenSize?=?tk.getScreenSize();//?獲取屏幕大小

//?獲取寬高

int?screenWidth?=?screenSize.width;

int?screenHeight?=?screenSize.height;

//?找中心點(diǎn)

int?centerX?=?screenWidth?/?2;

int?centerY?=?screenHeight?/?2;

//?解析窗體大小

int?frameWidth?=?frameSize.width;

int?frameHeight?=?frameSize.height;

//?設(shè)置窗體位置

jFrame.setBounds(centerX?-?frameWidth?/?2,?centerY?-?frameHeight?/?2,

frameWidth,?frameHeight);

jFrame.setTitle(title);//?設(shè)置標(biāo)題

if?(iconFileName?!=?null)?{

jFrame.setIconImage(tk.getImage(iconFileName));//?設(shè)置圖標(biāo)

}

//?設(shè)置皮膚com.sun.java.swing.plaf.windows.WindowsLookAndFeel

/**/

try?{

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

SwingUtilities.updateComponentTreeUI(jFrame);

}?catch?(Exception?e1)?{

e1.printStackTrace();

}

jFrame.setResizable(resizable);//?設(shè)置窗體是否可以改變大小

jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);?//?設(shè)置程序關(guān)閉動(dòng)作

jFrame.setVisible(true);//?顯示窗體

}

}

class?CalculatorFrame?extends?JFrame?{

public?CalculatorFrame()?{

add(new?CalculatorPanel());

MyFrameUitl.init(this,?new?Dimension(400,?300),?"簡(jiǎn)易計(jì)算器",?null,

false);

this.pack();//?讓組件按原始大小顯示,并且窗體的大小剛好能夠容納所有的組件

}

}

public?class?CalculatorTest?{

public?static?void?main(String[]?args)?{

new?CalculatorFrame();

}

}

運(yùn)行效果:

用java實(shí)現(xiàn)一個(gè)簡(jiǎn)單的計(jì)算器。

/*

* @(#)JCalculator.java 1.00 06/17/2015

*/

import?java.awt.*;

import?java.awt.event.*;

import?javax.swing.*;

/**

* A simple calculator program.

*?pI saw this program in a QQ group, and help a friend correct it./p

*

*?@author?Singyuen?Yip

*?@version?1.00 12/29/2009

*?@see?JFrame

*?@see?ActionListener

*/

public?class?JCalculator?extends?JFrame?implements?ActionListener {

/**

* Serial Version UID

*/

private?static?final?long?serialVersionUID?= -169068472193786457L;

/**

* This class help close the Window.

*?@author?Singyuen?Yip

*

*/

private?class?WindowCloser?extends?WindowAdapter {

public?void?windowClosing(WindowEvent we) {

System.exit(0);

}

}

int?i;

// Strings for Digit Operator buttons.

private?final?String[]?str?= {?"7",?"8",?"9",?"/",?"4",?"5",?"6",?"*","1",

"2",?"3",?"-",?".",?"0",?"=",?"+"?};

// Build buttons.

JButton[]?buttons?=?new?JButton[str.length];

// For cancel or reset.

JButton?reset?=?new?JButton("CE");

// Build the text field to show the result.

JTextField?display?=?new?JTextField("0");

/**

* Constructor without parameters.

*/

public?JCalculator() {

super("Calculator");

// Add a panel.

JPanel panel1 =?new?JPanel(new?GridLayout(4, 4));

// panel1.setLayout(new GridLayout(4,4));

for?(i?= 0;?i??str.length;?i++) {

buttons[i] =?new?JButton(str[i]);

panel1.add(buttons[i]);

}

JPanel panel2 =?new?JPanel(new?BorderLayout());

// panel2.setLayout(new BorderLayout());

panel2.add("Center",?display);

panel2.add("East",?reset);

// JPanel panel3 = new Panel();

getContentPane().setLayout(new?BorderLayout());

getContentPane().add("North", panel2);

getContentPane().add("Center", panel1);

// Add action listener for each digit operator button.

for?(i?= 0;?i??str.length;?i++)

buttons[i].addActionListener(this);

// Add listener for "reset" button.

reset.addActionListener(this);

// Add listener for "display" button.

display.addActionListener(this);

// The "close" button "X".

addWindowListener(new?WindowCloser());

// Initialize the window size.

setSize(800, 800);

// Show the window.

// show(); Using show() while JDK version is below 1.5.

setVisible(true);

// Fit the certain size.

pack();

}

public?void?actionPerformed(ActionEvent e) {

Object target = e.getSource();

String label = e.getActionCommand();

if?(target ==?reset)

handleReset();

else?if?("0123456789.".indexOf(label) 0)

handleNumber(label);

else

handleOperator(label);

}

// Is the first digit pressed?

boolean?isFirstDigit?=?true;

/**

* Number handling.

*?@param?key the key of the button.

*/

public?void?handleNumber(String key) {

if?(isFirstDigit)

display.setText(key);

else?if?((key.equals(".")) (display.getText().indexOf(".") 0))

display.setText(display.getText() +?".");

else?if?(!key.equals("."))

display.setText(display.getText() + key);

isFirstDigit?=?false;

}

/**

* Reset the calculator.

*/

public?void?handleReset() {

display.setText("0");

isFirstDigit?=?true;

operator?=?"=";

}

double?number?= 0.0;

String?operator?=?"=";

/**

* Handling the operation.

*?@param?key pressed operator's key.

*/

public?void?handleOperator(String key) {

if?(operator.equals("+"))

number?+= Double.valueOf(display.getText());

else?if?(operator.equals("-"))

number?-= Double.valueOf(display.getText());

else?if?(operator.equals("*"))

number?*= Double.valueOf(display.getText());

else?if?(operator.equals("/"))

number?/= Double.valueOf(display.getText());

else?if?(operator.equals("="))

number?= Double.valueOf(display.getText());

display.setText(String.valueOf(number));

operator?= key;

isFirstDigit?=?true;

}

public?static?void?main(String[] args) {

new?JCalculator();

}

}

運(yùn)行界面:

用java語言,編寫一個(gè)計(jì)算器

有一個(gè)現(xiàn)成的.你看看吧

import java.awt.event.ActionEvent;

public class Application extends JFrame {

protected String str = "";

protected boolean isChar = true;

protected boolean isEqual = false;

protected JTextField textField;

public Application() {

Listener listerner = new Listener(this);

getContentPane().setLayout(null);

JButton button = new JButton("7");

button.addActionListener(listerner);

button.setBounds(12, 69, 43, 27);

getContentPane().add(button);

textField = new JTextField();

textField.setText("0");

textField.setEditable(false);

textField.setHorizontalAlignment(JTextField.RIGHT);

textField.setBounds(12, 22, 377, 27);

getContentPane().add(textField);

textField.setColumns(10);

JButton button_1 = new JButton("8");

button_1.addActionListener(listerner);

button_1.setBounds(103, 69, 43, 27);

getContentPane().add(button_1);

JButton button_2 = new JButton("9");

button_2.addActionListener(listerner);

button_2.setBounds(182, 69, 43, 27);

getContentPane().add(button_2);

JButton button_3 = new JButton("4");

button_3.addActionListener(listerner);

button_3.setBounds(12, 106, 43, 27);

getContentPane().add(button_3);

JButton button_4 = new JButton("5");

button_4.addActionListener(listerner);

button_4.setBounds(103, 106, 43, 27);

getContentPane().add(button_4);

JButton button_5 = new JButton("6");

button_5.addActionListener(listerner);

button_5.setBounds(182, 106, 43, 27);

getContentPane().add(button_5);

JButton button_6 = new JButton("1");

button_6.addActionListener(listerner);

button_6.setBounds(12, 143, 43, 27);

getContentPane().add(button_6);

JButton button_7 = new JButton("2");

button_7.addActionListener(listerner);

button_7.setBounds(103, 143, 43, 27);

getContentPane().add(button_7);

JButton button_8 = new JButton("3");

button_8.addActionListener(listerner);

button_8.setBounds(182, 143, 43, 27);

getContentPane().add(button_8);

JButton button_9 = new JButton("+");

button_9.addActionListener(listerner);

button_9.setBounds(269, 72, 43, 27);

getContentPane().add(button_9);

JButton button_10 = new JButton("-");

button_10.addActionListener(listerner);

button_10.setBounds(346, 72, 43, 27);

getContentPane().add(button_10);

JButton button_11 = new JButton("*");

button_11.addActionListener(listerner);

button_11.setBounds(269, 109, 43, 27);

getContentPane().add(button_11);

JButton button_12 = new JButton("/");

button_12.addActionListener(listerner);

button_12.setBounds(346, 109, 43, 27);

getContentPane().add(button_12);

JButton button_13 = new JButton("=");

button_13.addActionListener(listerner);

button_13.setBounds(346, 143, 43, 27);

getContentPane().add(button_13);

JButton button_14 = new JButton("0");

button_14.addActionListener(listerner);

button_14.setBounds(103, 180, 43, 27);

getContentPane().add(button_14);

JButton btnReset = new JButton("reset");

btnReset.addActionListener(listerner);

btnReset.setBounds(269, 180, 118, 27);

getContentPane().add(btnReset);

JButton button_15 = new JButton(".");

button_15.addActionListener(listerner);

button_15.setBounds(269, 146, 43, 27);

getContentPane().add(button_15);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setResizable(false);

this.setSize(442, 260);

this.setLocationRelativeTo(null);

this.setVisible(true);

}

public static void main(String[] args) {

new Application();

}

}

class Listener implements ActionListener {

private Application app = null;

public Listener(Application app) {

this.app = app;

}

public void actionPerformed(ActionEvent e) {

String value = e.getActionCommand();

if (value.matches("[0-9.]")) {

if (app.isChar) {

app.textField.setText("");

app.isChar = false;

}

if (app.isEqual app.str.matches("[0-9.]*")) {

app.str = "";

app.isEqual = false;

}

app.str += value;

app.textField.setText(app.textField.getText() + value);

} else if (value.matches("[\\+\\-\\*/]")) {

if (!app.str.substring(app.str.length() - 1)

.matches("[\\+\\-\\*/]")) {

app.str += value;

app.isChar = true;

}

} else if ("=".equals(value)) {

app.isEqual = true;

if (app.str.substring(app.str.length() - 1).matches("[\\+\\-]")) {

app.str += "0";

} else if (app.str.substring(app.str.length() - 1)

.matches("[\\*/]")) {

app.str += "1";

}

Interpreter bsh = new Interpreter();

String obj = null;

try {

obj = bsh.eval(app.str).toString();

} catch (Exception exception) {

System.out.println(exception.getMessage());

}

System.out.println(app.str);

app.textField.setText(obj);

app.str = obj;

app.isChar = true;

} else {

app.str = "";

app.textField.setText("0");

}

}

}

用JAVA編寫一個(gè)計(jì)算器

import?java.awt.BorderLayout;

import?java.awt.Color;

import?java.awt.GridLayout;

import?java.awt.event.ActionEvent;

import?java.awt.event.ActionListener;

import?javax.swing.ImageIcon;

import?javax.swing.JButton;

import?javax.swing.JFrame;

import?javax.swing.JPanel;

import?javax.swing.JTextField;

import?javax.swing.SwingConstants;

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();?//?定義按鈕面板

//?當(dāng)點(diǎn)擊按鈕+、-、*、/時(shí),com?=?true

boolean?com?=?false;

//?當(dāng)i=0時(shí)說明是我們第一次輸入,字符串text不會(huì)累加

int?i?=?0;

//?存放text的內(nèi)容

String?text?=?"";

//?存放點(diǎn)擊按鈕+、-、*、/之前的數(shù)值

double?defbutton?=?0;

//?+、-、*、/的代號(hào)分別為1,2,3,4

int?symbol?=?0;

//?構(gòu)造函數(shù)

Jisuanqi()?{

super("WangJiao");?//?設(shè)定標(biāo)題

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);?//?設(shè)定關(guān)閉窗體時(shí)退出程序

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

pane.setLayout(new?BorderLayout());

setBounds(380,?220,?30,?80);?//?前兩個(gè)參數(shù)是在屏幕上顯示的坐標(biāo),后兩個(gè)是大小

//?替換圖標(biāo)

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

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

//?);

setIconImage(icon.getImage());

pane.add(result,?BorderLayout.NORTH);

pane.add(number_key,?BorderLayout.CENTER);

pane.add(number_key.equal,?BorderLayout.SOUTH);

number_key.one.addActionListener(this);?//?對(duì)1按鈕添加監(jiān)聽事件

number_key.two.addActionListener(this);?//?對(duì)2按鈕添加監(jiān)聽事件

number_key.three.addActionListener(this);?//?對(duì)3按鈕添加監(jiān)聽事件

number_key.four.addActionListener(this);?//?對(duì)4按鈕添加監(jiān)聽事件

number_key.five.addActionListener(this);?//?對(duì)5按鈕添加監(jiān)聽事件

number_key.six.addActionListener(this);?//?對(duì)6按鈕添加監(jiān)聽事件

number_key.seven.addActionListener(this);?//?對(duì)7按鈕添加監(jiān)聽事件

number_key.eight.addActionListener(this);?//?對(duì)8按鈕添加監(jiān)聽事件

number_key.nine.addActionListener(this);?//?對(duì)9按鈕添加監(jiān)聽事件

number_key.zero.addActionListener(this);?//?對(duì)0按鈕添加監(jiān)聽事件

number_key.ce.addActionListener(this);?//?對(duì)置零按鈕添加監(jiān)聽事件

number_key.plus.addActionListener(this);?//?對(duì)+按鈕添加監(jiān)聽事件

number_key.equal.addActionListener(this);?//?對(duì)=按鈕添加監(jiān)聽事件

number_key.sub.addActionListener(this);?//?對(duì)-按鈕添加監(jiān)聽事件

number_key.mul.addActionListener(this);?//?對(duì)*按鈕添加監(jiān)聽事件

number_key.div.addActionListener(this);?//?對(duì)/按鈕添加監(jiān)聽事件

number_key.point.addActionListener(this);?//?對(duì).按鈕添加監(jiān)聽事件

setContentPane(pane);

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

}

//?各個(gè)按鈕觸發(fā)的事件

public?void?actionPerformed(ActionEvent?e)?{

/*

?*?如果是點(diǎn)擊數(shù)字按鈕那么先要判斷是否在此之前點(diǎn)擊了+、-、*、/、=,如果是那么com=true?如果沒有com=

?*?false;或者是否點(diǎn)擊數(shù)字鍵,如果是i?=?1,如果沒有?i?=?0;

?*/

if?(e.getSource()?==?number_key.one)?{

if?(com?||?i?==?0)?{

result.text.setText("1");

com?=?false;

i?=?1;

}?else?{

text?=?result.text.getText();

result.text.setText(text?+?"1");

}

}?else?if?(e.getSource()?==?number_key.two)?{

if?(com?||?i?==?0)?{

result.text.setText("2");

com?=?false;

i?=?1;

}?else?{

text?=?result.text.getText();

result.text.setText(text?+?"2");

}

}?else?if?(e.getSource()?==?number_key.three)?{

if?(com?||?i?==?0)?{

result.text.setText("3");

com?=?false;

i?=?1;

}?else?{

text?=?result.text.getText();

result.text.setText(text?+?"3");

}

}?else?if?(e.getSource()?==?number_key.four)?{

if?(com?||?i?==?0)?{

result.text.setText("4");

com?=?false;

i?=?1;

}?else?{

text?=?result.text.getText();

result.text.setText(text?+?"4");

}

}?else?if?(e.getSource()?==?number_key.five)?{

if?(com?||?i?==?0)?{

result.text.setText("5");

com?=?false;

i?=?1;

}?else?{

text?=?result.text.getText();

result.text.setText(text?+?"5");

}

}?else?if?(e.getSource()?==?number_key.six)?{

if?(com?||?i?==?0)?{

result.text.setText("6");

com?=?false;

i?=?1;

}?else?{

text?=?result.text.getText();

result.text.setText(text?+?"6");

}

}?else?if?(e.getSource()?==?number_key.seven)?{

if?(com?||?i?==?0)?{

result.text.setText("7");

com?=?false;

i?=?1;

}?else?{

text?=?result.text.getText();

result.text.setText(text?+?"7");

}

}?else?if?(e.getSource()?==?number_key.eight)?{

if?(com?||?i?==?0)?{

result.text.setText("8");

com?=?false;

i?=?1;

}?else?{

text?=?result.text.getText();

result.text.setText(text?+?"8");

}

}?else?if?(e.getSource()?==?number_key.nine)?{

if?(com?||?i?==?0)?{

result.text.setText("9");

com?=?false;

i?=?1;

}?else?{

text?=?result.text.getText();

result.text.setText(text?+?"9");

}

}

/*

?*?對(duì)于0這個(gè)按鈕有一定的說法,在我的程序里不會(huì)出現(xiàn)如00000這樣的情況,我加了判斷條件就是

?*?如果text中的數(shù)值=0就要判斷在這個(gè)數(shù)值中是否有.存在?如果有那么就在原來數(shù)值基礎(chǔ)之上添?加0;否則保持原來的數(shù)值不變

?*/

else?if?(e.getSource()?==?number_key.zero)?{?//?result.text.getText()是得到text里內(nèi)容的意思

if?(com?||?i?==?0)?{

result.text.setText("0");

com?=?false;

i?=?1;

}?else?{

text?=?result.text.getText();

if?(Float.parseFloat(text)??0?||?Float.parseFloat(text)??0)?{?//?Float.parseFloat(text)就是類型轉(zhuǎn)換了,下面都是一樣

result.text.setText(text?+?"0");

}?else?{

if?(text.trim().indexOf(".")?==?-1)?{

result.text.setText(text);

}?else?{

result.text.setText(text?+?"0");

}

}

}

}?else?if?(e.getSource()?==?number_key.ce)?{

result.text.setText("0");

i?=?0;

com?=?true;

//?text?=?"";

defbutton?=?0;

}

/*

?*?本程序不會(huì)讓一個(gè)數(shù)值中出現(xiàn)2個(gè)以上的小數(shù)點(diǎn).具體做法是:判斷是否已經(jīng)存在.存在就不添加,?不存在就添加.

?*/

else?if?(e.getSource()?==?number_key.point)?{

if?(com?||?i?==?0)?{

result.text.setText("0.");

com?=?false;

i?=?1;

}?else?{

text?=?result.text.getText();

if?(text.trim().indexOf(".")?==?-1)?{

result.text.setText(text?+?".");

}?else?{

result.text.setText(text);

}

}

}?//?獲得點(diǎn)擊+之前的數(shù)值

else?if?(e.getSource()?==?number_key.plus)?{

com?=?true;

i?=?0;

defbutton?=?Double.parseDouble(result.text.getText());

symbol?=?1;

}?//?獲得點(diǎn)擊-之前的數(shù)值

else?if?(e.getSource()?==?number_key.sub)?{

com?=?true;

i?=?0;

defbutton?=?Double.parseDouble(result.text.getText());

symbol?=?2;

}?//?獲得點(diǎn)擊*之前的數(shù)值

else?if?(e.getSource()?==?number_key.mul)?{

com?=?true;

i?=?0;

defbutton?=?Double.parseDouble(result.text.getText());

System.out.println(defbutton);

symbol?=?3;

}?//?獲得點(diǎn)擊/之前的數(shù)值

else?if?(e.getSource()?==?number_key.div)?{

com?=?true;

i?=?0;

defbutton?=?Double.parseDouble(result.text.getText());

symbol?=?4;

}?else?if?(e.getSource()?==?number_key.equal)?{

switch?(symbol)?{

case?1:?{?//?計(jì)算加法

double?ad?=?defbutton

+?Double.parseDouble(result.text.getText());

result.text.setText(ad?+?"");

i?=?0;

text?=?"";

break;

}

case?2:?{?//?計(jì)算減法

double?ad?=?defbutton

-?Double.parseDouble(result.text.getText());

result.text.setText(String.valueOf(ad));

i?=?0;

text?=?"";

break;

}

case?3:?{?//?計(jì)算乘法

double?ad?=?defbutton

*?Double.parseDouble(result.text.getText());

result.text.setText(ad?+?"");

i?=?0;

text?=?"";

break;

}

case?4:?{?//?計(jì)算除法

double?ad?=?defbutton

/?Double.parseDouble(result.text.getText());

result.text.setText(ad?+?"");

i?=?0;

text?=?"";

break;

}

}

System.out.println(com);

}

System.out.println(result.text.getText());

}

@SuppressWarnings("deprecation")

public?static?void?main(String[]?args)?{

Jisuanqi?loveyou?=?new?Jisuanqi();

loveyou.show();

}

}

//?計(jì)算器數(shù)字按鈕定義面板

class?Number_Key?extends?JPanel?{

/**

?*

?*/

private?static?final?long?serialVersionUID?=?1L;

JButton?zero?=?new?JButton("0");?//?數(shù)字鍵0

JButton?one?=?new?JButton("1");?//?數(shù)字鍵1

JButton?two?=?new?JButton("2");?//?數(shù)字鍵2

JButton?three?=?new?JButton("3");?//?數(shù)字鍵3

JButton?four?=?new?JButton("4");?//?數(shù)字鍵4

JButton?five?=?new?JButton("5");?//?數(shù)字鍵5

JButton?six?=?new?JButton("6");?//?數(shù)字鍵6

JButton?seven?=?new?JButton("7");?//?數(shù)字鍵7

JButton?eight?=?new?JButton("8");?//?數(shù)字鍵8

JButton?nine?=?new?JButton("9");?//?數(shù)字鍵9

JButton?plus?=?new?JButton("+");

JButton?sub?=?new?JButton("-");

JButton?mul?=?new?JButton("*");

JButton?div?=?new?JButton("/");

JButton?equal?=?new?JButton("=");

JButton?ce?=?new?JButton("清零");?//?置零鍵

JButton?point?=?new?JButton(".");

Number_Key()?{

setLayout(new?GridLayout(4,?4,?1,?1));?//?定義布局管理器為網(wǎng)格布局

setBackground(Color.blue);?//?設(shè)置背景顏色

//?添加按鈕

add(one);

add(two);

add(three);

add(four);

add(five);

add(six);

add(seven);

add(eight);

add(nine);

add(zero);

add(plus);

add(sub);

add(mul);

add(div);

add(point);

add(equal);

add(ce);

}

}

//?計(jì)算器顯示結(jié)果的窗體

class?Result?extends?JPanel?{

/**

?*

?*/

private?static?final?long?serialVersionUID?=?1L;

//?text先是輸入和結(jié)果

JTextField?text?=?new?JTextField("0");

@SuppressWarnings("deprecation")

Result()?{?//?講輸入的數(shù)字或得到的結(jié)果在text的右邊顯示

text.setHorizontalAlignment(SwingConstants.RIGHT);

text.enable(false);?//?文本框不能編輯

setLayout(new?BorderLayout());?//?設(shè)定布局管理器邊框布局

add(text,?BorderLayout.CENTER);?//?text放置在窗體的中間

}

}

直接復(fù)制?保存成Jisuanqi?.java可以直接運(yùn)行了

當(dāng)前標(biāo)題:java代碼寫親戚計(jì)算器 最簡(jiǎn)單java計(jì)算器代碼
本文來源:http://jinyejixie.com/article30/dossppo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化建站公司、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站策劃靜態(tài)網(wǎng)站、App設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計(jì)公司
遂川县| 织金县| 搜索| 洛阳市| 乌拉特前旗| 府谷县| 塘沽区| 黑龙江省| 金堂县| 色达县| 丹棱县| 黄平县| 正阳县| 成武县| 温泉县| 秦安县| 普宁市| 肇庆市| 刚察县| 阿拉善右旗| 定南县| 阿尔山市| 镇康县| 和顺县| 许昌县| 宁陕县| 获嘉县| 固阳县| 涟源市| 运城市| 宁都县| 高要市| 许昌市| 合山市| 鲜城| 临漳县| 渝北区| 柳河县| 门头沟区| 三亚市| 乾安县|