2 qq 24981079 qq_24981079 于 2016.04.19 19:41 提问

关于简易计算机的Java程序 为什么一直报错?

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DecimalFormat;

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

public class calculator {
private JFrame f =new JFrame("Calculator");
private JPanel inputPanel =new JPanel();
private JPanel buttonsPanel=new JPanel();
private JPanel operationPanel =new JPanel();
private JTextField input=new JTextField(20);
private JTextField show=new JTextField(20);
private JButton backspace=new JButton("退格");
private JButton CE=new JButton("CE");
private JButton C=new JButton("C");
private JButton sqrt=new JButton("sqrt");
private JButton fang=new JButton("^");
private JButton mod=new JButton("%");
private JButton dao=new JButton("1/x");
private JButton sin=new JButton("sin");
private JButton cos=new JButton("cos");
private JButton lg=new JButton("ln");

private JButton add=new JButton("+");
private JButton sub=new JButton("-");
private JButton mul=new JButton("*");
private JButton div=new JButton("/");

private JButton decimal=new JButton (".");
private JButton equal=new JButton ("=");
private JButton zero=new JButton ("0");
private JButton one=new JButton ("1");
private JButton two=new JButton ("2");
private JButton three=new JButton ("3");
private JButton four=new JButton ("4");
private JButton five=new JButton ("5");
private JButton six=new JButton ("6");
private JButton seven=new JButton ("7");
private JButton eight=new JButton ("8");
private JButton nine=new JButton ("9");
private String numl="";
private String operator="";

public static void main(String[] args){
    new calculator();
}
public calculator(){
    show.setText("0");
    show.setEditable(false);
    show.setHorizontalAlignment(JTextField.RIGHT);
    input.setText("0");
    input.setEditable(false);
    input.setHorizontalAlignment(JTextField.RIGHT);
    inputPanel.setLayout(new GridLayout(2,1));
    inputPanel.add(show);
    inputPanel.add(input);
    f.add(inputPanel,BorderLayout.NORTH);
    operationPanel.add(sin);
    sin.setBackground(new Color(100,255,10));
    operationPanel.add(cos);
    cos.setBackground(new Color(100,255,10));
    operationPanel.add(CE);
    CE.setBackground(new Color(100,255,10));
    operationPanel.add(C);
    C.setBackground(new Color(100,255,10));
    operationPanel.add(backspace);
    backspace.setBackground(new Color(100,255,10));
    f.add(operationPanel,BorderLayout.CENTER);
    buttonsPanel.setLayout(new GridLayout(4,5));

    buttonsPanel.add(seven);
    buttonsPanel.add(eight);
    buttonsPanel.add(nine);
    buttonsPanel.add(div);
    buttonsPanel.add(mod);
    buttonsPanel.add(four);
    buttonsPanel.add(five);
    buttonsPanel.add(six);
    buttonsPanel.add(mul);
    buttonsPanel.add(fang);
    buttonsPanel.add(one);
    buttonsPanel.add(two);
    buttonsPanel.add(three);
    buttonsPanel.add(sub);
    buttonsPanel.add(sqrt);
    buttonsPanel.add(zero);
    buttonsPanel.add(decimal);
    buttonsPanel.add(equal);
    buttonsPanel.add(add);
    buttonsPanel.add(dao);
    f.add(buttonsPanel, BorderLayout.SOUTH);

    backspace.addMouseListener(new OperationMouseListener());
    CE.addMouseListener(new OperationMouseListener());
    C.addMouseListener(new OperationMouseListener());
    decimal.addMouseListener(new OperationMouseListener());
    equal.addMouseListener(new OperationMouseListener());

    add.addMouseListener(new CaleMouseListener());
    sub.addMouseListener(new CaleMouseListener());
    mul.addMouseListener(new CaleMouseListener());
    div.addMouseListener(new CaleMouseListener());
    sqrt.addMouseListener(new CaleMouseListener());
    fang.addMouseListener(new CaleMouseListener());
    mod.addMouseListener(new CaleMouseListener());
    dao.addMouseListener(new CaleMouseListener());
    sin.addMouseListener(new CaleMouseListener());
    cos.addMouseListener(new CaleMouseListener());

    zero.addMouseListener(new NumberMouseListener());
    one.addMouseListener(new NumberMouseListener());
    two.addMouseListener(new NumberMouseListener());
    three.addMouseListener(new NumberMouseListener());
    four.addMouseListener(new NumberMouseListener());
    five.addMouseListener(new NumberMouseListener());
    six.addMouseListener(new NumberMouseListener());
    seven.addMouseListener(new NumberMouseListener());
    eight.addMouseListener(new NumberMouseListener());
    nine.addMouseListener(new NumberMouseListener());

   f.setVisible(true);

   f.pack();
   f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

2个回答

qq_24981079
qq_24981079   2016.04.19 19:48

private class NumberMouseListener implements MouseListener{
public void mouseClicked(MouseEvent c){
if (operator==""){
if(input.getText().trim().equals("0")|input.getText().trim().equals("")){
input.setText(((JButton)c.getSource()).getText().trim());
}
else{
input.setText(input.getText().concat(((JButton)c.getSource()).getText().trim()));
}
if(operator==""){
show.setText("");
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
}
private class CaleMouseListener implements MouseListener{
public void mouseClicked1(MouseEvent e){
double s=0
if(((JButton)e.getSource()).getText().trim()=="-")
{
if(input.getText().trim().equals("0")|input.getText().trim().equals(""))
{
input.setText("-");
return;
}
}
if(e.getSource()==sqrt)
{
double t=Math.sqrt(Double.parseDouble(input.getText().trim()));
input.setText(new DecimalFormat("0.00").format(t));
}
else if(e.getSource()==dao)
{
double t=1/Double.parseDouble(input.getText().trim());
input.setText(new DecimalFormat("0.00").format(t));
}
else if(e.getSource()==sin)
{
double t=Math.sin(Double.parseDouble(input.getText().trim()));
input.setText(new DecimalFormat("0.00").format(t));
}
else if(e.getSource()==cos)
{
double t=Math.cos(Double.parseDouble(input.getText().trim()));
input.setText(new DecimalFormat("0.00").format(t));
}
else
{
String num1;
if(operator=="")
{
num1=input.getText();
operator=((JButton)e.getSource()).getText();
show.setText(getText().concat(((JButton)e.getSource()).getText()));
input.setText("0");
}
else{//实现连续运算
if(operator.equals("+")){
s=Double.parseDouble(num1)+
Double.parseDouble(input.getText().trim());
}else if(operator.equals("-")){
s=Double.parseDouble(num1)-
Double.parseDouble(input.getText().trim());
}else if(operator.equals("*")){
s=Double.parseDouble(num1)*
Double.parseDouble(input.getText().trim());
}else if(operator.equals("/")){
s=Double.parseDouble(num1)/
Double.parseDouble(input.getText().trim());
}else if(operator.equals("^")){
s=Math.pow(Double.parseDouble(num1), s);
Double.parseDouble(input.getText().trim());
}
else if(operator.equals("%")){
s=getMod(Double.parseDouble(num1),
Double.parseDouble(input.getText().trim()));
}
num1=new DecimalFormat("0.00").format(s);
show.setText(num1.getText().concat(((JButton)e.getSource()).getText()));
input.setText("0");
operator=((JButton)e.getSource()).getText().trim();
}
}
}
String getText() {
// TODO Auto-generated method stub
return null;
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub

}
@Override
public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub

}
@Override
public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub

}
@Override
public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub

}
@Override
public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub

}
}

private double getMod(double a,double b){
double s1=0;
if (a<b)
s1=a;
else if(a==b)
s1=0;
else
s1=a%b;
return s1;
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
}
class OperationMouseListener implements MouseListener{
public void mouseClicked1(MouseEvent e){
AbstractButton inputText;
String num1;
if(e.getSource()=="backspace"){
String inputText1=input.getText();
String showText=show.getText();
//if(inputText!=""){
if(inputText1.length()>0)
inputText1.setText(inputText1.substring(0,inputText1.length()-1));
else if(inputText1.length()==0&&showText.length()>0)
{inputText1.setText(showText.substring(0,showText.length()-1));
show.setText("0");
}
}else if(e.getSource=="C"){
inputText.setText("0");
show.setText("0");
num1="";
operator="";
}else if(e.getSource()=="CE"){
inputText.setText("0");
}else if(e.getSource()=="decimal"){
String text=inputText.getText().trim();
if(text.indexOf(".")==-1){
inputText.setText(text.concat("."));
}
}else if(e.getSource()=="equal"){
if(!operator.trim().equals("")){
if(!inputText.getText().trim().equals("")){
show.setText(show.getText().concat(inputText.getText()).concat("="));
double result=0;
if(operator.equals("+")){
result=Double.parseDouble(num1)+
Double.parseDouble(inputText.getText().trim());
}else if(operator.equals("-")){
result=Double.parseDouble(num1)-
Double.parseDouble(inputText.getText().trim());
}else if(operator.equals("*")){
result=Double.parseDouble(num1)*
Double.parseDouble(inputText.getText().trim());
}else if(operator.equals("/")){
result=Double.parseDouble(num1)/
Double.parseDouble(inputText.getText().trim());
}else if(operator.equals("^")){
result=Math.pow(Double.parseDouble(num1),
Double.parseDouble(inputText.getText().trim()));
}else if(operator.equals("%")){
result=getMod(Double.parseDouble(num1),
Double.parseDouble(inputText.getText().trim()));
}
inputText.setText(new DecimalFormat("0.00").format(result));
operator="";
num1="";
}
}
}
}
private Object textindex() {
    // TODO Auto-generated method stub
    return null;
}
private double getMod(double a,double b){
//TODO Auto-generated method stub
double s=0;
if(a>b)
s=a;
else if(a==b)
s=0;
else
s=a%b;
return s;
}
public void mouseEntered1(MouseEvent e){}
public void mouseExited1(MouseEvent e){}
public void mousePressed1(MouseEvent e){}
public void mouseReleased1(MouseEvent e){}










    public void mouseEntered111(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    public void mouseExited111(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    public void mousePressed111(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    public void mouseReleased111(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

@Override
public void mouseClicked(MouseEvent e) {
    // TODO Auto-generated method stub

}
public void mouseEntered11(MouseEvent e) {
    // TODO Auto-generated method stub

}
public void mouseExited11(MouseEvent e) {
    // TODO Auto-generated method stub

}
@Override
public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub

}
@Override
public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub

}


    @Override
    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    public String getText() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    public void mousePressed11(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    public void mouseReleased11(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }





}

}

}

CSDNXIAON
CSDNXIAON   2016.04.19 21:14

android简易计算机报错
----------------------同志你好,我是CSDN问答机器人小N,奉组织之命为你提供参考答案,编程尚未成功,同志仍需努力!

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!