欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

java計算器用戶框代碼 java設(shè)計計算器界面

急求JAVA簡易計算器窗體設(shè)計代碼

窗體寫好了,運算你自己寫

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、微信小程序定制開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了新市免費建站歡迎大家使用!

import java.awt.Button;

import java.awt.GridLayout;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class SimpleCalculator {

private JFrame f = new JFrame();

private JPanel firstPanel = new JPanel();

private JLabel firstNumLabel = new JLabel("第一個數(shù)字");

private JTextField number1 = new JTextField();

private JPanel secondPanel = new JPanel();

private Button add = new Button("+");

private Button mul = new Button("*");

private Button clear = new Button("清除");

private JPanel thirdPanel = new JPanel();

private JLabel resultLabel = new JLabel("結(jié)果為");

private JTextField result = new JTextField();

public SimpleCalculator(){

firstPanel.setLayout(new GridLayout(1, 2));

firstPanel.add(firstNumLabel);

firstPanel.add(number1);

secondPanel.setLayout(new GridLayout(1, 3));

secondPanel.add(add);

secondPanel.add(mul);

secondPanel.add(clear);

thirdPanel.setLayout(new GridLayout(1, 2));

thirdPanel.add(resultLabel);

thirdPanel.add(result);

f.add(new JLabel("簡易計算器"));

f.add(firstPanel);

f.add(secondPanel);

f.add(thirdPanel);

f.setLayout(new GridLayout(4, 1));

f.setVisible(true);

f.pack();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main(String[] args) {

new SimpleCalculator();

}

}

高分求 用JAVA編寫計算器界面(只要界面)代碼!

package importantpg;

import java.awt.*;

import java.awt.event.*;

public class jisuanqi

{

TextArea tf=new TextArea(3,50 );

Button lb=new Button("clear");

Panel p1=new Panel();

Panel p2=new Panel();

public jisuanqi()

{ Frame f=new Frame("計算器");

Panel p=new Panel();

f.setBackground(Color.white);

f.setSize(500, 1500);

f.setVisible(true);

f.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e) {System.exit(1);}

});

Button[] b=new Button[18]; b[10]=new Button("-"); b[15]=new Button("(");

b[11]=new Button("+"); b[12]=new Button("*"); b[16]=new Button(")");

b[13]=new Button("/"); b[14]=new Button("="); b[17]=new Button("退格");

for(int i=0;i=9;++i)

{

b[i]=new Button(""+i);

}

p.setLayout(new GridLayout(6,3));

avent aa=new avent();

lb.addActionListener(aa);

for(int i=0;i18;++i)

{

p.add(b[i]);b[i].addActionListener(aa);

}

p1.setLayout(new BorderLayout());

p1.add(lb,"North");p1.add(tf,"South");f.add(p1, "South");

f.add(p,"Center"); f.pack();

} //構(gòu)造方法

public static void main(String args[])

{

new jisuanqi();

}

class avent implements ActionListener //事件體

{

public void actionPerformed(ActionEvent e)

{

String s=null; String in="";

s=e.getActionCommand();

if (s!="="s!=nulls!="clear"s!="退格")

{

tf.append(s);

in+=s; s=null;

}

else if(s=="clear")

{

tf.replaceRange("",0,1000);

}else if(s=="=")

{

tf.append("="+jisuan(tf.getText()));

}

else if(s=="退格") {}

}///////////////////////////////////////////////事件處理方法

}

///////////////////////////////////////計算過程//////////////

static String jisuan(String s)

