package com.citi.Util;
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比鐘樓網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式鐘樓網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋鐘樓地區(qū)。費用合理售后完善,十余年實體公司更值得信賴。
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/**
* @author yc80549
*
*/
public class Calculator extends JFrame implements ActionListener {
/**
*
*
*/
private static final long serialVersionUID = 1L;
private JTextField expression = new JTextField();
private JPanel buttonContainer = new JPanel();
private JPanel operators = new JPanel();
private JButton b1 = new JButton("1");
private JButton b2 = new JButton("2");
private JButton b3 = new JButton("3");
private JButton b4 = new JButton("4");
private JButton b5 = new JButton("5");
private JButton b6 = new JButton("6");
private JButton b7 = new JButton("7");
private JButton b8 = new JButton("8");
private JButton b9 = new JButton("9");
private JButton b0 = new JButton("0");
private JButton bpoint = new JButton(".");
private JButton bracket1 = new JButton("(");
private JButton bracket2 = new JButton(")");
private JButton plus = new JButton("+");
private JButton minus = new JButton("-");
private JButton multiply = new JButton("*");
private JButton divide = new JButton("/");
private JButton getResult = new JButton("=");
private JButton backSpace = new JButton("Backspace");
private JButton clear = new JButton("C");
private JButton OFF = new JButton("OFF");
private StringCalculate2 cacu = new StringCalculate2();
public Calculator() {
super();
setVisible(true);
this.setLayout(new BorderLayout());
this.setSize(400, 220);
Toolkit tk = Toolkit.getDefaultToolkit();
int width = (int) tk.getScreenSize().getWidth();
int height = (int) tk.getScreenSize().getHeight();
this.setLocation((int) ((width - getSize().getWidth()) / 2),
(int) ((height - getSize().getHeight()) / 2));
this.locate();
this.addAllListener();
}
public void locate() {
buttonContainer.setLayout(new GridLayout(4, 4));
buttonContainer.add(b1);
buttonContainer.add(b2);
buttonContainer.add(b3);
buttonContainer.add(plus);
buttonContainer.add(b4);
buttonContainer.add(b5);
buttonContainer.add(b6);
buttonContainer.add(minus);
buttonContainer.add(b7);
buttonContainer.add(b8);
buttonContainer.add(b9);
buttonContainer.add(multiply);
buttonContainer.add(b0);
buttonContainer.add(bpoint);
buttonContainer.add(getResult);
buttonContainer.add(divide);
operators.add(bracket1);
operators.add(bracket2);
operators.add(backSpace);
operators.add(clear);
operators.add(OFF);
this.add(expression, BorderLayout.NORTH);
this.add(buttonContainer, BorderLayout.CENTER);
this.add(operators, BorderLayout.SOUTH);
}
public void addAllListener() {
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);
plus.addActionListener(this);
multiply.addActionListener(this);
divide.addActionListener(this);
minus.addActionListener(this);
bpoint.addActionListener(this);
bracket1.addActionListener(this);
bracket2.addActionListener(this);
getResult.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
double result = cacu.parse(expression.getText());
expression.setText("" + result);
} catch (Exception e1) {
System.err
.println("invaild expression or RuntimeException");
}
}
});
clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
expression.setText("");
}
});
OFF.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(1);
}
});
backSpace.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
expression.setText(expression.getText().substring(0,
expression.getText().length() - 1));
}
});
}
public void actionPerformed(ActionEvent e) {
JButton source = (JButton) e.getSource();
expression.setText(expression.getText() + source.getText());
}
public static void main(String[] args) {
Calculator cal = new Calculator();
cal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
package com.citi.Util;
import java.util.Stack;
import javax.swing.JOptionPane;
public class StringCalculate2 {
private StackDouble numbers = new StackDouble();
private StackCharacter chs = new StackCharacter();
private boolean compare(char str) {
if (chs.empty()) {
return true;
}
char last = (char) chs.lastElement();
switch (str) {
case '*': {
if (last == '+' || last == '-')
return true;
else
return false;
}
case '/': {
if (last == '+' || last == '-')
return true;
else
return false;
}
case '+':
return false;
case '-':
return false;
}
return true;
}
public Double caculate(String str) {
StringBuffer sb = new StringBuffer(str);
StringBuffer num = new StringBuffer();
String tem = null;
char next;
while (sb.length() 0) {
tem = sb.substring(0, 1);
sb.delete(0, 1);
if (isNum(tem.trim())) {
num.append(tem);
} else {
if (num.length() 0 !"".equals(num.toString().trim())) {
Double d = new Double(num.toString().trim());
numbers.push(d);
num.delete(0, num.length());
}
if (!chs.isEmpty()) {
while (!compare(tem.charAt(0))) {
caculate();
}
}
if (numbers.isEmpty()) {
num.append(tem);
} else {
chs.push(new Character(tem.charAt(0)));
}
next = sb.charAt(0);
if (next == '-') {
num.append(next);
sb.delete(0, 1);
}
}
}
Double d = new Double(num.toString().trim());
numbers.push(d);
while (!chs.isEmpty()) {
caculate();
}
return numbers.pop();
}
public void caculate() {
Double b = numbers.pop();
Double a = null;
a = numbers.pop();
char ope = chs.pop();
Double result = null;
switch (ope) {
case '+':
result = a + b;
numbers.push(result);
break;
case '-':
result = a - b;
numbers.push(result);
break;
case '*':
result = a * b;
numbers.push(result);
break;
case '/':
result = a / b;
numbers.push(result);
break;
}
}
private boolean isNum(String num) {
return num.matches("[0-9.]");
}
public Double parse(String str) {
int start = 0;
StringBuffer sts = new StringBuffer(str);
int end = -1;
while ((end = sts.indexOf(")")) 0) {
String s = sts.substring(start, end + 1);
int first = s.lastIndexOf("(");
Double value = caculate(sts.substring(first + 1, end));
sts.replace(first, end + 1, value.toString());
}
return caculate(sts.toString());
}
public static void main(String[] args) {
String exp = JOptionPane
.showInputDialog("please input expression you want to caculate!");
if (exp.trim().equals("")) {
System.out.println("you have input nothing");
return;
}
StringCalculate2 sc = new StringCalculate2();
JOptionPane.showMessageDialog(null, "the result is: " + sc.parse(exp));
}
}
是用awt和swing做的,很好看比單單用AWT做好看
import java.awt.*;
import javax.swing.*;
public class Conputer extends JFrame {
protected Container con = getContentPane();// 指向內(nèi)容面板
protected JMenuBar a = new JMenuBar();// 菜單條
protected JMenu a1 = new JMenu("編輯(E)");// 菜單1
protected JMenu a2 = new JMenu("查看(V)");// 菜單2
protected JMenu a3 = new JMenu("幫助(H)");// 菜單3
protected JMenuItem a11 = new JMenuItem("復(fù)制(C)", 'C');// 菜單1的菜單項
protected JMenuItem a12 = new JMenuItem("粘貼(P)", 'P');// 菜單1的菜單項
protected JMenuItem a21 = new JMenuItem("標準型(T)", 'T');// 菜單2的菜單項
protected JMenuItem a22 = new JMenuItem("科學(xué)型(S)", 'S');// 菜單2的菜單項
protected JMenuItem a31 = new JMenuItem("幫助主題(H)", 'H');// 菜單3的菜單項
protected JTextField jtf = new JTextField(30);// 文本框
protected JButton[] jb = new JButton[27];
protected String[] arr = { "Backspace", "CE", "C", "MC", "7", "8", "9", "/", "sqrt",
"MR", "4", "5", "6", "*", "%", "MS", "1", "2", "3", "-", "1/x",
"M+", "0", "+/-", ".", "+", "=" };
protected JPanel jp = new JPanel();// 主面板
protected JPanel m = new JPanel();// 次面板
protected JPanel m1 = new JPanel();// 次次面板
protected JPanel m2 = new JPanel();// 次次面板
protected JPanel m3 = new JPanel();// 次次面板
protected JPanel m4 = new JPanel();// 次次面板
protected JPanel m5 = new JPanel();// 次次面板
protected JPanel m6 = new JPanel();// 次次面板
protected GridLayout glo = new GridLayout(6, 1, 3, 3);// 主網(wǎng)格布局
protected GridLayout glo1 = new GridLayout(1, 3, 3, 3);// 次網(wǎng)格布局
protected GridLayout glo2 = new GridLayout(1, 6, 3, 3);// 次網(wǎng)格布局
protected Conputer(String s) {
super(s);
a1.setMnemonic('E');
a1.add(a11);
a1.add(a12);
a2.setMnemonic('V');
a2.add(a21);
a2.add(a22);
a3.setMnemonic('H');
a3.add(a31);
a.add(a1);
a.add(a2);
a.add(a3);
this.setJMenuBar(a);// 菜單條完成
for (int i = 0; i 27; i++) {
jb[i] = new JButton(arr[i]);
}
jp.setLayout(glo);
jp.add(m1);
m1.add(jtf);
jtf.setEditable(false); jtf.setText("0."); jtf.setForeground(Color.BLUE); jtf.setBackground(Color.WHITE);
jtf.setHorizontalAlignment(JTextField.RIGHT);//文本顯示在右邊
jp.add(m2);
m2.setLayout(glo1);
m2.add(jb[0]);
m2.add(jb[1]);
m2.add(jb[2]);
jp.add(m3);
m3.setLayout(glo2);
m3.add(jb[3]);
m3.add(jb[4]);
m3.add(jb[5]);
m3.add(jb[6]);
m3.add(jb[7]);
m3.add(jb[8]);
jp.add(m4);
m4.setLayout(glo2);
m4.add(jb[9]);
m4.add(jb[10]);
m4.add(jb[11]);
m4.add(jb[12]);
m4.add(jb[13]);
m4.add(jb[14]);
jp.add(m5);
m5.setLayout(glo2);
m5.add(jb[15]);
m5.add(jb[16]);
m5.add(jb[17]);
m5.add(jb[18]);
m5.add(jb[19]);
m5.add(jb[20]);
jp.add(m6);
m6.setLayout(glo2);
m6.add(jb[21]);
m6.add(jb[22]);
m6.add(jb[23]);
m6.add(jb[24]);
m6.add(jb[25]);
m6.add(jb[26]);
m.add(jp);
con.add(m);
this.setResizable(false);// 不能用鼠標拉伸窗體
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//可以關(guān)閉窗體
this.setSize(380, 275);
this.setVisible(true);
}
protected Conputer() {
this("計算器");
}
public static void main(String[] args) {
new Conputer("計算器");
}
}
import java.awt.*;
import java.awt.event.*;
public class lvhaiya{
int tmp,sum,sum1,sum2=1,sum3=1;
String a,b,c,d;String s="";
Frame f=new Frame("計算器");
private String[]name={
"0","1","2","3","4","5","6","7","8","9","+","-","*","/","=","空格"
};
public Button[] button=new Button[name.length];
TextField t=new TextField("",30);
Panel p=new Panel();
Panel p1=new Panel();
Color color=new Color(100,170,90);
public lvhaiya(){
p1.setLayout(new GridLayout(5,5));
for(int i=0;iname.length;i++){
button[i]=new Button(name[i]);
p1.add(button[i]);
}
p.setLayout(new FlowLayout(FlowLayout.LEFT));
p.setBackground(color);
p.add(t);
f.add(p,BorderLayout.NORTH);
f.add(p1,BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
f.addWindowListener( new WindowClose());
t.setText("0.");
for(int i=0;iname.length;i++){
button[i].addActionListener(new ButtonEventl());
}
}
public static void main(String[]args){
new lvhaiya();
}
class WindowClose extends WindowAdapter{
public void windouClosing(WindowEvent e){
System.exit(0);
}
}
class ButtonEventl implements ActionListener{
public void actionPerformed(ActionEvent e)throws ArithmeticException{
String command=e.getActionCommand();
if(command.equals("+")){
sum=Integer.parseInt(t.getText())+sum;
t.setText(String.valueOf(sum));
a=t.getText();
s="";
}
else if(command.equals("-")){
sum1=Integer.parseInt(t.getText());
b=t.getText();
s="";
}
else if(command.equals("*")){
sum2=Integer.parseInt(t.getText())*sum2;
t.setText(String.valueOf(sum2));
c=t.getText();
s="";
}
else if(command.equals("/")){
sum3=Integer.parseInt(t.getText());
d=t.getText();
s="";
}
else if(command.equals("=")){
if(a!=null){
t.setText(String.valueOf(sum+tmp));
}
if(b!=null){
t.setText(String.valueOf(sum1-tmp));
}
if(c!=null){
t.setText(String.valueOf(sum2*tmp));
}
try{
if(d!=null){
t.setText(String.valueOf(sum3/tmp));
}
}
catch(ArithmeticException a){
t.setText("除數(shù)不能為零");
}
}
else if(command.equals("空格")){
sum=0;
a=null;
b=null;
c=null;
d=null;
sum1=0;
sum2=1;
sum3=1;
tmp=0;
t.setText("0");
s="";
}
else{
s=s+command;
t.setText(s);
tmp=Integer.parseInt(s);
}
}
}
}
//按鈕可以自己美化一下 希望可以幫到你
我給你找找
package com.bj.calcultor;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calcultor extends Frame implements ActionListener {
public static void main(String[] args) {//定義主方
new Calcultor();//創(chuàng)建匿名對象,并調(diào)用test()方法;
}
//定義按鈕名稱
String[] arr={"1","2","3","4","5","6","7","8","9","0","+","-","*","/","=","."};
JButton [] button=new JButton[arr.length];
JButton reset = new JButton("CE");
JTextField display = new JTextField(20);
//創(chuàng)建窗口,定義組件
//執(zhí)行窗口事件:關(guān)閉窗口
private class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}
public Calcultor(){
super("計算器");//定義標題
//定義面板容器,并布局
JPanel jpanel=new JPanel(new GridLayout(4,4));
//添加按鈕,并給按鈕添加名稱
for (int i = 0; i arr.length; i++) {
button[i]= new JButton(arr[i]);
jpanel.add(button[i]);
}
JPanel panel2 = new JPanel();
panel2.add("Northr", display);
display.setEnabled(false);
panel2.add("East", reset);
this.add("North", panel2);
this.add("Center", jpanel);
for (int i = 0; i arr.length; i++){
addWindowListener(new WindowCloser());
setVisible(true);
setSize(400,400);
pack();
button[i].addActionListener(this);
reset.addActionListener(this);
display.addActionListener(this);
}
}
@Override
public void actionPerformed(ActionEvent e) {//定義事件
// TODO Auto-generated method stub
Object target=e.getSource();
String lable=e.getActionCommand();
if(target==reset){
handleReset();
}else if("0123456789.".indexOf(lable)0){
handleNumber(lable);
}else{
hadleOperator(lable);
}
}
boolean isFirstDigit=true;
private void hadleOperator(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;
}
private void handleNumber(String key) {
if(true){
display.setText(key);//把鍵值設(shè)置為文本框內(nèi)容
}else if(key.equals(".") display.getText().indexOf(".")0){
display.setText(display.getText()+".");//把文本框內(nèi)容設(shè)置:display.getText()+"."
}else if(!key.equals(".")){
display.setText(display.getText() + key);//把文本框內(nèi)容設(shè)置:display.getText()+key
isFirstDigit=false;
}
}
private void handleReset() {
display.setText("0");
isFirstDigit=true;
operator="=";
}
String operator="=";
Double number=0.0;
}
java設(shè)計計算器,主要是使用java swing,對每個按鈕進行監(jiān)聽,實現(xiàn)相關(guān)功能,如下代碼:
package?com.qiu.lin.he;
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)點擊按鈕+、-、*、/時,com?=?true
boolean?com?=?false;
//?當(dāng)i=0時說明是我們第一次輸入,字符串text不會累加
int?i?=?0;
//?存放text的內(nèi)容
String?text?=?"";
//?存放點擊按鈕+、-、*、/之前的數(shù)值
double?defbutton?=?0;
//?+、-、*、/的代號分別為1,2,3,4
int?symbol?=?0;
//?構(gòu)造函數(shù)
Jisuanqi()?{
super("WangJiao");?//?設(shè)定標題
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);?//?設(shè)定關(guān)閉窗體時退出程序
JPanel?pane?=?new?JPanel();?//?定義主面板
pane.setLayout(new?BorderLayout());
setBounds(380,?220,?30,?80);?//?前兩個參數(shù)是在屏幕上顯示的坐標,后兩個是大小
//?替換圖標
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);?//?對1按鈕添加監(jiān)聽事件
number_key.two.addActionListener(this);?//?對2按鈕添加監(jiān)聽事件
number_key.three.addActionListener(this);?//?對3按鈕添加監(jiān)聽事件
number_key.four.addActionListener(this);?//?對4按鈕添加監(jiān)聽事件
number_key.five.addActionListener(this);?//?對5按鈕添加監(jiān)聽事件
number_key.six.addActionListener(this);?//?對6按鈕添加監(jiān)聽事件
number_key.seven.addActionListener(this);?//?對7按鈕添加監(jiān)聽事件
number_key.eight.addActionListener(this);?//?對8按鈕添加監(jiān)聽事件
number_key.nine.addActionListener(this);?//?對9按鈕添加監(jiān)聽事件
number_key.zero.addActionListener(this);?//?對0按鈕添加監(jiān)聽事件
number_key.ce.addActionListener(this);?//?對置零按鈕添加監(jiān)聽事件
number_key.plus.addActionListener(this);?//?對+按鈕添加監(jiān)聽事件
number_key.equal.addActionListener(this);?//?對=按鈕添加監(jiān)聽事件
number_key.sub.addActionListener(this);?//?對-按鈕添加監(jiān)聽事件
number_key.mul.addActionListener(this);?//?對*按鈕添加監(jiān)聽事件
number_key.div.addActionListener(this);?//?對/按鈕添加監(jiān)聽事件
number_key.point.addActionListener(this);?//?對.按鈕添加監(jiān)聽事件
setContentPane(pane);
pack();?//?初始化窗體大小為正好盛放所有按鈕
}
//?各個按鈕觸發(fā)的事件
public?void?actionPerformed(ActionEvent?e)?{
/*
?*?如果是點擊數(shù)字按鈕那么先要判斷是否在此之前點擊了+、-、*、/、=,如果是那么com=true?如果沒有com=
?*?false;或者是否點擊數(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");
}
}
/*
?*?對于0這個按鈕有一定的說法,在我的程序里不會出現(xiàn)如00000這樣的情況,我加了判斷條件就是
?*?如果text中的數(shù)值=0就要判斷在這個數(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;
}
/*
?*?本程序不會讓一個數(shù)值中出現(xiàn)2個以上的小數(shù)點.具體做法是:判斷是否已經(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);
}
}
}?//?獲得點擊+之前的數(shù)值
else?if?(e.getSource()?==?number_key.plus)?{
com?=?true;
i?=?0;
defbutton?=?Double.parseDouble(result.text.getText());
symbol?=?1;
}?//?獲得點擊-之前的數(shù)值
else?if?(e.getSource()?==?number_key.sub)?{
com?=?true;
i?=?0;
defbutton?=?Double.parseDouble(result.text.getText());
symbol?=?2;
}?//?獲得點擊*之前的數(shù)值
else?if?(e.getSource()?==?number_key.mul)?{
com?=?true;
i?=?0;
defbutton?=?Double.parseDouble(result.text.getText());
System.out.println(defbutton);
symbol?=?3;
}?//?獲得點擊/之前的數(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:?{?//?計算加法
double?ad?=?defbutton
+?Double.parseDouble(result.text.getText());
result.text.setText(ad?+?"");
i?=?0;
text?=?"";
break;
}
case?2:?{?//?計算減法
double?ad?=?defbutton
-?Double.parseDouble(result.text.getText());
result.text.setText(String.valueOf(ad));
i?=?0;
text?=?"";
break;
}
case?3:?{?//?計算乘法
double?ad?=?defbutton
*?Double.parseDouble(result.text.getText());
result.text.setText(ad?+?"");
i?=?0;
text?=?"";
break;
}
case?4:?{?//?計算除法
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();
}
}
//?計算器數(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);
}
}
//?計算器顯示結(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放置在窗體的中間
}
}
運行結(jié)果如下: