免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
论坛 程序设计 Java 求解
最近访问板块 发新帖
查看: 1683 | 回复: 2
打印 上一主题 下一主题

求解 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-12-17 20:23 |只看该作者 |倒序浏览
我学java一周尝试做了个计算器 运行没报错  但单击按钮 文本框中不显示相应的数字,郁闷  求大师帮忙找找错
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class edec extends JFrame implements ActionListener {
        private JFrame frame;
        private JButton button1, button2, button3, button4, button5, button6,
                        button7, button8, button9, button0, buttonadd, buttonreduce,
                        buttonride, buttondivide, buttonpoint, buttonequal;
        private JTextField textField;
        String string;

        public void init() {
                getContentPane().setLayout(null);
                setBounds(100, 100, 291, 280);
                setResizable(false);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                final JButton button1 = new JButton();
                button1.setBackground(Color.WHITE);
                button1.setText("1";
                button1.setBounds(10, 88, 59, 29);
                getContentPane().add(button1);
                button1.addActionListener(this);

                final JButton button2 = new JButton();
                button2.setBackground(Color.WHITE);
                button2.setText("2";
                button2.setBounds(75, 88, 59, 29);
                getContentPane().add(button2);
                button2.addActionListener(this);

                final JButton button3 = new JButton();
                button3.setBackground(Color.WHITE);
                button3.setText("3";
                button3.addActionListener(this);
                button3.setBounds(140, 88, 59, 29);
                getContentPane().add(button3);
                button3.addActionListener(this);

                final JButton button4 = new JButton();
                button4.setBackground(Color.WHITE);
                button4.setText("4";
                button4.setBounds(10, 123, 59, 29);
                getContentPane().add(button4);
                button4.addActionListener(this);

                final JButton button5 = new JButton();
                button5.setBackground(Color.WHITE);
                button5.setText("5";
                button5.setBounds(75, 123, 59, 29);
                getContentPane().add(button5);
                button5.addActionListener(this);

                final JButton button6 = new JButton();
                button6.setBackground(Color.WHITE);
                button6.setText("6";
                button6.setBounds(140, 123, 59, 29);
                getContentPane().add(button6);
                button6.addActionListener(this);

                final JButton button7 = new JButton();
                button7.setBackground(Color.WHITE);
                button7.setText("7";
                button7.setBounds(10, 158, 59, 29);
                getContentPane().add(button7);
                button7.addActionListener(this);

                final JButton button8 = new JButton();
                button8.setBackground(Color.WHITE);
                button8.setText("8";
                button8.setBounds(75, 158, 59, 29);
                getContentPane().add(button;
                button8.addActionListener(this);

                final JButton button9 = new JButton();
                button9.setBackground(Color.WHITE);
                button9.setText("9";
                button9.setBounds(140, 158, 59, 29);
                getContentPane().add(button9);
                button9.addActionListener(this);

                final JButton button0 = new JButton();
                button0.setBackground(Color.WHITE);
                button0.setText("0";
                button0.setBounds(10, 193, 59, 29);
                getContentPane().add(button0);
                button0.addActionListener(this);

                final JButton buttonpoint = new JButton();
                buttonpoint.setBackground(Color.WHITE);
                buttonpoint.setText(".");
                buttonpoint.setBounds(75, 193, 59, 29);
                getContentPane().add(buttonpoint);
                button0.addActionListener(this);

                final JButton buttonequal = new JButton();
                buttonequal.setBackground(Color.WHITE);
                buttonequal.setText("=");
                buttonequal.setBounds(140, 193, 59, 29);
                getContentPane().add(buttonequal);
                buttonequal.addActionListener(this);

                final JButton buttondivide = new JButton();
                buttondivide.setBackground(Color.WHITE);
                buttondivide.setText("/");
                buttondivide.setBounds(205, 88, 59, 29);
                getContentPane().add(buttondivide);
                buttondivide.addActionListener(this);

                final JButton buttonride = new JButton();
                buttonride.setBackground(Color.WHITE);
                buttonride.setText("*");
                buttonride.setBounds(205, 123, 59, 29);
                getContentPane().add(buttonride);
                buttonride.addActionListener(this);

                final JButton buttonreduce = new JButton();
                buttonreduce.setBackground(Color.WHITE);
                buttonreduce.setText("-");
                buttonreduce.setBounds(205, 158, 59, 29);
                getContentPane().add(buttonreduce);
                buttonreduce.addActionListener(this);

                final JButton buttonadd = new JButton();
                buttonadd.setBackground(Color.WHITE);
                buttonadd.setText("+");
                buttonadd.setBounds(205, 193, 59, 29);
                getContentPane().add(buttonadd);
                buttonadd.addActionListener(this);

                final JTextField textField = new JTextField();
                textField.setBounds(10, 53, 254, 29);
                getContentPane().add(textField);
        }

        public static void main(String args[]) {
                edec frame = new edec();
                frame.setVisible(true);
                frame.init();

        }
        String foreNum = null;
        String currNum = null;
        String currOpr = null;

        public void actionPerformed(ActionEvent e) {
                Object object = e.getSource();
                if (object == button0 || object == button1 || object == button3
                                || object == button2 || object == button4 || object == button5
                                || object == button6 || object == button7 || object == button8
                                || object == button9) {
                        textField.setText(e.getActionCommand());
                } else if (object == buttonpoint) {
                        textField.setText(textField.getText() + '.');
                } else if (object == buttonadd || object == buttonreduce
                                || object == buttonride || object == buttonride) {
                        currNum = textField.getText();
                        if (currOpr == "+")
                                textField.setText(String.valueOf(Double.parseDouble(foreNum)
                                                + Double.parseDouble(currNum)));
                        else if (currOpr == "-")
                                textField.setText(String.valueOf(Double.parseDouble(foreNum)
                                                - Double.parseDouble(currNum)));
                        else if (currOpr == "*")
                                textField.setText(String.valueOf(Double.parseDouble(foreNum)
                                                * Double.parseDouble(currNum)));
                        else if (currOpr == "/") {
                                if (Double.parseDouble(textField.getText()) == 0)
                                        textField.setText("除数不能为零");
                                else
                                        textField
                                                        .setText(String.valueOf(Double.parseDouble(foreNum)
                                                                        / Double.parseDouble(currNum)));
                        }
                } else if (object == buttonequal) {
                        currNum = textField.getText();
                        if (currOpr == "+")
                                textField.setText(String.valueOf(Double.parseDouble(foreNum)
                                                + Double.parseDouble(currNum)));
                        else if (currOpr == "-")
                                textField.setText(String.valueOf(Double.parseDouble(foreNum)
                                                - Double.parseDouble(currNum)));
                        else if (currOpr == "*")
                                textField.setText(String.valueOf(Double.parseDouble(foreNum)
                                                * Double.parseDouble(currNum)));
                        else if (currOpr == "/") {
                                if (Double.parseDouble(textField.getText()) == 0)
                                        textField.setText("除数不能为零");
                                else
                                        textField
                                                        .setText(String.valueOf(Double.parseDouble(foreNum)
                                                                        / Double.parseDouble(currNum)));
                        }
                }

        }
}

论坛徽章:
19
CU大牛徽章
日期:2013-03-13 15:32:35CU大牛徽章
日期:2013-09-18 15:15:15CU大牛徽章
日期:2013-05-20 10:46:44CU大牛徽章
日期:2013-05-20 10:46:38CU大牛徽章
日期:2013-05-20 10:46:31CU大牛徽章
日期:2013-05-20 10:46:25CU大牛徽章
日期:2013-05-20 10:46:18CU大牛徽章
日期:2013-04-17 11:19:51CU大牛徽章
日期:2013-04-17 11:19:42CU大牛徽章
日期:2013-04-17 11:19:37CU大牛徽章
日期:2013-04-17 11:19:32CU大牛徽章
日期:2013-04-17 11:19:28
2 [报告]
发表于 2013-01-13 14:42 |只看该作者
回复 1# 飞云豹


    你的代码里面怎么有这个 public void init() ,你做的事JApplet吗

论坛徽章:
19
CU大牛徽章
日期:2013-03-13 15:32:35CU大牛徽章
日期:2013-09-18 15:15:15CU大牛徽章
日期:2013-05-20 10:46:44CU大牛徽章
日期:2013-05-20 10:46:38CU大牛徽章
日期:2013-05-20 10:46:31CU大牛徽章
日期:2013-05-20 10:46:25CU大牛徽章
日期:2013-05-20 10:46:18CU大牛徽章
日期:2013-04-17 11:19:51CU大牛徽章
日期:2013-04-17 11:19:42CU大牛徽章
日期:2013-04-17 11:19:37CU大牛徽章
日期:2013-04-17 11:19:32CU大牛徽章
日期:2013-04-17 11:19:28
3 [报告]
发表于 2013-01-13 14:48 |只看该作者
public void actionPerformed(ActionEvent e) 是这个么?好久不用,忘了
  1. if (object == buttonequal) {
  2.                         currNum = textField.getText();
  3.                         if (currOpr == "+")
  4.                                 textField.setText(String.valueOf(Double.parseDouble(foreNum)
  5.                                                 + Double.parseDouble(currNum)));
  6.                         else if (currOpr == "-")
  7.                                 textField.setText(String.valueOf(Double.parseDouble(foreNum)
  8.                                                 - Double.parseDouble(currNum)));
  9.                         else if (currOpr == "*")
  10.                                 textField.setText(String.valueOf(Double.parseDouble(foreNum)
  11.                                                 * Double.parseDouble(currNum)));
  12.                         else if (currOpr == "/") {
  13.                                 if (Double.parseDouble(textField.getText()) == 0)
  14.                                         textField.setText("除数不能为零");
  15.                                 else
  16.                                         textField.setText(String.valueOf(Double.parseDouble(foreNum)
  17.                                                                         / Double.parseDouble(currNum)));
  18.                         }
复制代码
而且这一段儿,看着怪怪的
貌似不是String.valueOf(),而是.....toString()吧
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP