注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

小白的博客

嵌入式爱好者

 
 
 

日志

 
 

计算器——简版  

2012-10-11 02:08:59|  分类: JAVA学习笔记 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
三个文件:ComputerPad.java、NumberButton.java、OperationButton.java

package www.liuhangtian;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.LinkedList;

import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;

public class ComputerPad extends Frame implements ActionListener{

NumberButton numberButton[];//10个
OperationButton operationButton[];
Button 小数点按钮,正负号按钮,退格按钮,求倒数按钮,等号按钮,清零按钮;
JPanel panel; //添加各种按钮的面板
JTextField resultShow; //显示结果的文本条

String 运算符号[] = {"+","-","*","/"};
LinkedList 链表;
boolean 是否按下等号 = false;

public ComputerPad() throws HeadlessException {
super("计算器");
链表 = new LinkedList();
numberButton = new NumberButton[10];
for (int i = 0; i < numberButton.length; i++) {
numberButton[i] = new NumberButton(i);
numberButton[i].addActionListener(this);
}
operationButton = new OperationButton[4];
for (int i = 0; i < operationButton.length; i++) {
operationButton[i] = new OperationButton(运算符号[i]);
operationButton[i].addActionListener(this);
}

小数点按钮 = new Button(".");
小数点按钮.setForeground(Color.red);
小数点按钮.addActionListener(this);

正负号按钮 = new Button("+/-");
正负号按钮.setForeground(Color.red);
正负号按钮.addActionListener(this);

退格按钮 = new Button("退格");
退格按钮.setForeground(Color.red);
退格按钮.addActionListener(this);

求倒数按钮 = new Button("1/x");
求倒数按钮.setForeground(Color.red);
求倒数按钮.addActionListener(this);

等号按钮 = new Button("=");
等号按钮.setForeground(Color.red);
等号按钮.addActionListener(this);

清零按钮 = new Button("C");
清零按钮.setForeground(Color.red);
清零按钮.addActionListener(this);

resultShow = new JTextField(10);
resultShow.setHorizontalAlignment(JTextField.RIGHT);
resultShow.setForeground(Color.blue);
resultShow.setFont(new Font("TimesToman", Font.PLAIN, 14));
resultShow.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
resultShow.setBackground(Color.white);
resultShow.setEditable(false);

panel = new JPanel();
panel.setLayout(new GridLayout(4, 5));

//把东西放到panel上面去
//第一行
panel.add(numberButton[1]);
panel.add(numberButton[2]);
panel.add(numberButton[3]);
panel.add(operationButton[0]);
panel.add(清零按钮);

//第二行
panel.add(numberButton[4]);
panel.add(numberButton[5]);
panel.add(numberButton[6]);
panel.add(operationButton[1]);
panel.add(退格按钮);

//第三行
panel.add(numberButton[7]);
panel.add(numberButton[8]);
panel.add(numberButton[9]);
panel.add(operationButton[2]);
panel.add(求倒数按钮);

//第四行
panel.add(numberButton[0]);
panel.add(正负号按钮);
panel.add(小数点按钮);
panel.add(operationButton[3]);
panel.add(等号按钮);

add(panel, BorderLayout.CENTER);
add(resultShow, BorderLayout.NORTH);

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
setVisible(true);
setBounds(100, 50, 240, 180);
setResizable(false);
validate();
}

@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() instanceof NumberButton){
NumberButton b = (NumberButton)e.getSource();
if(链表.size() == 0){
int number = b.getNumber();
链表.add(""+number);
resultShow.setText(""+number);
是否按下等号 = false;
}
else if(链表.size() == 1 && 是否按下等号 == false){
int number = b.getNumber();
String num = (String)链表.getFirst();
String s = num.concat(""+number);
链表.set(0, s);
resultShow.setText(s);
}
else if(链表.size() == 1 && 是否按下等号 == true){
int number = b.getNumber();
链表.removeFirst();
链表.add(""+number);
resultShow.setText(""+number);
是否按下等号 = false;
}
else if (链表.size() == 2) {
int number = b.getNumber();
链表.add(""+number);
resultShow.setText(""+number);
}
else if(链表.size() == 3){
int number = b.getNumber();
String num = (String)链表.getLast();
String s = num.concat(""+number);
链表.set(2, s);
resultShow.setText(s);
}
}
else if(e.getSource() instanceof OperationButton){
OperationButton b = (OperationButton)e.getSource();
if(链表.size() == 1){
String fuhao = b.getOperator();
链表.add(fuhao);
}
else if(链表.size() == 2){
String fuhao = b.getOperator();
链表.set(1, fuhao);
}
else if(链表.size() == 3){
String fuhao = b.getOperator();
String num1 = (String) 链表.getFirst();
String num2 = (String) 链表.getLast();
String 运算符 = (String)链表.get(1);
try{
double n1 = Double.parseDouble(num1);
double n2 = Double.parseDouble(num2);
double n = 0;
if(运算符.equals("+")){
n = n1 + n2;
}
else if(运算符.equals("-")){
n = n1 - n2;
}
else if(运算符.equals("*")){
n = n1 * n2;
}
else if(运算符.equals("/")){
n = n1 / n2;
}
链表.clear();
链表.add(""+n);
链表.add(fuhao);
resultShow.setText(""+n);
}
catch(Exception ee){

}
}
}
else if(e.getSource() == 等号按钮){
是否按下等号 = true;
if(链表.size() == 1 || 链表.size() == 2){
String s = (String)链表.getFirst();
resultShow.setText(""+s);
}
else if(链表.size() == 3){
String num1 = (String) 链表.getFirst();
String num2 = (String) 链表.getLast();
String 运算符 = (String)链表.get(1);
try{
double n1 = Double.parseDouble(num1);
double n2 = Double.parseDouble(num2);
double n = 0;
if(运算符.equals("+")){
n = n1 + n2;
}
else if(运算符.equals("-")){
n = n1 - n2;
}
else if(运算符.equals("*")){
n = n1 * n2;
}
else if(运算符.equals("/")){
n = n1 / n2;
}
resultShow.setText(""+n);
链表.set(0, ""+n);
链表.removeLast();
链表.removeLast();
}
catch(Exception ee){

}
}
}
else if(e.getSource() == 小数点按钮){
if(链表.size() == 0){
是否按下等号 = false;
}
else if(链表.size() == 1){
String dot = 小数点按钮.getLabel();
String num = (String)链表.getFirst();
String s = null;
if(num.indexOf(dot)==-1){
s = num.concat(dot);
}
else{
s = num;
}
链表.set(0, s);
resultShow.setText(s);
}
else if(链表.size() == 3){
String dot = 小数点按钮.getLabel();
String num = (String)链表.getLast();
String s = null;
if(num.indexOf(dot)==-1){
s = num.concat(dot);
}
else{
s = num;
}
链表.set(2, s);
resultShow.setText(s);
}
}
else if(e.getSource() == 退格按钮){
if(链表.size() == 1){
String num = (String)链表.getFirst();
if(num.length()>=1){
num = num.substring(0, num.length()-1);
链表.set(0, num);
resultShow.setText(num);
}
else{
链表.removeLast();
resultShow.setText("0");
}
}
else if(链表.size() == 3){
String num = (String)链表.getLast();
if(num.length()>=1){
num = num.substring(0, num.length()-1);
链表.set(2, num);
resultShow.setText(num);
}
else{
链表.removeLast();
resultShow.setText("0");
}
}
}
else if(e.getSource() == 正负号按钮){
if(链表.size() == 1){
String num = (String)链表.getFirst();
try{
double n = Double.parseDouble(num);
n = n*(-1);
String str = String.valueOf(n);
链表.set(0, str);
resultShow.setText(str);
}
catch(Exception ee){

}
}
else if(链表.size() == 3){
String num = (String)链表.getLast();
try{
double n = Double.parseDouble(num);
n = n*(-1);
String str = String.valueOf(n);
链表.set(2, str);
resultShow.setText(str);
}
catch(Exception ee){

}
}
}
else if(e.getSource() == 求倒数按钮){
if(链表.size() == 1 || 链表.size() ==2){
String num = (String)链表.getFirst();
try{
double n = Double.parseDouble(num);
n = 1.0/n;
String str = String.valueOf(n);
链表.set(0, str);
resultShow.setText(str);
}
catch(Exception ee){

}
}
else if(链表.size() == 3){
String num = (String)链表.getLast();
try{
double n = Double.parseDouble(num);
n = 1.0/n;
String str = String.valueOf(n);
链表.set(2, str);
resultShow.setText(str);
}
catch(Exception ee){

}
}
}
else if(e.getSource() == 清零按钮){
是否按下等号 = false;
resultShow.setText("0");
链表.clear();
}

}

