Topic: 寻求好心人帮我修改一下这个程序 |
Print this page |
1.寻求好心人帮我修改一下这个程序 | Copy to clipboard |
Posted by: zwguo1008 Posted on: 2007-11-01 10:19 本人想编个简单的计算器,可老有个异常,寻求好心人帮解决一下,不要在源程序上有大的改动哦,谢谢了 程序如下: import java.awt.*; import javax.swing.*; import java.awt.event.*; public class jisuanqi extends JFrame implements ActionListener { JTextField displaytext=new JTextField(30); String[] keys={"1","2","3","+","4","5","6","-","7","8","9","*","0",".","=","/","C"}; JButton[] Button; String numstr1; String numstr2; char op; boolean firstinput=true; public jisuanqi() { setTitle("我的计算器"); setSize(260,200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridLayout layout=new GridLayout(3,1,10,10); JPanel row1=new JPanel(); row1.add(displaytext); add(row1); GridLayout layout1=new GridLayout(4,4,10,10); JPanel row2=new JPanel(); row2.setLayout(layout1); for(int index=0;index<16;index++) { Button[index]=new JButton(keys[index]); Button[index].addActionListener(this); Button[index].setSize(50,30); row2.add(Button[index]); } add(row2); JPanel row3=new JPanel(); Button[16]=new JButton(keys[16]); Button[16].addActionListener(this); row3.add(Button[16]); add(row3); setVisible(true); } public void actionPerformed(ActionEvent e) { String resultstr; String str=e.getActionCommand(); char ch; ch=str.charAt(0); switch(ch) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '.': if(firstinput) { numstr1=numstr1+ch; displaytext.setText(numstr1); } else { numstr2=numstr2+ch; displaytext.setText(numstr2); } break; case '+': case '-': case '*': case '/': op=ch; firstinput=false; break; case '=': resultstr=jisuan(); displaytext.setText(resultstr); numstr1=resultstr; numstr2=""; break; case 'C': displaytext.setText("0."); numstr1=""; numstr2=""; break; } } public String jisuan() { try { double A= Double.valueOf(numstr1); double B= Double.valueOf(numstr1); double result=0.0; switch(op) { case '+': result=A+B; break; case '-': result=A-B; break; case '*': result=A*B; break; case '/': result=A/B; break; } return String.valueOf(result); } catch(ArithmeticException e) { return "错误"+e.getMessage(); } catch(NumberFormatException e) { if(numstr1.equals("")) return "错误:第一个数不可用"; else return "错误:第二个数不可用"; } catch(Exception e) { return "错误"; } } public static void main(String[] args) { jisuanqi myjisuanqi=new jisuanqi(); } } |
2.Re:寻求好心人帮我修改一下这个程序 [Re: zwguo1008] | Copy to clipboard |
Posted by: andy_wang_5 Posted on: 2007-11-01 11:04
可以显示画面,但不能运算,剩下的你自己改吧。写程序一定要打好基础。不能单纯的模仿。 |
3.Re:寻求好心人帮我修改一下这个程序 [Re: zwguo1008] | Copy to clipboard |
Posted by: andy_wang_5 Posted on: 2007-11-01 11:13 本来已经下线了,忍不住上来发个牢骚。 你基础太不扎实了。 你连NullPointerException都不能解决,怎么写程序。 你连layout都不懂。能写swing吗? |
4.Re:寻求好心人帮我修改一下这个程序 [Re: zwguo1008] | Copy to clipboard |
Posted by: chenshuai123 Posted on: 2007-11-05 00:17 我是新手,才学半个月.这是我写的,大家给点意见,谢谢大家,共同提高: import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Computer implements ActionListener{ private TextField tf = new TextField("0",30); private Button[] b = new Button[20]; private String[] label = {"back","CE","C","+","7", "8","9","-","4","5","6","*","1","2","3","/","0", "+/-",".","="}; private boolean flag = false; private double d1 = 0; private double d2 = 1; private String operator = "+"; public Computer(){ Frame f = new Frame("Computer"); f.add(tf,"North"); Panel p = new Panel(); MenuBar mb = new MenuBar(); Menu m1 = new Menu("编辑"); Menu m2 = new Menu("帮助"); MenuItem mi1 = new MenuItem("复制"); MenuItem mi2 = new MenuItem("粘贴"); MenuItem mi3 = new MenuItem("关于计算器"); m1.add(mi1); m1.add(mi2); m2.add(mi3); mb.add(m1); mb.add(m2); f.setMenuBar(mb); p.setLayout(new GridLayout(5,4,5,5)); for(int i=0;i<b.length;i++){ b[i] = new Button(label[i]); p.add(b[i]); b[i].addActionListener(this); } f.add; tf.setEditable(false); f.setVisible(true); f.setResizable(false); f.pack(); } public static void main(String[] args) { new Computer(); } public void actionPerformed(ActionEvent ae) { String command = ae.getActionCommand(); if(command.equals("0")||command.equals("1")|| command.equals("2")||command.equals("3")|| command.equals("4")||command.equals("5")|| command.equals("6")||command.equals("7")|| command.equals("8")||command.equals("9")){ if(flag){ tf.setText(tf.getText()+command); }else{ tf.setText(command); flag = true; } }else if(command.equals(".")){ String temp = tf.getText(); if(temp.indexOf(".")==-1){ tf.setText(temp+"."); flag=true; } }else if(command.equals("+")||command.equals("-")|| command.equals("*")||command.equals("/")){ d1 = Double.parseDouble(tf.getText()); operator = command; flag = false; }else if(command.equals("=")){ d2 = Double.parseDouble(tf.getText()); if(operator.equals("+")){ d1 = d1+d2; }else if(operator.equals("-")){ d1 = d1-d2; }else if(operator.equals("*")){ d1 = d1*d2; }else if(operator.equals("/")){ d1 = d1/d2; } tf.setText(""+d1); flag = false; } } } |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |