帮我看看java计算器 我实在是看不出问题在那里

gaosong9002 2009-12-27 09:30:06
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class jisuan extends WindowAdapter{
JTextField text;
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
static double a;
static String s, str;
public jisuan(){
super("计算器");
Container c=getContentPane();
setResizable(false);//让窗口不能调节大小
setLocationRelativeTo(null);
GridBagLayout gbl=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
p3.setLayout(gbl);
text=new JTextField(21);
gbc.fill=GridBagConstraints.NONE;
gbc.anchor=GridBagConstraints.CENTER;
gbl.setConstraints(text,gbc);
p1.add(text,"North");
JButton b1=new JButton("7");
p2.add(b1);
b1.addActionListener(new buttonlistener());
JButton b2=new JButton("8");
p2.add(b2);
b2.addActionListener(new buttonlistener());
JButton b3=new JButton("9");
p2.add(b3);
b3.addActionListener(new buttonlistener());
JButton b4=new JButton("/");
p2.add(b4);
b4.addActionListener(new buttonlistener());
JButton b5=new JButton("4");
p2.add(b5);
b5.addActionListener(new buttonlistener());
JButton b6=new JButton("5");
p2.add(b6);
b6.addActionListener(new buttonlistener());
JButton b7=new JButton("6");
p2.add(b7);
b7.addActionListener(new buttonlistener());
JButton b8=new JButton("*");
p2.add(b8);
b8.addActionListener(new buttonlistener());
JButton b9=new JButton("1");
p2.add(b9);
b9.addActionListener(new buttonlistener());
JButton b10=new JButton("2");
p2.add(b10);
b10.addActionListener(new buttonlistener());
JButton b11=new JButton("3");
p2.add(b11);
b11.addActionListener(new buttonlistener());
JButton b12=new JButton("-");
p2.add(b12);
b12.addActionListener(new buttonlistener());
JButton b13=new JButton("0");
p2.add(b13);
b13.addActionListener(new buttonlistener());
JButton b14=new JButton(".");
p2.add(b14);
b14.addActionListener(new buttonlistener());
JButton b15=new JButton("=");
p2.add(b15);
b15.addActionListener(new buttonlistener());
JButton b16=new JButton("+");
p2.add(b16);
b16.addActionListener(new buttonlistener());
JButton b17=new JButton("清零");
gbc.fill=GridBagConstraints.BOTH;
gbc.weightx=1.0;
gbc.weighty=1.0;
gbl.setConstraints(b17,gbc);
p3.add(b17);
JButton b18=new JButton("结束");
b18.addActionListener(new close());
p3.add(b18);
c.addWindowListener(this);
p2.setLayout(new GridLayout(4,0));
c.add(p1,"North");
c.add(p2,"Center");
c.add(p3,"South");
c.setVisible(true);
text.setEditable(false);//文本框不可编写只能按钮输入
text.setHorizontalAlignment(JTextField.RIGHT);//文本框设置焦点起始位置为又

}
class buttonlistener implements ActionListener{
public void actionPerformed(ActionEvent e){
Button button= (Button) e.getSource();
if(button.getLabel()=="="){
suan();
str=String.valueOf(a);
text.setText(str);
s="";
}else if (button.getLabel()=="+"){
suan();
text.setText("");
s="+";
}else if (button.getLabel()=="-"){
suan();
text.setText("");
s="-";
}else if (button.getLabel()=="/"){
suan();
text.setText("");
s="/";
}else if (button.getLabel()=="*"){
suan();
text.setText("");
s="*";
}else{
text.setText(text.getText()+button.getLabel());
if(button.getLabel()=="清零")
text.setText("");
}
}
public void suan() {
if (s == "+")
a += Double.parseDouble(text.getText());
else if (s == "-")
a -= Double.parseDouble(text.getText());
else if (s == "*")
a *= Double.parseDouble(text.getText());
else if (s == "/")
a /= Double.parseDouble(text.getText());
else
a = Double.parseDouble(text.getText());
}
}
class close implements ActionListener {//退出
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
public static void main(String args[]){
JFrame frame=new jisuan();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(250,230);
frame.setVisible(true);
}
}
...全文
86 14 打赏 收藏 举报
写回复
14 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
小刘子已注册 2009-12-28
  • 打赏
  • 举报
回复
这句“class close implements ActionListener {//退出
public void actionPerformed(ActionEvent e) {
System.exit(0); ”和“frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ”这句的作用是一样的
TigerBiao 2009-12-28
  • 打赏
  • 举报
回复
关注中。。
小刘子已注册 2009-12-28
  • 打赏
  • 举报
回复
这句“JFrame frame=new jisuan();”

class close implements ActionListener {//退出
public void actionPerformed(ActionEvent e) {
System.exit(0);
不善^ 2009-12-27
  • 打赏
  • 举报
回复
报的什么错啊 ?
sean1203 2009-12-27
  • 打赏
  • 举报
回复
SADSDSDDASDASD 2009-12-27
  • 打赏
  • 举报
回复
import java.util.LinkedList;
import javax.swing.*;
import java.awt.event.*;
public class HandleDigit implements ActionListener{
LinkedList<String> list;
JTextField resultShow;
JTextField showComputerProcess;
HandleDigit(LinkedList<String> list,JTextField t1,JTextField t2) {
this.list=list;
resultShow=t1;
showComputerProcess=t2;
}
public void actionPerformed(ActionEvent e){
NumberButton b=(NumberButton)e.getSource();
if(list.size()==0) {
int number=b.getNumber();
list.add(""+number);
resultShow.setText(""+number);
showComputerProcess.setText(""+list.get(0));
}
else if(list.size()==1){
int number=b.getNumber();
String num=list.getFirst();
String s=num.concat(""+number);
list.set(0,s);
resultShow.setText(s);
showComputerProcess.setText(""+list.get(0));
}
else if(list.size()==2){
int number=b.getNumber();
list.add(""+number);
resultShow.setText(""+number);
showComputerProcess.setText(""+list.get(0)+""+list.get(1)+
""+list.get(2));
}
else if(list.size()==3) {
int number=b.getNumber();
String num=list.getLast();
String s=num.concat(""+number);
list.set(2,s);
resultShow.setText(s);
showComputerProcess.setText(""+list.get(0)+""+list.get(1)+
""+list.get(2));
}
}

}
import java.util.LinkedList;
import javax.swing.*;
import java.awt.event.*;
public class HandleDot implements ActionListener{
LinkedList<String> list;
JTextField resultShow,showComputerProcess;
HandleDot(LinkedList<String> list,JTextField t1,JTextField t2){
this.list=list;
resultShow=t1;
showComputerProcess=t2;
}
public void actionPerformed(ActionEvent e){
String dot=e.getActionCommand();
if(list.size()==1){
String num=list.getFirst();
String s=null;
if(num.indexOf(dot)==-1){
s=num.concat(dot);
list.set(0,s);
}
else
s=num;
list.set(0,s);
resultShow.setText(s);
showComputerProcess.setText(""+list.get(0));
}
else if(list.size()==3){
String num=list.getLast();
String s=null;
if(num.indexOf(dot)==-1){
s=num.concat(dot);
list.set(2,s);
}
else
s=num;
resultShow.setText(s);
showComputerProcess.setText(""+list.get(0)+""+list.get(1)+
""+list.get(2));
}
}
}
import java.util.LinkedList;
import javax.swing.*;
import java.awt.event.*;
public class HandleDownNumber implements ActionListener{
LinkedList<String> list;
JTextField resultShow,showComputerProcess;
JTextArea saveComputerProcess;
HandleDownNumber(LinkedList<String> list,JTextField t1,JTextField t2,
JTextArea t3){
this.list=list;
resultShow=t1;
showComputerProcess=t2;
saveComputerProcess=t3;
}
public void actionPerformed(ActionEvent e){
if(list.size()==1||list.size()==2){
String numOne=list.getFirst();
try{ double d=Double.parseDouble(numOne);
double result=1.0/d;
String str=String.valueOf(result);
list.set(0,str);
resultShow.setText(str);
String proccess="1/"+numOne+"="+result;
showComputerProcess.setText(proccess);
saveComputerProcess.append("\n"+proccess);
if(list.size()==2)
list.removeLast(); //移掉运算符号
}
catch(Exception ee){}
}
else if(list.size()==3){
String numTwo=list.getLast();
try{ double d=Double.parseDouble(numTwo);
double result=1.0/d;
String str=String.valueOf(result);
list.set(0,str);
resultShow.setText(str);
String proccess="1/"+numTwo+"="+result;
showComputerProcess.setText(proccess);
saveComputerProcess.append("\n"+proccess);
list.removeLast(); //移掉第2个运算数
list.removeLast(); //移掉运算符号
}
catch(Exception ee){}
}
}
}
import java.util.LinkedList;
import javax.swing.*;
import java.awt.event.*;
public class HandleEquality implements ActionListener{
LinkedList<String> list;
JTextField resultShow,showComputerProcess;
JTextArea saveComputerProcess;
HandleEquality(LinkedList<String> list,JTextField t1,JTextField t2,
JTextArea t3){
this.list=list;
resultShow=t1;
showComputerProcess=t2;
saveComputerProcess=t3;
}
public void actionPerformed(ActionEvent e){
if(list.size()==1){
String num=list.getFirst();
resultShow.setText(""+num);
showComputerProcess.setText(list.get(0));
}
if(list.size()==2){
String num=list.getFirst();
String 运算符号=list.get(1);
try{ double n1=Double.parseDouble(num);
double n2=Double.parseDouble(num);
double result=0;
if(运算符号.equals("+"))
result=n1+n2;
else if(运算符号.equals("-"))
result=n1-n2;
else if(运算符号.equals("*"))
result=n1*n2;
else if(运算符号.equals("/"))
result=n1/n2;
resultShow.setText(""+result);
String proccess=num+""+运算符号+""+num+"="+result;
showComputerProcess.setText(proccess);
saveComputerProcess.append(" "+proccess+"\n");
list.set(0,""+result);
}
catch(Exception ee){}
}
else if(list.size()==3){
String numOne=list.getFirst();
String 运算符号=list.get(1);
String numTwo=list.getLast();
try{ double n1=Double.parseDouble(numOne);
double n2=Double.parseDouble(numTwo);
double result=0;
if(运算符号.equals("+"))
result=n1+n2;
else if(运算符号.equals("-"))
result=n1-n2;
else if(运算符号.equals("*"))
result=n1*n2;
else if(运算符号.equals("/"))
result=n1/n2;
resultShow.setText(""+result);
String proccess=numOne+""+运算符号+""+numTwo+"="+result;
showComputerProcess.setText(proccess);
saveComputerProcess.append(" "+proccess+"\n");
list.set(0,""+result);
list.removeLast(); //移掉第2个运算数
list.removeLast(); //移掉运算符号
}
catch(Exception ee){}
}
}
}
import java.util.LinkedList;
import javax.swing.*;
import java.awt.event.*;
public class HandleOperation implements ActionListener{
LinkedList<String> list;
JTextField resultShow,showComputerProcess;
JTextArea saveComputerProcess;
HandleOperation(LinkedList<String> list,JTextField t1,JTextField t2,
JTextArea t3){
this.list=list;
resultShow=t1;
showComputerProcess=t2;
saveComputerProcess=t3;
}
public void actionPerformed(ActionEvent e){
OperationButton b=(OperationButton)e.getSource();
if(list.size()==1){
String fuhao=b.getOperateSign();
list.add(fuhao);
showComputerProcess.setText(""+list.get(0)+""+list.get(1));
}
else if(list.size()==2){
String fuhao=b.getOperateSign();
list.set(1,fuhao);
showComputerProcess.setText(""+list.get(0)+""+list.get(1));
}
else if(list.size()==3){
String numOne=list.getFirst();
String numTwo=list.getLast();
String 运算符号=list.get(1);
String middleProcess=numOne+""+运算符号+numTwo;
try{
double n1=Double.parseDouble(numOne);
double n2=Double.parseDouble(numTwo);
double result=0;
if(运算符号.equals("+"))
result=n1+n2;
else if(运算符号.equals("-"))
result=n1-n2;
else if(运算符号.equals("*"))
result=n1*n2;
else if(运算符号.equals("/"))
result=n1/n2;
String fuhao=b.getOperateSign();
list.clear();
list.add(""+result);
list.add(fuhao);
String pro=middleProcess+"="+result+""+list.get(1);
showComputerProcess.setText(pro);
saveComputerProcess.append(" "+middleProcess+"="+result+"\n");
resultShow.setText(""+result);
}
catch(Exception ee){}
}
}
}
}
SADSDSDDASDASD 2009-12-27
  • 打赏
  • 举报
回复
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.LinkedList;
import java.io.*;
public class CalculatorWindow extends JFrame implements ActionListener{
NumberButton numberButton[];
OperationButton operationButton[];
JButton 小数点操作,正负号操作,退格操作,等号操作,清零操作,sin;
JTextField resultShow; //显示计算结果
JTextField showComputerProcess; //显示当前计算过程
JTextArea saveComputerProcess; //显示计算步骤
JButton saveButton,copyButton,clearButton;
LinkedList<String> list; //链表用来存放第一个运算数、运算符号和第二个运算数
HandleDigit handleDigit; //负责处理ActionEvent事件
HandleOperation handleOperation ;
HandleBack handleBack;
HandleClear handleClear;
HandleEquality handleEquality;
HandleDot handleDot;
HandlePOrN handlePOrN;
HandleSin handleSin;
public CalculatorWindow(){
setTitle("计算器");
JPanel panelLeft,panelRight;
list=new LinkedList<String>();
resultShow=new JTextField(10);
resultShow.setHorizontalAlignment(JTextField.RIGHT);
resultShow.setForeground(Color.blue);
resultShow.setFont(new Font("TimesRoman",Font.BOLD,16));
resultShow.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
resultShow.setEditable(false);
resultShow.setBackground(Color.white);
showComputerProcess=new JTextField();
showComputerProcess.setHorizontalAlignment(JTextField.CENTER);
showComputerProcess.setFont(new Font("Arial",Font.BOLD,16));
showComputerProcess.setBackground(Color.cyan);
showComputerProcess.setEditable(false);
saveComputerProcess=new JTextArea();
saveComputerProcess.setEditable(false);
saveComputerProcess.setFont(new Font("宋体",Font.PLAIN,16));
numberButton=new NumberButton[10];
handleDigit=new HandleDigit(list,resultShow,showComputerProcess);
for(int i=0;i<=9;i++){
numberButton[i]=new NumberButton(i);
numberButton[i].setFont(new Font("Arial",Font.BOLD,20));
numberButton[i].addActionListener(handleDigit);
}
operationButton=new OperationButton[4];
handleOperation=new HandleOperation(list,resultShow,
showComputerProcess,saveComputerProcess);
String 运算符号[]={"+","-","*","/"};
for(int i=0;i<4;i++){
operationButton[i]=new OperationButton(运算符号[i]);
operationButton[i].setFont(new Font("Arial",Font.BOLD,20));
operationButton[i].addActionListener(handleOperation);
}
小数点操作=new JButton(".");
handleDot=new HandleDot(list,resultShow,showComputerProcess);
小数点操作.addActionListener(handleDot);
正负号操作=new JButton("+/-");
handlePOrN=new HandlePOrN(list,resultShow,showComputerProcess);
正负号操作.addActionListener(handlePOrN);
等号操作=new JButton("=");
handleEquality=new HandleEquality(list,resultShow,
showComputerProcess,saveComputerProcess);
等号操作.addActionListener(handleEquality);
sin=new JButton("sin");
handleSin=new HandleSin(list,resultShow,
showComputerProcess,saveComputerProcess);
sin.addActionListener(handleSin);
退格操作=new JButton("退格");
handleBack=new HandleBack(list,resultShow,showComputerProcess);
退格操作.addActionListener(handleBack);
清零操作=new JButton("C");
handleClear=new HandleClear(list,resultShow,showComputerProcess);
清零操作.addActionListener(handleClear);
清零操作.setForeground(Color.red);
退格操作.setForeground(Color.red);
等号操作.setForeground(Color.red);
sin.setForeground(Color.blue);
正负号操作.setForeground(Color.blue);
小数点操作.setForeground(Color.blue);
panelLeft=new JPanel();
panelRight=new JPanel();
panelLeft.setLayout(new BorderLayout());
JPanel centerInLeft=new JPanel();
panelLeft.add(resultShow,BorderLayout.NORTH);
panelLeft.add(centerInLeft,BorderLayout.CENTER);
centerInLeft.setLayout(new GridLayout(4,5));
centerInLeft.add(numberButton[1]);
centerInLeft.add(numberButton[2]);
centerInLeft.add(numberButton[3]);
centerInLeft.add(operationButton[0]);
centerInLeft.add(清零操作);
centerInLeft.add(numberButton[4]);
centerInLeft.add(numberButton[5]);
centerInLeft.add(numberButton[6]);
centerInLeft.add(operationButton[1]);
centerInLeft.add(退格操作);
centerInLeft.add(numberButton[7]);
centerInLeft.add(numberButton[8]);
centerInLeft.add(numberButton[9]);
centerInLeft.add(operationButton[2]);
centerInLeft.add(sin);
centerInLeft.add(numberButton[0]);
centerInLeft.add(正负号操作);
centerInLeft.add(小数点操作);
centerInLeft.add(operationButton[3]);
centerInLeft.add(等号操作);
panelRight.setLayout(new BorderLayout());
panelRight.add(showComputerProcess,BorderLayout.NORTH);
saveButton=new JButton("保存");
copyButton=new JButton("复制");
clearButton=new JButton("清除");
saveButton.setToolTipText("保存计算过程到文件");
copyButton.setToolTipText("复制选中的计算过程");
clearButton.setToolTipText("清除计算过程");
saveButton.addActionListener(this);
copyButton.addActionListener(this);
clearButton.addActionListener(this);
panelRight.add(new JScrollPane(saveComputerProcess),BorderLayout.CENTER);
JPanel southInPanelRight=new JPanel();
southInPanelRight.add(saveButton);
southInPanelRight.add(copyButton);
southInPanelRight.add(clearButton);
panelRight.add(southInPanelRight,BorderLayout.SOUTH);
JSplitPane split=new JSplitPane
(JSplitPane.HORIZONTAL_SPLIT,panelLeft,panelRight);
add(split,BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setBounds(100,50,528,258);
validate();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==copyButton)
saveComputerProcess.copy();
if(e.getSource()==clearButton)
saveComputerProcess.setText(null);
if(e.getSource()==saveButton){
JFileChooser chooser=new JFileChooser();
int state=chooser.showSaveDialog(null);
File file=chooser.getSelectedFile();
if(file!=null&&state==JFileChooser.APPROVE_OPTION){
try{ String content=saveComputerProcess.getText();
StringReader read=new StringReader(content);
BufferedReader in= new BufferedReader(read);
FileWriter outOne=new FileWriter(file);
BufferedWriter out= new BufferedWriter(outOne);
String str=null;
while((str=in.readLine())!=null){
out.write(str);
out.newLine();
}
in.close();
out.close();
}
catch(IOException e1){}
}
}
}
public static void main(String args[]){
new CalculatorWindow();
}
}
import java.util.LinkedList;
import javax.swing.*;
import java.awt.event.*;
public class HandleBack implements ActionListener{
LinkedList<String> list;
JTextField resultShow,showComputerProcess;
HandleBack(LinkedList<String> list,JTextField t1,JTextField t2){
this.list=list;
resultShow=t1;
showComputerProcess=t2;
}
public void actionPerformed(ActionEvent e){
if(list.size()==1){
String num=(String)list.getFirst();
if(num.length()>=1){
num=num.substring(0,num.length()-1);
list.set(0,num);
resultShow.setText(num);
showComputerProcess.setText(""+num);
}
else{
list.removeLast();
resultShow.setText("0");
showComputerProcess.setText("0");
}
}
else if(list.size()==3){
String num=(String)list.getLast();
if(num.length()>=1){
num=num.substring(0,num.length()-1);
list.set(2,num);
resultShow.setText(num);
showComputerProcess.setText(num);
}
else{
list.removeLast();
resultShow.setText("0");
showComputerProcess.setText("0");
}
}
}
}
import java.util.LinkedList;
import javax.swing.*;
import java.awt.event.*;
public class HandleClear implements ActionListener{
LinkedList<String> list;
JTextField resultShow,showComputerProcess;
HandleClear(LinkedList<String> list,JTextField t1,JTextField t2){
this.list=list;
resultShow=t1;
showComputerProcess=t2;
}
public void actionPerformed(ActionEvent e){
resultShow.setText("0");
list.clear();
showComputerProcess.setText(null);
}
}
SADSDSDDASDASD 2009-12-27
  • 打赏
  • 举报
回复
JFrame frame=new jisuan(); ????????这个就不对 我吧你的程序弄到我电脑里了 错误很多啊
CrySleeper 2009-12-27
  • 打赏
  • 举报
回复
捣腾分的?
luffyke 2009-12-27
  • 打赏
  • 举报
回复
public class jisuan extends WindowAdapter

JFrame frame=new jisuan();

??
cmdgjw 2009-12-27
  • 打赏
  • 举报
回复
楼主你好我也在做这个;你加我的qq群咱么讨论;74552467
我的思路是把每次的输入都拼接在一个字符串上。然后检查字符串再操作就比较简单了!不然判断每次输入的情况太麻烦了!
zhangxi_1989 2009-12-27
  • 打赏
  • 举报
回复
把错误信息写出来吧,大家一起分析分析..你这样光给原代码还得让别人复制下来自己编译..
zl3450341 2009-12-27
  • 打赏
  • 举报
回复
报错信息也没有
叫别人怎么帮你看呢
wqin8 2009-12-27
  • 打赏
  • 举报
回复
念书的时候,也写过这玩意儿!
相关推荐
发帖
Java

4.9w+

社区成员

Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
帖子事件
创建了帖子
2009-12-27 09:30
社区公告
暂无公告