总结:虽然,没有人会帮你到底,凭什么要对你怜香惜玉
注意实现哪一个运算就把相关代码放在else if这个判断语句里面
package com.rue;import java.awt.BorderLayout;import java.awt.FlowLayout;import java.awt.Font;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;//实现计算器的功能,我他妈的真是不懂变通。class Ji extends JFrame implements ActionListener { private static final String CE = null; // 定义按钮 StringBuffer str = new StringBuffer(); JButton jb_0, jb_1, jb_2, jb_3, jb_4, jb_5, jb_6, jb_7, jb_8, jb_9, jb_jia, jb_jian, jb_cheng, jb_chu, jb_dian, jb_deng, jb_delete; JLabel jf; double num1 = 0; double num2 = 0; char c = '\0'; double m; boolean ready = true; public Ji() { JPanel jp = new JPanel(new GridLayout(5, 4));// 把所有的按钮组件和文本框放在这个panel里面 jf = new JLabel("0.0"); jf.setFont(new Font("", 1, 18)); jf.setHorizontalAlignment(JLabel.RIGHT); jb_delete = new JButton("CE");// 设一个删除的按钮 jb_0 = new JButton("0"); JButton jb_genghao = new JButton("√"); jb_1 = new JButton("1"); jb_genghao.addActionListener(this); jb_2 = new JButton("2"); jb_3 = new JButton("3"); jb_4 = new JButton("4"); jb_5 = new JButton("5"); jb_6 = new JButton("6"); jb_7 = new JButton("7"); jb_8 = new JButton("8"); jb_9 = new JButton("9"); jb_jia = new JButton("+"); jb_jian = new JButton("-"); jb_cheng = new JButton("*"); jb_chu = new JButton("/"); jb_dian = new JButton("."); jb_deng = new JButton("="); // jb_delete.addActionListener(this); jb_0.addActionListener(this); jb_1.addActionListener(this); jb_2.addActionListener(this); jb_3.addActionListener(this); jb_4.addActionListener(this); jb_5.addActionListener(this); jb_6.addActionListener(this); jb_7.addActionListener(this); jb_8.addActionListener(this); jb_9.addActionListener(this); jb_jia.addActionListener(this); jb_jian.addActionListener(this); jb_cheng.addActionListener(this); jb_chu.addActionListener(this); jb_dian.addActionListener(this); jb_deng.addActionListener(this); JButton jb_Back = new JButton("Back"); jb_Back.addActionListener(this); jp.add(jb_1); jp.add(jb_2); jp.add(jb_3); jp.add(jb_4); jp.add(jb_5); jp.add(jb_6); jp.add(jb_7); jp.add(jb_8); // this.add(jp); jp.add(jb_9); jp.add(jb_jia); jp.add(jb_jian); jp.add(jb_cheng); jp.add(jb_chu); jp.add(jb_dian); jp.add(jb_deng); jp.add(jb_0); jp.add(jb_Back); jp.add(jb_delete); jp.add(jb_genghao); this.add(jf, BorderLayout.NORTH); this.add(jp); // this.setLayout(); this.setVisible(true); this.setBounds(100, 20, 230, 300); this.setDefaultCloseOperation(3); } public double count() { switch (c) { case '+': m = num1 + num2; break; case '-': m = num1 - num2; break; case '*': m = num1 * num2; break; case '/': m = num1 / num2; break; case '√': m = Math.sqrt(num1); break; case 'E': m = '\0'; } return m; } public void actionPerformed(ActionEvent e) { String str = e.getActionCommand(); if (str.equals("0") || str.equals(".") || str.equals("1") || str.equals("2") || str.equals("3") || str.equals("4") || str.equals("5") || str.equals("6") || str.equals("7") || str.equals("8") || str.equals("9") || str.equals("1")) { if (ready) { jf.setText(str); ready = false; } else { jf.setText(jf.getText() + str); ready = false; } } else if (str.equals("+")) { num1 = Double.parseDouble(jf.getText()); // count(); // 其实可以感觉到操作符有点不正常的。就是操作符设置的问题有点大 // if(num1=){} c = '+'; ready = true; } else if (str.equals("√")) { num1 = Double.parseDouble(jf.getText()); c = '√'; ready = true; } else if (str.equals("CE")) { num1 = Double.parseDouble(jf.getText()); // c='c';//这里可以判断按的是哪一个按钮吗? jf.setText("0.0");// 记住这里是要清空,但并不是把lable也全部删除看不见。Label的作用。 ready = true;// 当这里为false时,它会与0.0连接一起显示出来,此时设置我ture。它正常了。是不是继续执行下一步的意思呢? } else if (str.equals("-")) { num1 = Double.parseDouble(jf.getText()); c = '-'; ready = true; } else if (str.equals("*")) { num1 = Double.parseDouble(jf.getText()); c = '*'; ready = true; } else if (str.equals("/")) { num1 = Double.parseDouble(jf.getText()); c = '/'; ready = true; } else if (str.equals("=")) { num2 = Double.parseDouble(jf.getText()); jf.setText(count() + ""); ready = false; } }}public class Test { public static void main(String[] args) { new Ji(); }}