當前位置:首頁 » 編程語言 » java計算器程序

java計算器程序

發布時間: 2023-03-21 18:59:05

Ⅰ 用java程序編寫一個計算器

給你一個參考,希望不要被網路吞了當晚餐
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DecimalFormat;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Calculator {
//計算器面板
private JFrame f = new JFrame("Calculator");
//輸入面扳
private JPanel inputPanel = new JPanel();
//加減乘除面板
private JPanel operationPanel = new JPanel();
//數字面板
private JPanel buttonsPanel = new JPanel();

//輸入數據文本框
private JTextField input = new JTextField(20);

//退格鍵
private JButton backspace = new JButton("BackSpace");
//清空
private JButton CE = new JButton("CE ");
//刪除
private JButton C = new JButton("C ");

//四則運算符號鍵
private JButton add = new JButton("+");
private JButton sub = new JButton("-");
private JButton mul = new JButton("*");
private JButton div = new JButton("/");

//小數點
private JButton decimal = new JButton(".");
//等號
private JButton equal = new JButton("=");

//數字鍵
private JButton zero = new JButton("0");
private JButton one = new JButton("1");
private JButton two = new JButton("2");
private JButton three = new JButton("3");
private JButton four = new JButton("4");
private JButton five = new JButton("5");
private JButton six = new JButton("6");
private JButton seven = new JButton("7");
private JButton eight = new JButton("8");
private JButton nine = new JButton("9");

private String num1 = "";//保存第一個運算數字
private String operator = "";//保存運算符號

public static void main(String[] args) {
new Calculator();//new計算器實例
}

public Calculator(){
//添加組件,布局
inputPanel.add(input);
f.add(inputPanel, BorderLayout.NORTH);

operationPanel.add(backspace);
operationPanel.add(CE);
operationPanel.add(C);
f.add(operationPanel, BorderLayout.CENTER);

buttonsPanel.add(add);
buttonsPanel.add(sub);
buttonsPanel.add(mul);
buttonsPanel.add(div);
buttonsPanel.add(one);
buttonsPanel.add(two);
buttonsPanel.add(three);
buttonsPanel.add(zero);
buttonsPanel.add(four);
buttonsPanel.add(five);
buttonsPanel.add(six);
buttonsPanel.add(decimal);
buttonsPanel.add(seven);
buttonsPanel.add(eight);
buttonsPanel.add(nine);
buttonsPanel.add(equal);
buttonsPanel.setLayout(new GridLayout(4, 4));
f.add(buttonsPanel, BorderLayout.SOUTH);

//注冊各個組件監聽事件
backspace.addMouseListener(new OperationMouseListener());
CE.addMouseListener(new OperationMouseListener());
C.addMouseListener(new OperationMouseListener());
decimal.addMouseListener(new OperationMouseListener());
equal.addMouseListener(new OperationMouseListener());

//注冊四則運算監聽
add.addMouseListener(new CalcMouseListener());
sub.addMouseListener(new CalcMouseListener());
mul.addMouseListener(new CalcMouseListener());
div.addMouseListener(new CalcMouseListener());

//注冊數字監聽事件
zero.addMouseListener(new NumberMouseListener());
one.addMouseListener(new NumberMouseListener());
two.addMouseListener(new NumberMouseListener());
three.addMouseListener(new NumberMouseListener());
four.addMouseListener(new NumberMouseListener());
five.addMouseListener(new NumberMouseListener());
six.addMouseListener(new NumberMouseListener());
seven.addMouseListener(new NumberMouseListener());
eight.addMouseListener(new NumberMouseListener());
nine.addMouseListener(new NumberMouseListener());

f.setVisible(true);
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private class NumberMouseListener implements MouseListener{

public void mouseClicked(MouseEvent e) {
if(input.getText().trim().equals("0")){//如果文本框已經是0,結果還是0
input.setText(((JButton)e.getSource()).getText().trim());
}else{//否則的話,把0添加到後面,譬如文本框是1,結果就為10
input.setText(input.getText().concat(((JButton)e.getSource()).getText().trim()));
}
}

public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}

private class CalcMouseListener implements MouseListener{
//如果輸入的是運算符號,保存第一個結果和運算符號
public void mouseClicked(MouseEvent e) {
num1 = input.getText().trim();input.setText("");
operator = ((JButton)e.getSource()).getText().trim();
}

public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}

private class OperationMouseListener implements MouseListener{

public void mouseClicked(MouseEvent e) {
if(e.getSource() == backspace){//退格鍵,刪除一個直到沒有字元刪除
String inputText = input.getText();
if(inputText.length() > 0){
input.setText(inputText.substring(0, inputText.length() - 1));
}
}else if(e.getSource() == C){
input.setText("0");//C,清空所有運算數字和符號
num1 = "";
}else if(e.getSource() == CE){
input.setText("0");//CE-->將文本框置為0
}else if(e.getSource() == decimal){
String text = input.getText().trim();
//如果按了小數點,如果文本框已經有小數點,不做任何操作,否則在結果後面加上小數點
if(text.indexOf(".") == -1){
input.setText(text.concat("."));
}
}else if(e.getSource() == equal){
//如果是等號
if(!operator.trim().equals("")){
if(!input.getText().trim().equals("")){
double result = 0D;
if(operator.equals("+")){//執行加法運算
result = Double.parseDouble(num1) + Double.parseDouble(input.getText().trim());
}else if(operator.equals("-")){//減法運算
result = Double.parseDouble(num1) - Double.parseDouble(input.getText().trim());
}else if(operator.equals("*")){//乘法運算
result = Double.parseDouble(num1) * Double.parseDouble(input.getText().trim());
}else if(operator.equals("/")){//除法運算
result = Double.parseDouble(num1) / Double.parseDouble(input.getText().trim());
}

//格式化最終結果,保留兩位小數點
input.setText(new DecimalFormat("0.00").format(result));
}
}

}

}

public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}

}

Ⅱ 用JAVA編寫一個簡單的計算器,要求如下:

以下是上圖計算器的代碼:

  • packageComputer;

  • importjava.awt.BorderLayout;

  • importjava.awt.Color;

  • importjava.awt.Container;

  • importjava.awt.Font;

  • importjava.awt.GridLayout;

  • importjava.awt.event.ActionEvent;

  • importjava.awt.event.ActionListener;

  • importjava.util.Stack;

  • importjavax.swing.JApplet;

  • importjavax.swing.JButton;

  • importjavax.swing.JFrame;

  • importjavax.swing.JPanel;

  • importjavax.swing.JTextField;

  • {

  • /**

  • *

  • */

  • =1L;

  • privateJTextFieldtextField=newJTextField("請輸入");

  • Stringoperator="";//操作

  • Stringinput="";//輸入的式子

  • booleanflag=true;

  • //booleanflag1=true;

  • //booleanflag2=true;

  • publicvoidinit()//覆寫Applet里邊的init方法

  • {

  • ContainerC=getContentPane();

  • JButtonb[]=newJButton[16];

  • JPanelpanel=newJPanel();

  • C.add(textField,BorderLayout.NORTH);

  • C.add(panel,BorderLayout.CENTER);

  • panel.setLayout(newGridLayout(4,4,5,5));

  • Stringname[]={"7","8","9","+","4","5","6","-","1","2","3","*","0","C","=","/"};//設置按鈕

  • for(inti=0;i<16;i++)//添加按鈕

  • {

  • b[i]=newJButton(name[i]);

  • b[i].setBackground(newColor(192,192,192));

  • b[i].setForeground(Color.BLUE);//數字鍵設置為藍顏色

  • if(i%4==3)

  • b[i].setForeground(Color.RED);

  • b[i].setFont(newFont("宋體",Font.PLAIN,16));//設置字體格式

  • panel.add(b[i]);

  • b[i].addActionListener(this);

  • }

  • b[13].setForeground(Color.RED);//非數字鍵,即運算鍵設置為紅顏色

  • b[13].setForeground(Color.RED);

  • }

  • publicvoidactionPerformed(ActionEvente)

  • {

  • intcnt=0;

  • StringactionCommand=e.getActionCommand();

  • if(actionCommand.equals("+")||actionCommand.equals("-")||actionCommand.equals("*")||actionCommand.equals("/"))

  • input+=""+actionCommand+"";//設置輸入,把輸入的樣式改成需要的樣子

  • elseif(actionCommand.equals("C"))

  • input="";

  • elseif(actionCommand.equals("="))//當監聽到等號時,則處理input

  • {

  • input+="="+compute(input);

  • textField.setText(input);

  • input="";

  • cnt=1;

  • }

  • else

  • input+=actionCommand;//數字為了避免多位數的輸入不需要加空格

  • if(cnt==0)

  • textField.setText(input);

  • }

  • privateStringcompute(Stringinput)//即1237的樣例

  • {

  • Stringstr[];

  • str=input.split("");

  • Stack<Double>s=newStack<Double>();

  • doublem=Double.parseDouble(str[0]);

  • s.push(m);

  • for(inti=1;i<str.length;i++)

  • {

  • if(i%2==1)

  • {

  • if(str[i].compareTo("+")==0)

  • {

  • doublehelp=Double.parseDouble(str[i+1]);

  • s.push(help);

  • }

  • if(str[i].compareTo("-")==0)

  • {

  • doublehelp=Double.parseDouble(str[i+1]);

  • s.push(-help);

  • }

  • if(str[i].compareTo("*")==0)

  • {

  • doublehelp=Double.parseDouble(str[i+1]);

  • doubleans=s.peek();//取出棧頂元素

  • s.pop();//消棧

  • ans*=help;

  • s.push(ans);

  • }

  • if(str[i].compareTo("/")==0)

  • {

  • doublehelp=Double.parseDouble(str[i+1]);

  • doubleans=s.peek();

  • s.pop();

  • ans/=help;

  • s.push(ans);

  • }

  • }

  • }

  • doubleans=0d;

  • while(!s.isEmpty())

  • {

  • ans+=s.peek();

  • s.pop();

  • }

  • Stringresult=String.valueOf(ans);

  • returnresult;

  • }

  • publicstaticvoidmain(Stringargs[])

  • {

  • JFrameframe=newJFrame("Count");

  • Countapplet=newCount();

  • frame.getContentPane().add(applet,BorderLayout.CENTER);

  • applet.init();//applet的init方法

  • applet.start();//線程開始

  • frame.setSize(350,400);//設置窗口大小

  • frame.setVisible(true);//設置窗口可見

  • }

  • }

Ⅲ 求一個用JAVA編寫的計算器程序(1)實現簡單加、減、乘、除的運算。 (

public
MyCalculator()
{
f
=
new
JFrame("計算器ByMdou");
Container
contentPane
=
f.getContentPane();
/****************菜單的創建開始**************************************/
JMenuBar
mBar
=
new
JMenuBar();
mBar.setOpaque(true);
mEdit
=
new
JMenu("編輯(E)");
mEdit.setMnemonic(KeyEvent.VK_E);
mCopy
=
new
JMenuItem("復制(C)");
mEdit.add(mCopy);
mPaste
=
new
JMenuItem("粘貼(P)");
mEdit.add(mPaste);
mView
=
new
JMenu("查看(V)");
mView.setMnemonic(KeyEvent.VK_V);
mView.add(new
JMenuItem("標准型"));
mView.add(new
JMenuItem("科學型"));
mView.addSeparator();
mView.add(new
JMenuItem("查看分組"));
mHelp
=
new
JMenu("幫助(H)");
mHelp.setMnemonic(KeyEvent.VK_H);
mHelp.add(new
JMenuItem("幫助主題"));
mHelp.addSeparator();
mHelp.add(new
JMenuItem("關於計算器"));
mBar.add(mEdit);
mBar.add(mView);
mBar.add(mHelp);
f.setJMenuBar(mBar);
contentPane.setLayout(new
BorderLayout
());
JPanel
pTop
=
new
JPanel();
tResult
=
new
JTextField("0.",26);
tResult.setHorizontalAlignment(JTextField.RIGHT
);
tResult.setEditable(false);
pTop.add(tResult);
contentPane.add(pTop,BorderLayout.NORTH);
JPanel
pBottom
=
new
JPanel();
pBottom.setLayout(new
BorderLayout());
JPanel
pLeft
=
new
JPanel();
pLeft.setLayout(new
GridLayout(5,1,3,3));
bM
=
new
JButton("
");
bM.setEnabled(false);
pLeft.add(bM);
bOther
=
new
JButton("MC");
bOther.addActionListener(this);
bOther.setForeground(Color.RED);
bOther.setMargin(new
Insets(3,2,3,2));
pLeft.add(bOther);
bOther
=
new
JButton("MR");
bOther.addActionListener(this);
bOther.setForeground(Color.RED);
bOther.setMargin(new
Insets(3,2,3,2));
pLeft.add(bOther);
bOther
=
new
JButton("MS");
bOther.addActionListener(this);
bOther.setForeground(Color.RED);
bOther.setMargin(new
Insets(3,2,3,2));
pLeft.add(bOther);
bOther
=
new
JButton("M+");
bOther.addActionListener(this);
bOther.setForeground(Color.RED);
bOther.setMargin(new
Insets(3,2,3,2));
pLeft.add(bOther);

Ⅳ 用java編寫一個簡單計算器

/*

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

*/

importjava.awt.*;

importjava.awt.event.*;

importjavax.swing.*;

/**

* A simple calculator program.

*<p>I saw this program in a QQ group, and help a friend correct it.</p>

*

*@authorSingyuenYip

*@version1.00 12/29/2009

*@seeJFrame

*@seeActionListener

*/

{

/**

* Serial Version UID

*/

= -169068472193786457L;

/**

* This class help close the Window.

*@authorSingyuenYip

*

*/

{

publicvoidwindowClosing(WindowEvent we) {

System.exit(0);

}

}

inti;

// Strings for Digit & Operator buttons.

privatefinalString[]str= {"7","8","9","/","4","5","6","*","1",

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

// Build buttons.

JButton[]buttons=newJButton[str.length];

// For cancel or reset.

JButtonreset=newJButton("CE");

// Build the text field to show the result.

JTextFielddisplay=newJTextField("0");

/**

* Constructor without parameters.

*/

publicJCalculator() {

super("Calculator");

// Add a panel.

JPanel panel1 =newJPanel(newGridLayout(4, 4));

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

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

buttons[i] =newJButton(str[i]);

panel1.add(buttons[i]);

}

JPanel panel2 =newJPanel(newBorderLayout());

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

panel2.add("Center",display);

panel2.add("East",reset);

// JPanel panel3 = new Panel();

getContentPane().setLayout(newBorderLayout());

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

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

}

publicvoidactionPerformed(ActionEvent e) {

Object target = e.getSource();

String label = e.getActionCommand();

if(target ==reset)

handleReset();

elseif("0123456789.".indexOf(label) > 0)

handleNumber(label);

else

handleOperator(label);

}

// Is the first digit pressed?

booleanisFirstDigit=true;

/**

* Number handling.

*@paramkey the key of the button.

*/

publicvoidhandleNumber(String key) {

if(isFirstDigit)

display.setText(key);

elseif((key.equals(".")) && (display.getText().indexOf(".") < 0))

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

elseif(!key.equals("."))

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

isFirstDigit=false;

}

/**

* Reset the calculator.

*/

publicvoidhandleReset() {

display.setText("0");

isFirstDigit=true;

operator="=";

}

doublenumber= 0.0;

Stringoperator="=";

/**

* Handling the operation.

*@paramkey pressed operator's key.

*/

publicvoidhandleOperator(String key) {

if(operator.equals("+"))

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

elseif(operator.equals("-"))

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

elseif(operator.equals("*"))

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

elseif(operator.equals("/"))

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

elseif(operator.equals("="))

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

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

operator= key;

isFirstDigit=true;

}

publicstaticvoidmain(String[] args) {

newJCalculator();

}

}


運行界面:

Ⅳ 用JAVA編寫一個計算器

importjava.awt.BorderLayout;

importjava.awt.Color;

importjava.awt.GridLayout;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjavax.swing.ImageIcon;

importjavax.swing.JButton;

importjavax.swing.JFrame;

importjavax.swing.JPanel;

importjavax.swing.JTextField;

importjavax.swing.SwingConstants;

{

/**

*

*/

=1L;

Resultresult=newResult();//定義text的面板

Number_Keynumber_key=newNumber_Key();//定義按鈕面板

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

booleancom=false;

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

inti=0;

//存放text的內容

Stringtext="";

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

doubledefbutton=0;

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

intsymbol=0;

//構造函數

Jisuanqi(){

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

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

JPanelpane=newJPanel();//定義主面板

pane.setLayout(newBorderLayout());

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

//替換圖標

ImageIconicon=newImageIcon("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);//對1按鈕添加監聽事件

number_key.two.addActionListener(this);//對2按鈕添加監聽事件

number_key.three.addActionListener(this);//對3按鈕添加監聽事件

number_key.four.addActionListener(this);//對4按鈕添加監聽事件

number_key.five.addActionListener(this);//對5按鈕添加監聽事件

number_key.six.addActionListener(this);//對6按鈕添加監聽事件

number_key.seven.addActionListener(this);//對7按鈕添加監聽事件

number_key.eight.addActionListener(this);//對8按鈕添加監聽事件

number_key.nine.addActionListener(this);//對9按鈕添加監聽事件

number_key.zero.addActionListener(this);//對0按鈕添加監聽事件

number_key.ce.addActionListener(this);//對置零按鈕添加監聽事件

number_key.plus.addActionListener(this);//對+按鈕添加監聽事件

number_key.equal.addActionListener(this);//對=按鈕添加監聽事件

number_key.sub.addActionListener(this);//對-按鈕添加監聽事件

number_key.mul.addActionListener(this);//對*按鈕添加監聽事件

number_key.div.addActionListener(this);//對/按鈕添加監聽事件

number_key.point.addActionListener(this);//對.按鈕添加監聽事件

setContentPane(pane);

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

}

//各個按鈕觸發的事件

publicvoidactionPerformed(ActionEvente){

/*

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

*false;或者是否點擊數字鍵,如果是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");

}

}elseif(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");

}

}elseif(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");

}

}elseif(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");

}

}elseif(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");

}

}elseif(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");

}

}elseif(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");

}

}elseif(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");

}

}elseif(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");

}

}

/*

*對於0這個按鈕有一定的說法,在我的程序里不會出現如00000這樣的情況,我加了判斷條件就是

*如果text中的數值=0就要判斷在這個數值中是否有.存在?如果有那麼就在原來數值基礎之上添加0;否則保持原來的數值不變

*/

elseif(e.getSource()==number_key.zero){//result.text.getText()是得到text里內容的意思

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)就是類型轉換了,下面都是一樣

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

}else{

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

result.text.setText(text);

}else{

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

}

}

}

}elseif(e.getSource()==number_key.ce){

result.text.setText("0");

i=0;

com=true;

//text="";

defbutton=0;

}

/*

*本程序不會讓一個數值中出現2個以上的小數點.具體做法是:判斷是否已經存在.存在就不添加,不存在就添加.

*/

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

}

}

}//獲得點擊+之前的數值

elseif(e.getSource()==number_key.plus){

com=true;

i=0;

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

symbol=1;

}//獲得點擊-之前的數值

elseif(e.getSource()==number_key.sub){

com=true;

i=0;

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

symbol=2;

}//獲得點擊*之前的數值

elseif(e.getSource()==number_key.mul){

com=true;

i=0;

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

System.out.println(defbutton);

symbol=3;

}//獲得點擊/之前的數值

elseif(e.getSource()==number_key.div){

com=true;

i=0;

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

symbol=4;

}elseif(e.getSource()==number_key.equal){

switch(symbol){

case1:{//計算加法

doublead=defbutton

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

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

i=0;

text="";

break;

}

case2:{//計算減法

doublead=defbutton

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

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

i=0;

text="";

break;

}

case3:{//計算乘法

doublead=defbutton

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

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

i=0;

text="";

break;

}

case4:{//計算除法

doublead=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")

publicstaticvoidmain(String[]args){

Jisuanqiloveyou=newJisuanqi();

loveyou.show();

}

}

//計算器數字按鈕定義面板

classNumber_KeyextendsJPanel{

/**

*

*/

=1L;

JButtonzero=newJButton("0");//數字鍵0

JButtonone=newJButton("1");//數字鍵1

JButtontwo=newJButton("2");//數字鍵2

JButtonthree=newJButton("3");//數字鍵3

JButtonfour=newJButton("4");//數字鍵4

JButtonfive=newJButton("5");//數字鍵5

JButtonsix=newJButton("6");//數字鍵6

JButtonseven=newJButton("7");//數字鍵7

JButtoneight=newJButton("8");//數字鍵8

JButtonnine=newJButton("9");//數字鍵9

JButtonplus=newJButton("+");

JButtonsub=newJButton("-");

JButtonmul=newJButton("*");

JButtondiv=newJButton("/");

JButtonequal=newJButton("=");

JButtonce=newJButton("清零");//置零鍵

JButtonpoint=newJButton(".");

Number_Key(){

setLayout(newGridLayout(4,4,1,1));//定義布局管理器為網格布局

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

//添加按鈕

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

}

}

//計算器顯示結果的窗體

classResultextendsJPanel{

/**

*

*/

=1L;

//text先是輸入和結果

JTextFieldtext=newJTextField("0");

@SuppressWarnings("deprecation")

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

text.setHorizontalAlignment(SwingConstants.RIGHT);

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

setLayout(newBorderLayout());//設定布局管理器邊框布局

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

}

}

直接復制保存成Jisuanqi.java可以直接運行了

熱點內容
江蘇雲伺服器散熱器定製 發布:2024-11-05 18:50:15 瀏覽:718
投資分紅網站源碼 發布:2024-11-05 18:36:38 瀏覽:502
我的世界推薦適合萌新的伺服器 發布:2024-11-05 18:30:03 瀏覽:576
c語言考級 發布:2024-11-05 18:16:54 瀏覽:503
易語言不能編譯安卓app嗎 發布:2024-11-05 18:14:11 瀏覽:835
c語言重點知識 發布:2024-11-05 18:04:53 瀏覽:640
演算法分家 發布:2024-11-05 17:55:39 瀏覽:469
手游雲頂怎麼確定伺服器 發布:2024-11-05 17:52:03 瀏覽:777
天籟選哪個配置2021 發布:2024-11-05 17:50:26 瀏覽:812
rust怎麼找伺服器 發布:2024-11-05 17:49:45 瀏覽:487