{

String hs= "";

char[] ch = new char[s.length()];

int top = -1;

for (int i = 0; i s.length(); i++)

{

char rsc = s.charAt(i);

switch(rsc)

{

case ' ' : break;

case '+' : //操作符

case '-' :

while (top = 0) //棧不為空時

{

char pc = ch[top--]; //pop Operator

if (pc == '(')

{

ch[++top] = pc; //push Operator

break;

}

else {hs = hs + pc;}

}

ch[++top] = rsc; //push Operator

hs += " ";break;

case '*' : //操作符

case '/' :

while (top = 0) //棧不為空時

{

char c = ch[top--]; //pop Operator

if (c == '(')

{

ch[++top] = c; //push Operator

break;

}

else

{

if (c == '+' || c == '-')

{

ch[++top] = c; //push Operator

break;

}

else

{

hs = hs + c;

}

}

}

ch[++top] = rsc; //push Operator

hs += " ";

break;

case '(' : //操作符

ch[++top] = rsc;

hs+= " ";

break;

case ')' : //操作符

while (top = 0) //棧不為空時

{

char c = ch[top--]; //pop Operator

if (c == '(')

{

break;

}

else

{

hs= hs + c;

}

}

hs += " ";

break;

default : //操作數(shù)

hs= hs + rsc;

break;

}//switch

}/////////////////////////////for

while (top = 0)

{

hs= hs + ch[top--]; //pop Operator

}

//System.out.println("hs="+hs); //后綴

//////////////////////////////////////////////后綴////////////////////////////////////

//后綴表達(dá)式計算

double[] result = new double[hs.length()];

double x, y;

top = - 1;

String st = "";

for (int i = 0; i hs.length(); i++)

{

char c = hs.charAt(i);

if ((c = '0' c = '9') || c == '.')

{

st += c;

}

if ((c == ' ' st != "") || i == hs.length() - 1)

{

result[++top] = Double.parseDouble(st) ; //push Operands

st = "";

}

if (c == '+' || c == '-' || c == '*' || c == '/')

{

if ((st != ""))

{

result[++top] =Double.parseDouble(st) ; //push Operands

st = "";

}

y = result[top--];

x = result[top--];

switch (c)

{

case '+' :

result[++top] = x+y;break;//push 中間結(jié)果再次入棧

case '-' :

result[++top] = x-y;break;

case '*' :

result[++top] = x*y;break;

case '/' :

result[++top] = x/y;break;

default :break;

}

}////////////////////if

} ///////////////////////for

return String.valueOf(result[top]); //pop 最終結(jié)果

} /////////////////////////jisuan

}

java計算器代碼

這段代碼對你有幫助

import javax.swing.*;

import javax.swing.border.Border;

import java.awt.*;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.math.BigDecimal;

import java.math.RoundingMode;

import java.util.HashMap;

/**

* 我的計算器。MyCalculator 繼承于 JFrame,是計算器的界面

*/

public class MyCalculator extends JFrame {

private Border border = BorderFactory.createEmptyBorder(5, 5, 5, 5);

private JTextField textbox = new JTextField("0");

private CalculatorCore core = new CalculatorCore();

private ActionListener listener = new ActionListener() {

public void actionPerformed(ActionEvent e) {

JButton b = (JButton) e.getSource();

String label = b.getText();

String result = core.process(label);

textbox.setText(result);

}

};

public MyCalculator(String title) throws HeadlessException {

super(title); // 調(diào)用父類構(gòu)造方法

setupFrame(); // 調(diào)整窗體屬性

setupControls(); // 創(chuàng)建控件

}

private void setupControls() {

setupDisplayPanel(); // 創(chuàng)建文本面板

setupButtonsPanel(); // 創(chuàng)建按鈕面板

}

// 創(chuàng)建按鈕面板并添加按鈕

private void setupButtonsPanel() {

JPanel panel = new JPanel();

panel.setBorder(border);

panel.setLayout(new GridLayout(4, 5, 3, 3));

createButtons(panel, new String[]{

"7", "8", "9", "+", "C",

"4", "5", "6", "-", "CE",

"1", "2", "3", "*", "", // 空字符串表示這個位置沒有按鈕

"0", ".", "=", "/", ""

});

this.add(panel, BorderLayout.CENTER);

}

/**

* 在指定的面板上創(chuàng)建按鈕

*

* @param panel 要創(chuàng)建按鈕的面板

* @param labels 按鈕文字

*/

private void createButtons(JPanel panel, String[] labels) {

for (String label : labels) {

// 如果 label 為空,則表示創(chuàng)建一個空面板。否則創(chuàng)建一個按鈕。

if (label.equals("")) {

panel.add(new JPanel());

} else {

JButton b = new JButton(label);

b.addActionListener(listener); // 為按鈕添加偵聽器

panel.add(b);

}

}

}

// 設(shè)置顯示面板,用一個文本框來作為計算器的顯示部分。

private void setupDisplayPanel() {

JPanel panel = new JPanel();

panel.setLayout(new BorderLayout());

panel.setBorder(border);

setupTextbox();

panel.add(textbox, BorderLayout.CENTER);

this.add(panel, BorderLayout.NORTH);

}

// 調(diào)整文本框

private void setupTextbox() {

textbox.setHorizontalAlignment(JTextField.RIGHT); // 文本右對齊

textbox.setEditable(false); // 文本框只讀

textbox.setBackground(Color.white); // 文本框背景色為白色

}

// 調(diào)整窗體

private void setupFrame() {

this.setDefaultCloseOperation(EXIT_ON_CLOSE); // 當(dāng)窗體關(guān)閉時程序結(jié)束

this.setLocation(100, 50); // 設(shè)置窗體顯示在桌面上的位置

this.setSize(300, 200); // 設(shè)置窗體大小

this.setResizable(false); // 窗體大小固定

}

// 程序入口

public static void main(String[] args) throws Exception {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

MyCalculator frame = new MyCalculator("我的計算器");

frame.setVisible(true); // 在桌面上顯示窗體

}

}

/**

* 計算器核心邏輯。這個邏輯只能處理 1~2 個數(shù)的運算。

*/

class CalculatorCore {

private String displayText = "0"; // 要顯示的文本

boolean reset = true;

private BigDecimal number1, number2;

private String operator;

private HashMapString, Operator operators = new HashMapString, Operator();

private HashMapString, Processor processors = new HashMapString, Processor();

CalculatorCore() {

setupOperators();

setupProcessors();

}

// 為每種命令添加處理方式

private void setupProcessors() {

processors.put("[0-9]", new Processor() {

public void calculate(String command) {

numberClicked(command);

}

});

processors.put("\\.", new Processor() {

public void calculate(String command) {

dotClicked();

}

});

processors.put("=", new Processor() {

public void calculate(String command) {

equalsClicked();

}

});

processors.put("[+\\-*/]", new Processor() {

public void calculate(String command) {

operatorClicked(command);

}

});

processors.put("C", new Processor() {

public void calculate(String command) {

clearClicked();

}

});

processors.put("CE", new Processor() {

public void calculate(String command) {

clearErrorClicked();

}

});

}

// 為每種 operator 添加處理方式

private void setupOperators() {

operators.put("+", new Operator() {

public BigDecimal process(BigDecimal number1, BigDecimal number2) {

return number1.add(number2);

}

});

operators.put("-", new Operator() {

public BigDecimal process(BigDecimal number1, BigDecimal number2) {

return number1.subtract(number2);

}

});

operators.put("*", new Operator() {

public BigDecimal process(BigDecimal number1, BigDecimal number2) {

return number1.multiply(number2);

}

});

operators.put("/", new Operator() {

public BigDecimal process(BigDecimal number1, BigDecimal number2) {

return number1.divide(number2, 30, RoundingMode.HALF_UP);

}

});

}

// 根據(jù)命令處理。這里的命令實際上就是按鈕文本。

public String process(String command) {

for (String pattern : processors.keySet()) {

if (command.matches(pattern)) {

processors.get(pattern).calculate(command);

break;

}

}

return displayText;

}

// 當(dāng)按下 CE 時

private void clearErrorClicked() {

if (operator == null) {

number1 = null;

} else {

number2 = null;

}

displayText = "0";

reset = true;

}

// 當(dāng)按下 C 時,將計算器置為初始狀態(tài)。

private void clearClicked() {

number1 = null;

number2 = null;

operator = null;

displayText = "0";

reset = true;

}

// 當(dāng)按下 = 時

private void equalsClicked() {

calculateResult();

number1 = null;

number2 = null;

operator = null;

reset = true;

}

// 計算結(jié)果

private void calculateResult() {

number2 = new BigDecimal(displayText);

Operator oper = operators.get(operator);

if (oper != null) {

BigDecimal result = oper.process(number1, number2);

displayText = result.toString();

}

}

// 當(dāng)按下 +-*/ 時(這里也可以擴(kuò)展成其他中間操作符)

private void operatorClicked(String command) {

if (operator != null) {

calculateResult();

}

number1 = new BigDecimal(displayText);

operator = command;

reset = true;

}

// 當(dāng)按下 . 時

private void dotClicked() {

if (displayText.indexOf(".") == -1) {

displayText += ".";

} else if (reset) {

displayText = "0.";

}

reset = false;

}

// 當(dāng)按下 0-9 時

private void numberClicked(String command) {

if (reset) {

displayText = command;

} else {

displayText += command;

}

reset = false;

}

// 運算符處理接口

interface Operator {

BigDecimal process(BigDecimal number1, BigDecimal number2);

}

// 按鈕處理接口

interface Processor {

void calculate(String command);

}

}

急求?。。∪绾斡胘ava程序代碼實現(xiàn)計算器界面

package?jisuanqi_new;

import?java.awt.*;

import?java.awt.event.*;

public?class?JiSuanQi_new?implements?ActionListener

{

Panel?p1;//聲明面板p1

TextField?t1;//聲明文本行t1

String[]?label?=?{"7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+"};//聲明標(biāo)簽數(shù)組label1存放按鈕上的標(biāo)簽

Button[]?b;//聲明按鈕數(shù)組存放16個按鈕

private?int?i;//聲明i以備后用

private?String?op1?=?"0";//運算數(shù)備用

private?String?operator?=?"+";//運算符備用

private?boolean?append?=?false;//備用

public?JiSuanQi_new()//構(gòu)造方法

{

t1=new?TextField();//初始化文本行t1

b?=?new?Button[label.length];//初始化按鈕數(shù)組b

p1=new?Panel();//初始化面板p1

p1.setLayout(new?GridLayout(4,4,4,4));//使面板選擇網(wǎng)格布局管理器以備儲存16個按鈕(4行4列)

for(int?i=0;ib.length;i++)//利用for循環(huán)把標(biāo)簽放在按鈕上,使每個按鈕添加事件監(jiān)聽器,在面板p1上添加上16個按鈕

{

b[i]?=?new?Button(label[i]);//把標(biāo)簽依次放在16個按鈕上

b[i].addActionListener(this);//使每個按鈕添加動作事件監(jiān)聽器

p1.add(b[i]);?//分別將按鈕添加到面板p1上

}

Frame?f=new?Frame("計算器1.0");//初始化窗口f,起名字計算器1.0

f.setLayout(new?BorderLayout());//為窗口選擇邊界布局管理器

f.add(BorderLayout.NORTH,t1);//把文本行他添加到窗口的北部

f.add(BorderLayout.CENTER,p1);//把面吧p1添加到窗口的中間

f.addWindowListener(new?WindowAdapter(){//給窗口f添加窗口事件監(jiān)聽器

public?void?windowClosing(WindowEvent?eve){//運行窗口關(guān)閉方法

System.exit(0);//退出程序

}

});

f.setSize(250,?250);//設(shè)置窗口大小

f.setLocation(200,200);

f.setVisible(true);//顯示窗口

}

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

{

new?JiSuanQi_new();?//調(diào)用構(gòu)造方法

}

public?void?actionPerformed(ActionEvent?ae)

{//按鈕被操作發(fā)生

String?comm?=?ae.getActionCommand();//返回與此動作相關(guān)的命令字符串,即:使用者第一次點擊的按鈕是什么。

if("0123456789".indexOf(comm)!=-1)//如果相關(guān)命令字符串為0~9之間的數(shù)字則執(zhí)行

{

if(append){

String?temp?=?t1.getText();//新數(shù)字

t1.setText(temp+comm);

}else{?????????????????????????//因為此時append為false執(zhí)行這個

t1.setText(comm);?//將文本行t1設(shè)置為相關(guān)命令字符串(你按中的按鈕代碼)

append?=??true;//此時append=true若繼續(xù)按按鈕若繼續(xù)按數(shù)字的話1.第一次的按話不會改變2.非第一次按得話會覆蓋之前按得數(shù)字(即缺點:只能進(jìn)行單個數(shù)字的計算)

}

}

else?if(("+-*/".indexOf(comm)!=-1))//如果相關(guān)命令字符串為+-*/之間的數(shù)字則執(zhí)行

{

//保存

//t1.setText(comm);

op1?=?t1.getText();//把第一個數(shù)賦值給op1

operator?=?comm;//把命令字符串賦值給operator

append?=?false;//此時append為false即若繼續(xù)按按鈕若按數(shù)字的話將重復(fù)上面的動作,按符號的話將覆蓋之前的符號

}

else?if("=".equals(comm))//如果按的是=號,則按條件進(jìn)行下面的運算

{

String?op2?=?t1.getText();//op2第二個數(shù)

double?d1?=?Double.parseDouble(op1);

double?d2?=?Double.parseDouble(op2);

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

d1?=?d1?+?d2?;

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

d1?=?d1?-?d2;

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

d1?=?d1?*?d2;

}else?{

d1?=?d1?/?d2;

}

t1.setText(d1+"");//顯示計算結(jié)果

append?=?false;

}

else?if(".".equals(comm))//若是.號繼續(xù)按

{

String?temp?=?t1.getText();

if(temp.indexOf(".")==-1){

t1.setText(temp+".");

append?=?true;

}

}

}

}

JAVA 編寫計算器 要代碼最簡單的

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

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;

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

*

* @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)行的計算式

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

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

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

public CalculatorA(){

frame = new JFrame("我的計算器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時表示還沒有進(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ù)格式錯誤.....");

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;

}

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

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();

}

}

java 計算器代碼

import javax.swing.*;

import javax.swing.border.Border;

import java.awt.*;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.math.BigDecimal;

import java.math.RoundingMode;

import java.util.HashMap;

/**

* 我的計算器。Cheshi 繼承于 JFrame,是計算器的界面

c*/

public class Cheshi extends JFrame {

private Border border = BorderFactory.createEmptyBorder(5, 5, 5, 5);

private JTextField textbox = new JTextField("0");

private CalculatorCore core = new CalculatorCore();

private ActionListener listener = new ActionListener() {

public void actionPerformed(ActionEvent e) {

JButton b = (JButton) e.getSource();

String label = b.getText();

String result = core.process(label);

textbox.setText(result);

}

};

public Cheshi(String title) throws HeadlessException {

super(title); // 調(diào)用父類構(gòu)造方法

setupFrame(); // 調(diào)整窗體屬性

setupControls(); // 創(chuàng)建控件

}

private void setupControls() {

setupDisplayPanel(); // 創(chuàng)建文本面板

setupButtonsPanel(); // 創(chuàng)建按鈕面板

}

// 創(chuàng)建按鈕面板并添加按鈕

private void setupButtonsPanel() {

JPanel panel = new JPanel();

panel.setBorder(border);

panel.setLayout(new GridLayout(4, 5, 3, 3));

createButtons(panel, new String[]{

"7", "8", "9", "+", "C",

"4", "5", "6", "-", "CE",

"1", "2", "3", "*", "", // 空字符串表示這個位置沒有按鈕

"0", ".", "=", "/", ""

});

this.add(panel, BorderLayout.CENTER);

}

/**

* 在指定的面板上創(chuàng)建按鈕

*

* @param panel 要創(chuàng)建按鈕的面板

* @param labels 按鈕文字

*/

private void createButtons(JPanel panel, String[] labels) {

for (String label : labels) {

// 如果 label 為空,則表示創(chuàng)建一個空面板。否則創(chuàng)建一個按鈕。

if (label.equals("")) {

panel.add(new JPanel());

} else {

JButton b = new JButton(label);

b.addActionListener(listener); // 為按鈕添加偵聽器

panel.add(b);

}

}

}

// 設(shè)置顯示面板,用一個文本框來作為計算器的顯示部分。

private void setupDisplayPanel() {

JPanel panel = new JPanel();

panel.setLayout(new BorderLayout());

panel.setBorder(border);

setupTextbox();

panel.add(textbox, BorderLayout.CENTER);

this.add(panel, BorderLayout.NORTH);

}

// 調(diào)整文本框

private void setupTextbox() {

textbox.setHorizontalAlignment(JTextField.RIGHT); // 文本右對齊

textbox.setEditable(false); // 文本框只讀

textbox.setBackground(Color.white); // 文本框背景色為白色

}

// 調(diào)整窗體

private void setupFrame() {

this.setDefaultCloseOperation(EXIT_ON_CLOSE); // 當(dāng)窗體關(guān)閉時程序結(jié)束

this.setLocation(100, 50); // 設(shè)置窗體顯示在桌面上的位置

this.setSize(300, 200); // 設(shè)置窗體大小

this.setResizable(false); // 窗體大小固定

}

// 程序入口

public static void main(String[] args) throws Exception {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

Cheshi frame = new Cheshi("我的計算器");

frame.setVisible(true); // 在桌面上顯示窗體

}

}

