import?java.awt.*;
10多年專注成都網(wǎng)站制作,企業(yè)網(wǎng)站制作,個人網(wǎng)站制作服務,為大家分享網(wǎng)站制作知識、方案,網(wǎng)站設計流程、步驟,成功服務上千家企業(yè)。為您提供網(wǎng)站建設,網(wǎng)站制作,網(wǎng)頁設計及定制高端網(wǎng)站建設服務,專注于企業(yè)網(wǎng)站制作,高端網(wǎng)頁制作,對成都樓梯護欄等多個領域,擁有豐富的網(wǎng)站維護經驗。
import?java.awt.event.*;
import?javax.swing.*;
import?javax.swing.border.*;
import?java.util.LinkedList;?//工具包
import?java.text.NumberFormat;?//文本包
/**
*?java?swing計算器
*?@author?young
*
*/
public?class?Calculator?extends?Frame?implements?ActionListener?//?計算器類
{
JTextField?result;
NumberButton?numberButton[];
OperatorButton?operatorButton[];
Button?radixpoint,?positiveminus,?backspace,?reciprocal,?equal,?clear;?//?聲明成員變量
//?小數(shù)點按鈕,正負號按鈕,退格按鈕,求倒數(shù)按鈕,等號按鈕,清零按鈕
Panel?panel;
String?operator[]?=?{?"+",?"-",?"*",?"/"?};
LinkedList?linklist;
boolean?pressequal?=?false;
public?Calculator()?//?構造方法
{
super("計算器");
linklist?=?new?LinkedList();
numberButton?=?new?NumberButton[10];
for?(int?i?=?0;?i?=?9;?i++)?{
numberButton[i]?=?new?NumberButton(i);
numberButton[i].addActionListener(this);
}
operatorButton?=?new?OperatorButton[4];
for?(int?i?=?0;?i??4;?i++)?{
operatorButton[i]?=?new?OperatorButton(operator[i]);
operatorButton[i].addActionListener(this);
}
radixpoint?=?new?Button(".");
positiveminus?=?new?Button("+/-");
backspace?=?new?Button("CE");
reciprocal?=?new?Button("1/x");
equal?=?new?Button("=");
clear?=?new?Button("C");
radixpoint.setForeground(Color.red);
positiveminus.setForeground(Color.red);
backspace.setForeground(Color.red);
reciprocal.setForeground(Color.red);
equal.setForeground(Color.red);
clear.setForeground(Color.red);
radixpoint.addActionListener(this);
positiveminus.addActionListener(this);
backspace.addActionListener(this);
reciprocal.addActionListener(this);
equal.addActionListener(this);
clear.addActionListener(this);
result?=?new?JTextField(10);
result.setHorizontalAlignment(JTextField.RIGHT);
result.setForeground(Color.black);
result.setBackground(Color.white);
result.setFont(new?Font("TimesRoman",?Font.PLAIN,?14));
result.setBorder(new?SoftBevelBorder(BevelBorder.LOWERED));
result.setEditable(false);
panel?=?new?Panel();
panel.setLayout(new?GridLayout(4,?5));
panel.add(numberButton[1]);
panel.add(numberButton[2]);
panel.add(numberButton[3]);
panel.add(backspace);
panel.add(clear);
panel.add(numberButton[4]);
panel.add(numberButton[5]);
panel.add(numberButton[6]);
panel.add(operatorButton[0]);
panel.add(operatorButton[2]);
panel.add(numberButton[7]);
panel.add(numberButton[8]);
panel.add(numberButton[9]);
panel.add(operatorButton[1]);
panel.add(operatorButton[3]);
panel.add(numberButton[0]);
panel.add(positiveminus);
panel.add(reciprocal);
panel.add(radixpoint);
panel.add(equal);
add(result,?"North");
add(panel,?"Center");
addWindowListener(new?WindowAdapter()?{
public?void?windowClosing(WindowEvent?e)?{
System.exit(0);
}
});
setSize(270,?200);
setLocation(300,?230);
setVisible(true);
}
public?void?actionPerformed(ActionEvent?e)?//?按鈕的單擊事件處理方法
{
if?(e.getSource()?instanceof?NumberButton)?//?數(shù)字按鈕
{
NumberButton?b?=?(NumberButton)?e.getSource();
if?(linklist.size()?==?0)?{
int?number?=?b.getNumber();
linklist.add(""?+?number);
result.setText(""?+?number);
pressequal?=?false;
}
else?if?(linklist.size()?==?1??pressequal?==?false)?{
int?number?=?b.getNumber();
String?num?=?(String)?linklist.getFirst();
String?s?=?num.concat(""?+?number);
linklist.set(0,?s);
result.setText(s);
}?else?if?(linklist.size()?==?1??pressequal?==?true)?{
int?number?=?b.getNumber();
linklist.removeFirst();
linklist.add(""?+?number);
pressequal?=?false;
result.setText(""?+?number);
}?else?if?(linklist.size()?==?2)?{
int?number?=?b.getNumber();
linklist.add(""?+?number);
result.setText(""?+?number);
}?else?if?(linklist.size()?==?3)?{
int?number?=?b.getNumber();
String?num?=?(String)?linklist.getLast();
String?s?=?num.concat(""?+?number);
linklist.set(2,?s);
result.setText(s);
}
}?else?if?(e.getSource()?instanceof?OperatorButton)?//?操作按鈕
{
OperatorButton?b?=?(OperatorButton)?e.getSource();
if?(linklist.size()?==?1)?{
String?fuhao?=?b.getOperator();
linklist.add(fuhao);
}?else?if?(linklist.size()?==?2)?{
String?fuhao?=?b.getOperator();
linklist.set(1,?fuhao);
}?else?if?(linklist.size()?==?3)?{
String?fuhao?=?b.getOperator();
String?number1?=?(String)?linklist.getFirst();
String?number2?=?(String)?linklist.getLast();
String?operator?=?(String)?linklist.get(1);
try?{
double?n1?=?Double.parseDouble(number1);
double?n2?=?Double.parseDouble(number2);
double?n?=?0;
if?(operator.equals("+"))?{
n?=?n1?+?n2;
}?else?if?(operator.equals("-"))?{
n?=?n1?-?n2;
}?else?if?(operator.equals("*"))?{
n?=?n1?*?n2;
}?else?if?(operator.equals("/"))?{
n?=?n1?/?n2;
}
linklist.clear();
linklist.add(""?+?n);
linklist.add(fuhao);
result.setText(""?+?n);
}?catch?(Exception?ee)?{
}
}
}?else?if?(e.getSource()?==?equal)?//?等號按鈕
{
pressequal?=?true;
if?(linklist.size()?==?1?||?linklist.size()?==?2)?{
String?num?=?(String)?linklist.getFirst();
result.setText(""?+?num);
}?else?if?(linklist.size()?==?3)?{
String?number1?=?(String)?linklist.getFirst();
String?number2?=?(String)?linklist.getLast();
String?operator?=?(String)?linklist.get(1);
try?{
double?n1?=?Double.parseDouble(number1);
double?n2?=?Double.parseDouble(number2);
double?n?=?0;
if?(operator.equals("+"))?{
n?=?n1?+?n2;
}?else?if?(operator.equals("-"))?{
n?=?n1?-?n2;
}?else?if?(operator.equals("*"))?{
n?=?n1?*?n2;
}?else?if?(operator.equals("/"))?{
n?=?n1?/?n2;
}
result.setText(""?+?n);
linklist.set(0,?""?+?n);
linklist.removeLast();
linklist.removeLast();
}?catch?(Exception?ee)?{
}
}
}?else?if?(e.getSource()?==?radixpoint)?//?小數(shù)點按鈕
{
if?(linklist.size()?==?0)?{
pressequal?=?false;
}?else?if?(linklist.size()?==?1)?{
String?dot?=?radixpoint.getLabel();
String?num?=?(String)?linklist.getFirst();
String?s?=?null;
if?(num.indexOf(dot)?==?-1)?{
s?=?num.concat(dot);
linklist.set(0,?s);
}?else?{
s?=?num;
}
linklist.set(0,?s);
result.setText(s);
}
else?if?(linklist.size()?==?3)?{
String?dot?=?radixpoint.getLabel();
String?num?=?(String)?linklist.getLast();
String?s?=?null;
if?(num.indexOf(dot)?==?-1)?{
s?=?num.concat(dot);
linklist.set(2,?s);
}?else?{
s?=?num;
}
result.setText(s);
}
}?else?if?(e.getSource()?==?backspace)?//?退格按鈕
{
if?(linklist.size()?==?1)?{
String?num?=?(String)?linklist.getFirst();
if?(num.length()?=?1)?{
num?=?num.substring(0,?num.length()?-?1);
linklist.set(0,?num);
result.setText(num);
}?else?{
linklist.removeLast();
result.setText("0");
}
}?else?if?(linklist.size()?==?3)?{
String?num?=?(String)?linklist.getLast();
if?(num.length()?=?1)?{
num?=?num.substring(0,?num.length()?-?1);
linklist.set(2,?num);
result.setText(num);
}?else?{
linklist.removeLast();
result.setText("0");
}
}
}?else?if?(e.getSource()?==?positiveminus)?//?正負號按鈕
{
if?(linklist.size()?==?1)?{
String?number1?=?(String)?linklist.getFirst();
try?{
double?d?=?Double.parseDouble(number1);
d?=?-1?*?d;
String?str?=?String.valueOf(d);
linklist.set(0,?str);
result.setText(str);
}?catch?(Exception?ee)?{
}
}?else?if?(linklist.size()?==?3)?{
String?number2?=?(String)?linklist.getLast();
try?{
double?d?=?Double.parseDouble(number2);
d?=?-1?*?d;
String?str?=?String.valueOf(d);
linklist.set(2,?str);
result.setText(str);
}?catch?(Exception?ee)?{
}
}
}?else?if?(e.getSource()?==?reciprocal)?//?求倒數(shù)按鈕
{
if?(linklist.size()?==?1?||?linklist.size()?==?2)?{
String?number1?=?(String)?linklist.getFirst();
try?{
double?d?=?Double.parseDouble(number1);
d?=?1.0?/?d;
String?str?=?String.valueOf(d);
linklist.set(0,?str);
result.setText(str);
}?catch?(Exception?ee)?{
}
}?else?if?(linklist.size()?==?3)?{
String?number2?=?(String)?linklist.getLast();
try?{
double?d?=?Double.parseDouble(number2);
d?=?1.0?/?d;
String?str?=?String.valueOf(d);
linklist.set(0,?str);
result.setText(str);
}?catch?(Exception?ee)?{
}
}
}?else?if?(e.getSource()?==?clear)?//?清零按鈕
{
pressequal?=?false;
result.setText("0");
linklist.clear();
}
}
public?static?void?main(String?args[])?{
new?Calculator();
}
}
class?NumberButton?extends?Button?//?數(shù)字按鈕類
{
int?number;
public?NumberButton(int?number)?//?構造方法
{
super(""?+?number);
this.number?=?number;
setForeground(Color.blue);
}
public?int?getNumber()?{
return?number;
}
}
class?OperatorButton?extends?Button?//?運算符號按鈕類
{
String?operator;
public?OperatorButton(String?operator)?//?構造方法
{
super(operator);
this.operator?=?operator;
setForeground(Color.red);
}
public?String?getOperator()?{
return?operator;
}
}
跟小數(shù)沒有關系啊,+ - * /
把2個數(shù)都設置成double類型的,
或者float類型的就可以了。
如果我理解的不對,請指出。
簡單寫了下,代碼如下請參照:
/**
*?計算器類
*?
*?@author?Administrator
*
*/
public?class?Calculator?extends?JFrame?implements?ActionListener?{
private?static?final?long?serialVersionUID?=?3868243398506940702L;
//?文本框
private?JTextField?result;
//?按鈕數(shù)組
private?JButton[]?buttons;
//?按鈕文本
private?final?String[]?characters?=?{?"7",?"8",?"9",?"/",?"4",?"5",?"6",
"*",?"1",?"2",?"3",?"-",?"0",?".",?"=",?"+"?};
//?是否為第一個輸入的數(shù)字
private?boolean?isFirstDigit?=?true;
//?運算結果
private?double?resultNum?=?0.0;
//?運算符
private?String?operator?=?"=";
public?Calculator(String?title)?{
//?設置標題欄
super(title);
//?初始化各組件
init();
//?注冊各組件監(jiān)聽器
registerListener();
//?顯示窗體
setVisible(true);
}
/**
*?初始化各組件
*/
private?void?init()?{
//?常用屬性初始化
setSize(220,?200);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
/*?文本框對象初始化?*/
result?=?new?JTextField("0");
//?文本右對齊
result.setHorizontalAlignment(JTextField.RIGHT);
//?設置是否可編輯
result.setEditable(false);
/*?按鈕初始化?*/
buttons?=?new?JButton[characters.length];
for?(int?i?=?0;?i??buttons.length;?i++)?{
buttons[i]?=?new?JButton(characters[i]);
buttons[i].setFocusable(false);?//?不允許按鈕定位焦點
}
/*?將文本框與按鈕添加到窗體中?*/
add(result,?BorderLayout.NORTH);
JPanel?pnl?=?new?JPanel(new?GridLayout(4,?4,?5,?5));
for?(JButton?jButton?:?buttons)?{
pnl.add(jButton);
}
add(pnl);
this.getContentPane().setFocusable(true);
}
/**
*?注冊監(jiān)聽器
*/
private?void?registerListener()?{
for?(JButton?jButton?:?buttons)?{
jButton.addActionListener(this);
}
//?注冊鍵盤事件
this.getContentPane().addKeyListener(new?KeyAdapter()?{
@Override
public?void?keyPressed(KeyEvent?e)?{
String?text?=?String.valueOf(e.getKeyChar());
if?(Character.isDigit(text.charAt(0))?||?".".equals(text))?{?//?數(shù)字或小數(shù)點
handleNumber(text);
}?else?if?("+-*/=".indexOf(text)?!=?-1)?{?//?運算符
handleOperator(text);
}?else?if?(e.getKeyCode()?==?8)?{?//?退格鍵
String?tmp?=?result.getText().trim();
if?(tmp.length()?==?1)?{
result.setText("0");
isFirstDigit?=?true;
}?else?{
result.setText(tmp.substring(0,?tmp.length()?-?1));
}
}
}
});
}
@Override
public?void?actionPerformed(ActionEvent?e)?{
JButton?btn?=?(JButton)?e.getSource();
String?text?=?btn.getText().trim();
if?(Character.isDigit(text.charAt(0))?||?".".equals(text))?{?//?處理數(shù)字和小數(shù)點
handleNumber(text);
}?else?{?//?處理運算符
handleOperator(text);
}
}
/**
*?處理數(shù)字和小數(shù)點
*?
*?@param?text
*/
private?void?handleNumber(String?text)?{
if?(isFirstDigit)?{?//?第一次輸入
if?(".".equals(text))?{
this.result.setText("0.");
}?else?{
this.result.setText(text);
}
}?else?if?("0".equals(text)??"0".equals(this.result.getText()))?{
isFirstDigit?=?true;
return;
}?else?if?(".".equals(text)??this.result.getText().indexOf(".")?==?-1)?{
this.result.setText(this.result.getText()?+?".");
}?else?if?(!".".equals(text))?{
this.result.setText(this.result.getText()?+?text);
}
isFirstDigit?=?false;
}
/**
*?處理運算符
*?
*?@param?text
*/
private?void?handleOperator(String?text)?{
switch?(operator)?{?//?處理各項運算??適用于JDK1.7版本的
case?"+":
resultNum?+=?Double.parseDouble(this.result.getText());
break;
case?"-":
resultNum?-=?Double.parseDouble(this.result.getText());
break;
case?"*":
resultNum?*=?Double.parseDouble(this.result.getText());
break;
case?"/":
resultNum?/=?Double.parseDouble(this.result.getText());
break;
case?"=":
resultNum?=?Double.parseDouble(this.result.getText());
break;
}
//?將文本框的值修改為運算結果
this.result.setText(String.valueOf(resultNum));
//?將點擊的運算符放入operator保存
operator?=?text;
//?下一個數(shù)字第一次點擊
isFirstDigit?=?true;
}
public?static?void?main(String[]?args)?{
?new?Calculator("My?Calculator");
?}
}
運行結果如下:
if(temp==".")//判斷只能輸入一個小數(shù),這個放在actionPerformed里的\x0d\x0a{\x0d\x0a for(int i=0;i
回答于?2022-11-16