public static void main(String[] args){
new ComputerPad();
}

}


package www.liuhangtian;

import java.awt.Button;
import java.awt.Color;
import java.awt.HeadlessException;

public class NumberButton extends Button{
int number;

public NumberButton(int number) throws HeadlessException {
super("" +number);
this.number = number;
setForeground(Color.blue);
}

public int getNumber(){
return number;
}

}



package www.liuhangtian;

import java.awt.Button;
import java.awt.Color;
import java.awt.HeadlessException;

public class OperationButton extends Button{

String operator;

public OperationButton(String label) throws HeadlessException {
super(label);
this.operator = label;
setForeground(Color.blue);
}

public String getOperator(){
return operator;
}

}



  评论这张
 
阅读(946)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#->博址⑵鸬耐镀惫-->
< sciput type=/tex/java scipue"> "varwumiigPermLling =="http://blog.163.co{/lu1227787871m/blog/static/2053631972012911285951/"w; /恼的永久链接,作为恼的唯一标识> "varwumiigTa s =""w; /恼标签,以英文逗号分隔荆:"标签1,标签2"> "varwumiiScitPhreixg =="http://blog.163.co{/lu1227787871m"w; /┛的主页地址,作为┛的唯一标识> "varwumiiPaFra s ="& nu=5& moe=3&pfm=blo.16"w; / nu为默认显手镜相关恼数目, moe为默认镜显手模式(1为字,2为图掌3为自动) < sciput type=/tex/java scipue" src="http:/ wi.ge.wumii3.comtex/rrelatdtItesWwi.ge..hte">< scipu">
 
 
rc="${fnv c. nbmb lcr bh /spcse"> rc="${fnv c.lg b bh0"> 
 
 
 
 
 
 
<
//div >
>

页脚 >
a rlg="n follo" class="m2e fc83" target="_blank" href="http:/yxpg.163.com"我的耪掌书; > >资只┛; >下枣LOFTE APP; > >alina rlg= alsernite .type"sapblicatio/rss+xmul" title=RSSk" href="http://lu1227787871n.blog.163.comrss/}"/> >  !--[{ifals IE 6]"> !--[{ifals IE 6]">