/**

* 計算器核心邏輯。這個邏輯只能處理 1~2 個數(shù)的運算。

*/

class CalculatorCore {

private String displayText = "0"; // 要顯示的文本

boolean reset = true;

private BigDecimal number1, number2;

private String operator;

private HashMapString, Operator operators = new HashMapString, Operator();

private HashMapString, Processor processors = new HashMapString, Processor();

CalculatorCore() {

setupOperators();

setupProcessors();

}

// 為每種命令添加處理方式

private void setupProcessors() {

processors.put("[0-9]", new Processor() {

public void calculate(String command) {

numberClicked(command);

}

});

processors.put("\\.", new Processor() {

public void calculate(String command) {

dotClicked();

}

});

processors.put("=", new Processor() {

public void calculate(String command) {

equalsClicked();

}

});

processors.put("[+\\-*/]", new Processor() {

public void calculate(String command) {

operatorClicked(command);

}

});

processors.put("C", new Processor() {

public void calculate(String command) {

clearClicked();

}

});

processors.put("CE", new Processor() {

public void calculate(String command) {

clearErrorClicked();

}

});

}

// 為每種 operator 添加處理方式

private void setupOperators() {

operators.put("+", new Operator() {

public BigDecimal process(BigDecimal number1, BigDecimal number2) {

return number1.add(number2);

}

});

operators.put("-", new Operator() {

public BigDecimal process(BigDecimal number1, BigDecimal number2) {

return number1.subtract(number2);

}

});

operators.put("*", new Operator() {

public BigDecimal process(BigDecimal number1, BigDecimal number2) {

return number1.multiply(number2);

}

});

operators.put("/", new Operator() {

public BigDecimal process(BigDecimal number1, BigDecimal number2) {

return number1.divide(number2, 30, RoundingMode.HALF_UP);

}

});

}

// 根據(jù)命令處理。這里的命令實際上就是按鈕文本。

public String process(String command) {

for (String pattern : processors.keySet()) {

if (command.matches(pattern)) {

processors.get(pattern).calculate(command);

break;

}

}

return displayText;

}

// 當(dāng)按下 CE 時

private void clearErrorClicked() {

if (operator == null) {

number1 = null;

} else {

number2 = null;

}

displayText = "0";

reset = true;

}

// 當(dāng)按下 C 時,將計算器置為初始狀態(tài)。

private void clearClicked() {

number1 = null;

number2 = null;

operator = null;

displayText = "0";

reset = true;

}

// 當(dāng)按下 = 時

private void equalsClicked() {

calculateResult();

number1 = null;

number2 = null;

operator = null;

reset = true;

}

// 計算結(jié)果

private void calculateResult() {

number2 = new BigDecimal(displayText);

Operator oper = operators.get(operator);

if (oper != null) {

BigDecimal result = oper.process(number1, number2);

displayText = result.toString();

}

}

// 當(dāng)按下 +-*/ 時(這里也可以擴(kuò)展成其他中間操作符)

private void operatorClicked(String command) {

if (operator != null) {

calculateResult();

}

number1 = new BigDecimal(displayText);

operator = command;

reset = true;

}

// 當(dāng)按下 . 時

private void dotClicked() {

if (displayText.indexOf(".") == -1) {

displayText += ".";

} else if (reset) {

displayText = "0.";

}

reset = false;

}

// 當(dāng)按下 0-9 時

private void numberClicked(String command) {

if (reset) {

displayText = command;

} else {

displayText += command;

}

reset = false;

}

// 運算符處理接口

interface Operator {

BigDecimal process(BigDecimal number1, BigDecimal number2);

}

// 按鈕處理接口

interface Processor {

void calculate(String command);

}

}

