| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 544 人关注过本帖
标题:JAVA做的计算器出了个问题求助
只看楼主 加入收藏
小月饼
Rank: 1
等 级:新手上路
帖 子:7
专家分:4
注 册:2013-11-3
结帖率:66.67%
收藏
 问题点数:0 回复次数:2 
JAVA做的计算器出了个问题求助
我用for循环定义了按键数组并且同样用循环设置了按钮。可是监听器却似乎没有效果,运行程序的时候点击按钮没反应……
程序代码:
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class JiSuanQi extends JFrame implements ActionListener

{
    String s = "", s1 = null, s2 = null;
    JFrame f = new JFrame("计算器");
    JTextField xianshi = new JTextField(30);
    JPanel p1 = new JPanel();
    JPanel p2 = new JPanel();
    JPanel p3 = new JPanel();
    JButton dengyu = new JButton("=");
    JButton qiuyu = new JButton("%");
    JButton del = new JButton("Del");
    JButton[] bt = new JButton[16];
    int id = 0;

    public static void main(String[] args) {
        new JiSuanQi().jisuan();
    }

    public void jisuan() {
        f.setLayout(new BorderLayout(8, 8));
        p2.setLayout(new GridLayout(4, 4, 8, 8));
        p3.setLayout(new BorderLayout(8, 8));
        f.add(p1, BorderLayout.NORTH);
        f.add(p2);
        p3.add(del, BorderLayout.NORTH);
        p3.add(qiuyu, BorderLayout.SOUTH);
        p3.add(dengyu, BorderLayout.CENTER);
        p1.add(xianshi);
        f.add(p3, BorderLayout.EAST);
        String[] b = { "1", "2", "3", "+", "4", "5", "6", "-", "7", "8", "9",
                "*", "0", ".", "C", "/" };

        // 设置颜色
        for (int i = 0; i < 16; i++) {
            bt[i] = new JButton(b[i]);
            p2.add(bt[i]);
        }
        for (int i = 0; i < 16; i++) {
            if (i == 3 | i == 7 | i == 11 | i == 15 | i == 14) {
                bt[i].setForeground(Color.BLACK);
            } else {
                bt[i].setForeground(Color.BLUE);
            }
        }

        dengyu.setForeground(Color.BLACK);
        del.setForeground(Color.BLACK);
        f.pack();
        f.setVisible(true);
       


       

        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

       

        for (int i = 0; i < b.length; i++) {
            (new Button(b[i])).addActionListener(this);
        }


    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO 自动生成的方法存根
        if(e.getSource() == bt[0]){
            s += 1;
            s2 += 1;
            xianshi.setText(s);
        }
       

        if(e.getSource() == bt[1]){
            s += 2;
            s2 += 2;
            xianshi.setText(s);
        }
        if(e.getSource() == bt[2]){
            s += 3;
            s2 += 3;
            xianshi.setText(s);       

        }
        if(e.getSource() == bt[3]){
            s1 = s;
            s += '+';
            id = 1;
            s2 = "";
            xianshi.setText(s);
           

        }
        if(e.getSource() == bt[4]){
            s += 4;
            s2 += 4;
            xianshi.setText(s);
           

        }
        if(e.getSource() == bt[5]){
            s += 5;
            s2 += 5;
            xianshi.setText(s);
           

        }
        if(e.getSource() == bt[6]){
            s += 6;
            s2 += 6;
            xianshi.setText(s);
           

        }
        if(e.getSource() == bt[7]){
            s1 = s;
            s += '-';
            id = 2;
            s2 = "";
            xianshi.setText(s);
           

        }
        if(e.getSource() == bt[8]){
            s += 7;
            s2 += 7;
            xianshi.setText(s);
           

        }
        if(e.getSource() == bt[9]){
            s += 8;
            s2 += 8;
            xianshi.setText(s);
           

        }
        if(e.getSource() == bt[10]){
            s += 9;
            s2 += 9;
            xianshi.setText(s);
           

        }
        if(e.getSource() == bt[11]){
            s1 = s;
            s += '*';
            id = 3;
            s2 = "";
            xianshi.setText(s);
        }
        if(e.getSource() == bt[12]){
            s += 0;
            s2 += 0;
            xianshi.setText(s);
        }
        if(e.getSource() == bt[13]){
            s += '.';
            s2 += '.';
            xianshi.setText(s);
        }
        if(e.getSource() == bt[14]){
            s = "";
            s2 = "";
            xianshi.setText(s);
        }
        if(e.getSource() == bt[15]){
            s1 = s;
            s += '/';
            id = 4;
            s2 = "";
            xianshi.setText(s);
        }
        if(e.getSource() == dengyu){
            if (id < 1)
                ;
            else {
                s += '=';
                double a = Double.parseDouble(s1);
                double b = Double.parseDouble(s2);
                double c = 0;
                switch (id) {
                case 1:
                    c = a + b;
                    break;
                case 2:
                    c = a - b;
                    break;
                case 3:
                    c = a * b;
                    break;
                case 4:
                    c = a / b;
                    break;
                }
                s += c;
                xianshi.setText(s);
            }
            s = "";
            s1 = "";
            s2 = "";
            id = 0;
        }
       

        if(e.getSource() == del){
            char[] c1;
            char[] c2 = new char[s.length() - 1];
            c1 = s.toCharArray();
            for (int i = 0; i < s.length() - 1; i++)
                c2[i] = c1[i];
            s = s.valueOf(c2);
            if (id < 1) {
                s1 = s;
            }
            if (s2.length() >= 1) {
                char[] c3;
                char[] c4 = new char[s2.length() - 1];
                c3 = s2.toCharArray();
                for (int i = 0; i < s2.length() - 1; i++)
                    c4[i] = c3[i];
                s2 = s2.valueOf(c4);
            }
            xianshi.setText(s);
           

        }
       

    }

}

搜索更多相关主题的帖子: 计算器 运行程序 null JAVA 
2014-12-09 15:04
小月饼
Rank: 1
等 级:新手上路
帖 子:7
专家分:4
注 册:2013-11-3
收藏
得分:0 
已经解决了……

        for (int i = 0; i < b.length; i++) {
            (new Button(b[i])).addActionListener(this);
        }
添加错了……

应该是……

        for (int i = 0; i < b.length; i++) {
            bt[i].addActionListener(this);
        }
2014-12-09 16:19
日知己所无
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:38
帖 子:427
专家分:2071
注 册:2014-3-22
收藏
得分:0 
恭喜
2014-12-09 21:55
快速回复:JAVA做的计算器出了个问题求助
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.100077 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved