導航:首頁 > 編程語言 > java實現gui加減乘除計算器

java實現gui加減乘除計算器

發布時間:2021-02-28 01:29:45

1. java實現有界面的簡單的兩個整數之間的加減乘除運算

這個必須通過swing技術實現,先看下效果圖:

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放置在窗體的中間
}
}

2. 用java代碼實現一個加減乘除運算計算器

現場寫一個:代碼如下
import java.util.Scanner;
public class Jisaunqi {
public static void main(String[] args) {
Double op1;
Double op2;
String op;
Scanner sc = new Scanner(System.in);
System.out.println("請輸入第一個操作數(回車結束)");
op1=sc.nextDouble();
System.out.println("請輸入運算符(回車結束)");
op=sc.next();
System.out.println("請輸入第二個操作數(回車結束)");
op2=sc.nextDouble();
System.out.print("結果是:");
switch(op) {
case "+":System.out.println(op1+op2);break;
case "-":System.out.println(op1-op2);break;
case "*":System.out.println(op1*op2);break;
case "/":System.out.println(op1/op2);break;
}
}
}
運行後,滑鼠點一下底部控制台,按提示輸入數據即可實現加減乘除。

3. 用JAVA實現計算器的GUI程序界面

實現抄基本功能,這個襲功能不就是計算嗎?
至於類圖,你既然是選修java的,那就更加簡單了,直接把類畫出來(類名,屬性,方法),有繼承的就用繼承表示,沒的就算,直接幾個類就是了
另外,調用關系,不就是類A如果有用到類B,就說出來再哪裡用到這樣而已嘛。
代碼注釋,不解釋
截圖運行效果,不用說了

4. 用Java編寫一個加減法計算器要有界面

下面簡單的寫了一個,也可以簡化一下的,你自己看看是否合適:
import java.awt.*;
import java.awt.event.*;
public class count extends WindowAdapter implements ActionListener{
Frame f;
TextField txt1;
TextField txt2;
TextField txt3;
Label lbl1;
Label lbl2;
Label lbl3;
Button btn1;
Button btn2;
Button btn3;
Button btn4;
void init(){
f=new Frame("計算器");
lbl1=new Label("x");
lbl2=new Label("y");
lbl3=new Label("結果");
txt1=new TextField(10);
txt2=new TextField(10);
txt3=new TextField(10);
btn1=new Button(" 求 和 ");
btn2=new Button(" 求 差 ");
btn3=new Button(" 清 除 ");
btn4=new Button(" 退 出 ");

f.setLayout(new FlowLayout());
f.add(lbl1); f.add(txt1);
f.add(lbl2); f.add(txt2);
f.add(lbl3); f.add(txt3);
f.add(btn1);f.add(btn2);f.add(btn3);f.add(btn4);
f.setVisible(true);
f.pack();

btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
f.addWindowListener(this);
}
public void windowClosing(WindowEvent e){
System.exit(0);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==btn1){
String s1=txt1.getText();
String s2=txt2.getText();
double d1=Double.parseDouble(s1);
double d2=Double.parseDouble(s2);
double d3=d1+d2;
String s3=Double.toString(d3);
txt3.setText(s3);
}
if(e.getSource()==btn2){
String s1=txt1.getText();
String s2=txt2.getText();
double d1=Double.parseDouble(s1);
double d2=Double.parseDouble(s2);
double d3=d1-d2;
String s3=Double.toString(d3);
txt3.setText(s3);
}
if(e.getSource()==btn3){
String s="";
txt1.setText(s);
txt2.setText(s);
txt3.setText(s);
}
if(e.getSource()==btn4){
System.exit(0);
}
}
public static void main(String[]args){
new count().init();
}
}

5. 用java編程加減乘除計算器

剛好用設計模式(簡單工廠模式)編寫了一個計算器。

packagecom.medavis.simplefactory.ui;

importjava.util.Scanner;

importcom.medavis.simplefactory.operate.OperatorFactory;

publicclassCaculatorUI{

privatestaticScannersc;

publicstaticvoidmain(String[]args){

try{
System.out.println("Pleaseinputanumber:");

sc=newScanner(System.in);

longfirst=sc.nextLong();

System.out.println("(+-*/):");

sc=newScanner(System.in);

Stringoperator=sc.nextLine();

System.out.println("Pleaseinputanumber:");

sc=newScanner(System.in);

longsecond=sc.nextLong();

OperatorFactoryopf=newOperatorFactory();

System.out.println(first+operator+second+"="+opf.getOperator(operator).getResult(first,second));

}catch(Exceptione){

System.out.println("Yourinputiswrong!");

}
}
}packagecom.medavis.simplefactory.operate;


{

@Override
publiclonggetResult(longfirst,longsecond){

returnfirst+second;

}
}
packagecom.medavis.simplefactory.operate;


{

@Override
publiclonggetResult(longfirst,longsecond){

returnfirst/second;

}
}

packagecom.medavis.simplefactory.operate;


publicclassOperatorFactory{

publicOperatorgetOperator(Stringoperator)throwsException{
switch(operator){
case"+":
returnnewOperatorAdd();
case"-":
returnnewOperatorSub();
case"*":
returnnewOperatorMul();
case"/":
returnnewOperatorDiv();
default:
thrownewException();
}
}

}


packagecom.medavis.simplefactory.operate;


{

@Override
publiclonggetResult(longfirst,longsecond){

returnfirst*second;

}
}

packagecom.medavis.simplefactory.operate;


{

@Override
publiclonggetResult(longfirst,longsecond){

returnfirst-second;

}
}


6. 用Java編寫實現加減乘除,界面如下

用Java編寫的實現加減乘除的程序如下

importjava.awt.BorderLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JButton;
importjavax.swing.JComboBox;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JOptionPane;
importjavax.swing.JPanel;
importjavax.swing.JTextField;
{
JLabeljl1=newJLabel("第一個數");
JLabeljl2=newJLabel("運算符");
JLabeljl3=newJLabel("第二個數");
JLabeljl4=newJLabel("結果:");
JTextFieldjtf1=newJTextField(8);
JTextFieldjtf2=newJTextField(8);
JTextFieldjtf3=newJTextField(8);
Stringa[]={"+","-","*","/"};
JComboBox<String>jcb=newJComboBox<String>(a);
JButtonjb1=newJButton("計算");
JButtonjb2=newJButton("退出");
JPaneljp=newJPanel();
Calculator(){
setTitle("計算器");
jb1.addActionListener(this);
jb2.addActionListener(this);
jp.setLayout(null);
jl1.setBounds(30,30,80,20);
jl2.setBounds(110,30,80,20);
jl3.setBounds(190,30,80,20);
jtf1.setBounds(30,60,70,20);
jcb.setBounds(110,60,70,20);
jtf2.setBounds(190,60,70,20);
jl4.setBounds(80,110,40,20);
jtf3.setBounds(110,110,100,20);
jb1.setBounds(60,160,60,25);
jb2.setBounds(170,160,60,25);
jp.add(jl1);jp.add(jl2);jp.add(jl3);
jp.add(jtf1);jp.add(jcb);jp.add(jtf2);
jp.add(jl4);jp.add(jtf3);
jp.add(jb1);jp.add(jb2);
add(jp,BorderLayout.CENTER);
setSize(300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
publicstaticvoidmain(String[]args){
newCalculator();
}
@Override
publicvoidactionPerformed(ActionEventae){
if(ae.getSource()==jb1){
Stringc=((String)jcb.getSelectedItem()).trim();
if(jtf1.getText().trim().equals("")){
JOptionPane.showMessageDialog(this,"第一個數不能為空");
jtf1.requestFocus();
return;
}
if(jtf2.getText().trim().equals("")){
JOptionPane.showMessageDialog(this,"第二個數不能為空");
jtf2.requestFocus();
return;
}
doublenum1=Double.parseDouble(jtf1.getText().trim());
doublenum2=Double.parseDouble(jtf2.getText().trim());
doublenum3=0;
switch(c){
case"+":num3=num1+num2;break;
case"-":num3=num1-num2;break;
case"*":num3=num1*num2;break;
case"/":
if(num2==0){
JOptionPane.showMessageDialog(this,"除數不能為0");
jtf2.requestFocus();
return;
}else{
num3=num1/num2;
break;
}
}
jtf3.setText(String.valueOf(num3));
}
if(ae.getSource()==jb2){
System.exit(0);
}
}
}

運行結果

7. java中,用布局器實現計算器界面 並實現加減乘除等功能,怎樣編程

java寫的,可行

package ex1;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JTextField;

public class Calcutor extends JFrame {
private JTextField tf;
private JPanel panel1, panel2, panel3, panel4;
private JMenuBar myBar;
private JMenu menu1, menu2, menu3;
private JMenuItem editItem1, editItem2, help1, help2, help3;
private JRadioButtonMenuItem seeItem1, seeItem2;//單選框
private JCheckBoxMenuItem seeItem3;//復選框
private ButtonGroup bgb;
private String back;
private boolean IfResult = true, flag = false;
private String oper = "=";
private double result = 0;
private Num numActionListener;
private DecimalFormat df;

public Calcutor(){
super("科學計算器");//設置標題欄

df = new DecimalFormat("#.####");//保留四位小數

this.setLayout(new BorderLayout(10, 5));
panel1 = new JPanel(new GridLayout(1, 3, 10, 10));
panel2 = new JPanel(new GridLayout(5, 6, 5, 5));//5行6列
panel3 = new JPanel(new GridLayout(5, 1, 5, 5));
panel4 = new JPanel(new BorderLayout(5, 5));

/**
* 菜單欄
*/
myBar = new JMenuBar();
menu1 = new JMenu("編輯(E)");
menu2 = new JMenu("查看(V)");
menu3 = new JMenu("幫助(H)");

menu1.setFont(new Font("宋體", Font.PLAIN, 12));
menu2.setFont(new Font("宋體", Font.PLAIN, 12));
menu3.setFont(new Font("宋體", Font.PLAIN, 12));

/**
* 編輯欄
*/
editItem1 = new JMenuItem("復制(C) Ctrl+C");
editItem2 = new JMenuItem("粘貼(P) Ctrl+V");

editItem1.setFont(new Font("宋體",Font.PLAIN,12));
editItem2.setFont(new Font("宋體",Font.PLAIN,12));

/**
* 查看欄
*/
seeItem1 = new JRadioButtonMenuItem("科學型(T)");
seeItem2 = new JRadioButtonMenuItem("標准型(S)");
seeItem3 = new JCheckBoxMenuItem("數字分組(I)");

seeItem1.setFont(new Font("宋體",Font.PLAIN,12));
seeItem2.setFont(new Font("宋體",Font.PLAIN,12));
seeItem3.setFont(new Font("宋體",Font.PLAIN,12));

/**
* 幫助欄
*/
help1 = new JMenuItem("幫助主題(H)");
help2 = new JMenuItem("關於計算器(A)");

help1.setFont(new Font("宋體",Font.PLAIN,12));
help2.setFont(new Font("宋體",Font.PLAIN,12));

bgb = new ButtonGroup();//選項組

menu1.add(editItem1);
menu1.add(editItem2);

menu2.add(seeItem1);
menu2.add(seeItem2);
menu2.addSeparator();//添加一條分割線
menu2.add(seeItem3);

menu3.add(help1);
menu3.addSeparator();//添加一條分割線
menu3.add(help2);

myBar.add(menu1);
myBar.add(menu2);
myBar.add(menu3);

this.setJMenuBar(myBar);

numActionListener = new Num();//實現數字監聽

/**
* 文本域,即為計算器的屏幕顯示區域
*/
tf = new JTextField();
tf.setEditable(false);//文本區域不可編輯
tf.setBackground(Color.white);//文本區域的背景色
tf.setHorizontalAlignment(JTextField.RIGHT);//文字右對齊
tf.setText("0");
tf.setBorder(BorderFactory.createLoweredBevelBorder());
init();//對計算器進行初始化

}
/**
* 初始化操作
* 添加按鈕
*/
private void init(){
addButton(panel1, "Backspace", new Clear(), Color.red);
addButton(panel1, "CE", new Clear(), Color.red);
addButton(panel1, "C", new Clear(), Color.red);

addButton(panel2, "1/x", new Signs(), Color.magenta);
addButton(panel2, "log", new Signs(), Color.magenta);
addButton(panel2, "7", numActionListener, Color.blue);
addButton(panel2, "8", numActionListener, Color.blue);
addButton(panel2, "9", numActionListener, Color.blue);
addButton(panel2, "÷", new Signs(), Color.red);

addButton(panel2, "n!", new Signs(), Color.magenta);
addButton(panel2, "sqrt", new Signs(), Color.magenta);
addButton(panel2, "4", numActionListener, Color.blue);
addButton(panel2, "5", numActionListener, Color.blue);
addButton(panel2, "6", numActionListener, Color.blue);
addButton(panel2, "×", new Signs(), Color.red);

addButton(panel2, "sin", new Signs(), Color.magenta);
addButton(panel2, "x^2", new Signs(), Color.magenta);
addButton(panel2, "1", numActionListener, Color.blue);
addButton(panel2, "2", numActionListener, Color.blue);
addButton(panel2, "3", numActionListener, Color.blue);
addButton(panel2, "-", new Signs(), Color.red);

addButton(panel2, "cos", new Signs(), Color.magenta);
addButton(panel2, "x^3", new Signs(), Color.magenta);
addButton(panel2, "0", numActionListener, Color.blue);
addButton(panel2, "-/+", new Clear(), Color.blue);
addButton(panel2, ".", new Dot(), Color.blue);
addButton(panel2, "+", new Signs(), Color.red);

addButton(panel2, "tan", new Signs(), Color.magenta);
addButton(panel2, "%", new Signs(), Color.magenta);
addButton(panel2, "π", numActionListener, Color.orange);
addButton(panel2, "e", numActionListener, Color.orange);
addButton(panel2, "′″", new Signs(), Color.orange);
addButton(panel2, "=", new Signs(), Color.red);

JButton btns = new JButton("計算器");
btns.setBorder(BorderFactory.createLoweredBevelBorder());
btns.setEnabled(false);//按鈕不可操作
btns.setPreferredSize(new Dimension(20, 20));

panel3.add(btns);//加入按鈕
addButton(panel3, "MC", null, Color.red);
addButton(panel3, "MR", null, Color.red);
addButton(panel3, "MS", null, Color.red);
addButton(panel3, "M+", null, Color.red);

panel4.add(panel1, BorderLayout.NORTH);
panel4.add(panel2, BorderLayout.CENTER);
this.add(tf, BorderLayout.NORTH);
this.add(panel3, BorderLayout.WEST);
this.add(panel4);

pack();
this.setResizable(false);//窗口不可改變大小
this.setLocation(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
* 統一設置按鈕的的使用方式
* @param panel
* @param name
* @param action
* @param color
*/
private void addButton(JPanel panel, String name, ActionListener action, Color color){
JButton bt = new JButton(name);
panel.add(bt);//在面板上增加按鈕
bt.setForeground(color);//設置前景(字體)顏色
bt.addActionListener(action);//增加監聽事件
}
/**
* 計算器的基礎操作(+ - × ÷)
* @param x
*/
private void getResult (double x){
if(oper == "+"){result += x;}
else if(oper == "-"){result -= x;}
else if(oper == "×"){result *= x;}
else if(oper == "÷"){result /= x;}
else if(oper == "="){result = x;}
tf.setText(df.format(result));
}
/**
* 運算符號的事件監聽
*/
class Signs implements ActionListener{
public void actionPerformed(ActionEvent e) {
/*
* 用ActionEvent對象的getActionCommand()方法
* 取得與引發事件對象相關的字元串
*/
String str = e.getActionCommand();

/* sqrt求平方根 */
if(str.equals("sqrt")){
double i = Double.parseDouble(tf.getText());
if(i>=0){
/*
* String.valueOf() 轉換為字元串
* df.format() 按要求保留四位小數
* Math.sqrt() 求算數平方根
*/
tf.setText(String.valueOf(df.format(Math.sqrt(i))));
}
else{
tf.setText("負數不能開平方根");
}
}

/* log求常用對數 */
else if(str.equals("log")){
double i = Double.parseDouble(tf.getText());
if(i>0){
tf.setText(String.valueOf(df.format(Math.log(i))));
}else{
tf.setText("負數不能求對數");
}
}

/* %求百分比 */
else if(str.equals("%")){
tf.setText(df.format(Double.parseDouble(tf.getText()) / 100));
}

/* 1/x求倒數 */
else if(str.equals("1/x")){
if(Double.parseDouble(tf.getText()) == 0){
tf.setText("除數不能為零");
}else{
tf.setText(df.format(1 / Double.parseDouble(tf.getText())));
}
}

/* sin求正弦函數 */
else if(str.equals("sin")){
double i = Double.parseDouble(tf.getText());
tf.setText(String.valueOf(df.format(Math.sin(i))));
}

/* cos求餘弦函數 */
else if(str.equals("cos")){
double i = Double.parseDouble(tf.getText());
tf.setText(String.valueOf(df.format(Math.cos(i))));
}

/* tan求正切函數 */
else if(str.equals("tan")){
double i = Double.parseDouble(tf.getText());
tf.setText(String.valueOf(df.format(Math.tan(i))));
}

/* n!求階乘 */
else if(str.equals("n!")){
double i = Double.parseDouble(tf.getText());
if((i%2==0)||(i%2==1))//判斷為整數放進行階乘操作
{
int j = (int)i;//強制類型轉換
int result=1;
for(int k=1;k<=j;k++)
result *= k;
tf.setText(String.valueOf(result));
}
else
{
tf.setText("無法進行階乘");
}
}

/* x^2求平方 */
else if(str.equals("x^2")){
double i = Double.parseDouble(tf.getText());
tf.setText(String.valueOf(df.format(i*i)));
}

/* x^3求立方 */
else if(str.equals("x^3")){
double i = Double.parseDouble(tf.getText());
tf.setText(String.valueOf(df.format(i*i*i)));
}

/* ′″角度轉換 */
/**
* 將角度值轉換成弧度值,方便三角函數的計算
*/
else if(str.equals("′″")){
double i = Double.parseDouble(tf.getText());
tf.setText(String.valueOf(i/180*Math.PI));
}

else{
if(flag){
IfResult = false;
}
if(IfResult){
oper = str;
}else{
getResult(Double.parseDouble(tf.getText()));
oper = str;
IfResult = true;
}
}
}
}
/**
* 清除按鈕的事件監聽
*/
class Clear implements ActionListener{
public void actionPerformed(ActionEvent e) {
/*
* 用ActionEvent對象的getActionCommand()方法
* 取得與引發事件對象相關的字元串
*/
String str = e.getActionCommand();
if(str == "C"){
tf.setText("0");
IfResult = true;
result = 0;
}else if(str == "-/+"){
double i = 0 - Double.parseDouble(tf.getText().trim());
tf.setText(df.format(i));
}else if(str == "Backspace"){
if(Double.parseDouble(tf.getText()) > 0){
if(tf.getText().length() > 1){
tf.setText(tf.getText().substring(0, tf.getText().length() - 1));
//使用退格刪除最後一位字元
}else{
tf.setText("0");
IfResult = true;
}
}else{
if(tf.getText().length() > 2){
tf.setText(tf.getText().substring(0, tf.getText().length() - 1));
}else{
tf.setText("0");
IfResult = true;
}
}
}else if(str == "CE"){
tf.setText("0");
IfResult = true;
}
}
}
/**
* 數字輸入的事件監聽
*/
class Num implements ActionListener{
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
if(IfResult){
tf.setText("");
IfResult = false;
}
if(str=="π")
{
tf.setText(String.valueOf(Math.PI));
}
else if(str=="e")
{
tf.setText(String.valueOf(Math.E));
}
else{
tf.setText(tf.getText().trim() + str);
if(tf.getText().equals("0")){
tf.setText("0");
IfResult = true;
flag = true;
}
}
}
}
/**
* 小數點的事件監聽
*/
class Dot implements ActionListener{
public void actionPerformed(ActionEvent e) {
IfResult = false;
if(tf.getText().trim().indexOf(".") == -1){
tf.setText(tf.getText() + ".");
}
}
}
/**
* main方法
*/
public static void main(String[] args) {
new Calcutor().setVisible(true);
}
}

8. 用Java設計一個圖形界面(GUI)的計算器應用程序,完成簡單的算術運算

下午發
五分鍾搞定,先給你發個簡單的,照例子自己去做你想要的計算器
package aaa;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class YunSuan implements ActionListener,ItemListener
{
public static JFrame jf;
public static Container c;
public static JTextField jtf1,jtf2,jtf3;
public static JButton jb1;
public static Choice c2;

public static String fuhao="";

public YunSuan()
{
jf=new JFrame("加減乘除運算");
c=jf.getContentPane();
c.setLayout(new FlowLayout());
jtf1=new JTextField(10);
jtf2=new JTextField(10);
jtf3=new JTextField(10);
c2=new Choice();
jb1=new JButton("=");

}

public void Jiemian()
{

c.add(jtf1);
c2.addItemListener(this);
c2.add("");
c2.add("+");
c2.add("-");
c2.add("*");
c2.add("/");
c.add(c2);
c.add(jtf2);
jb1.addActionListener(this);
c.add(jb1);
c.add(jtf3);

jf.setLocation(200,100);
jf.setVisible(true);
jf.pack();
}
public static void main(String args[])
{
YunSuan ys=new YunSuan();
ys.Jiemian();
}

public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
double s1=Integer.parseInt(jtf1.getText());
double s2=Integer.parseInt(jtf2.getText());
double result=0;
if(fuhao.equals("+"))
{
result=s1+s2;
String result2=String.valueOf(result);
jtf3.setText(result2);
}
if(fuhao.equals("-"))
{
result=s1-s2;
String result2=String.valueOf(result);
jtf3.setText(result2);
}
if(fuhao.equals("*"))
{
result=s1*s2;
String result2=String.valueOf(result);
jtf3.setText(result2);
}
if(fuhao.equals("/"))
{
result=s1/s2;
String result2=String.valueOf(result);
jtf3.setText(result2);
}
}

public void itemStateChanged(ItemEvent ie)
{
if(ie.getSource()==c2)
{
String str1=c2.getSelectedItem();
fanhui(str1);
}
}
public String fanhui(String str2)
{
return fuhao=str2;
}
}

9. 用Java編寫一個簡單的計算器界面,並且可以實現加減乘除計算,很簡單一個就可以了,還需要一個歸零按

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class Calculator {

public static void main(String[] args) {
Frame f = new Frame("Calculator");

final TextField tf = new TextField();
Panel p = new Panel();

f.setLayout(new BorderLayout());
f.add(tf, BorderLayout.NORTH);
f.add(p, BorderLayout.CENTER);

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

Button one = new Button("1");
Button two = new Button("2");
Button three = new Button("3");
Button four = new Button("4");
Button five = new Button("5");
Button six = new Button("6");
Button seven = new Button("7");
Button eight = new Button("8");
Button nine = new Button("9");
Button zero = new Button("0");
Button add = new Button("+");
Button sub = new Button("-");
Button mul = new Button("*");
Button div = new Button("/");
Button decimal = new Button(".");
final Button equal = new Button("=");

p.add(one);
p.add(two);
p.add(three);
p.add(four);
p.add(five);
p.add(six);
p.add(seven);
p.add(eight);
p.add(nine);
p.add(zero);
p.add(add);
p.add(sub);
p.add(mul);
p.add(div);
p.add(decimal);
p.add(equal);

final String[] ary = {"", null, ""};

class NumMouseListener implements MouseListener{

public void mouseClicked(MouseEvent e) {
String num = ((Button)e.getSource()).getLabel().trim();
if(ary[1] == null){
ary[0] = ary[0] + "" + num;
}else{
ary[2] = ary[2] + "" + num;
}

tf.setText(tf.getText() + "" + num);
}

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

}

one.addMouseListener(new NumMouseListener());
two.addMouseListener(new NumMouseListener());
three.addMouseListener(new NumMouseListener());
four.addMouseListener(new NumMouseListener());
five.addMouseListener(new NumMouseListener());
six.addMouseListener(new NumMouseListener());
seven.addMouseListener(new NumMouseListener());
eight.addMouseListener(new NumMouseListener());
nine.addMouseListener(new NumMouseListener());
zero.addMouseListener(new NumMouseListener());

class OperatotrMouseListener implements MouseListener{

public void mouseClicked(MouseEvent e) {
if(e.getSource() == equal){
try {
long result = calculate(Integer.parseInt(ary[0]), Integer.parseInt(ary[2]), ary[1]);
tf.setText(String.valueOf(result));

ary[0] = tf.getText();
ary[1] = null;
ary[2] = "";
} catch (Exception e1) {
e1.printStackTrace();
}
}else{
ary[1] = ((Button) e.getSource()).getLabel();

if(ary[0].length() > 0){
ary[2] = "";
}else{
ary[0] = String.valueOf(tf.getText());
}

tf.setText("");
}
}

private long calculate(int i, int j, String operator) throws Exception {
if(operator.equals("+")){
return i + j;
}else if(operator.equals("-")){
return i - j;
}else if(operator.equals("*")){
return i * j;
}else if(operator.equals("/")){
return i / j;
}else{
throw new Exception("Invalid operator.(Only +-/* accepted");
}
}

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

add.addMouseListener(new OperatotrMouseListener());
mul.addMouseListener(new OperatotrMouseListener());
div.addMouseListener(new OperatotrMouseListener());
sub.addMouseListener(new OperatotrMouseListener());
equal.addMouseListener(new OperatotrMouseListener());

f.addWindowListener(new WindowListener(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}

public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
});

f.setVisible(true);
f.setBounds(200, 200, 250, 250);
f.validate();
}

}

10. 用java預言編寫計算器模擬程序,功能要求:該程序顯示GUI用戶界面,能夠實現整數的加、減、乘、除四則運算.

如果是真的,也不是針對你一個人的,驚悚也無計於是呀。
還是繼續干你正在乾的事吧,最好找個搞笑網站,多笑笑吧。

閱讀全文

與java實現gui加減乘除計算器相關的資料

熱點內容
《月亮河》印度電影 瀏覽:871
flashcs5實用案例教程 瀏覽:850
百度貼吧密碼模板 瀏覽:974
食堂管理體系文件包括內容 瀏覽:290
飢荒目錄在哪個文件夾 瀏覽:52
烏魯木齊在哪裡學習編程 瀏覽:431
c語言創建文件夾 瀏覽:874
韓國講述養父與雙胞胎 瀏覽:808
西班牙言情電影 瀏覽:85
a標簽如何直接下載一個文件 瀏覽:777
多女主多鼎爐的小說 瀏覽:531
洪金寶元華元彪越南電影 瀏覽:340
win10ghost好么 瀏覽:207
java怎麼添加滾動條 瀏覽:946
qt生成excel文件 瀏覽:374
如何徹底清除用戶數據 瀏覽:590
假期去看了一場電影英文翻譯 瀏覽:171
香水在哪個網站買 瀏覽:481
學習編程需要掌握哪些英語 瀏覽:510
win10投射到安卓平板 瀏覽:973

友情鏈接