網(wǎng)頁名稱:java計算器用戶框代碼 java設(shè)計計算器界面
URL網(wǎng)址:http://aaarwkj.com/article26/doodjcg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊、關(guān)鍵詞優(yōu)化、外貿(mào)網(wǎng)站建設(shè)、自適應(yīng)網(wǎng)站、Google網(wǎng)站排名

廣告

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

成都網(wǎng)站建設(shè)
日本在线电影一区二区三区| 中文字幕精品一区二区三区精品| 亚洲理论在线观看电影| 日本91大神在线观看| 中文字幕一区二区三天| 欧美日韩亚洲国产专区精品| 禁区正片免费看完整国产 | 97久久久人妻精品一区| 国产高清内射一级一片高清视频| 视频一区视频二区在线| 一区二区三区四区中文在线| 亚洲熟妇av一区二区三区l| 精品人妻少妇一区二区三区| 麻豆影片在线免费观看| 丰满人妻视频一二三区| 亚洲综合av伊人久久| 成人午夜激情在线观看| 亚洲一区二区三区久久精品| 少妇被啪出水在线视频| 免费特黄特黄的欧美大片| 在线播放亚洲一区二区三区| 日本欧美高清一区二区| 亚洲三级伦理中文字幕| 欧美日本一道本一区二区三区| 日本一区二区三区不卡在线| 性生活免费在线观看视频| 日麻批视频在线免费观看| 欧美日韩亚洲中文综合网 | 日本毛茸茸的丰满熟妇| 久久综合伊人欧美精品| 久久av天堂在线观看| 国产一区二区主播不卡| 国产91黑丝视频在线观看| 99久久精品费精品国产风间由美| 亚洲国产精品va在线香蕉| 东京热男人的av天堂| 亚洲av日韩av一区| 中文字幕三级电影天堂| 欧美激情网页一区三区| 日韩精品欧美视频久久| 国产免费一级av剧情|