m0_68497476 2022-04-02 14:01 采纳率: 50%
浏览 68
已结题

这是一个简易计算器,设置四个按钮代表加减乘除,然后三个文本框,前两个文本框输入,第三个文本框显示结果,帮我看看哪出问题了,

package text2;

public class Calculator {
public static void main(String[] args) {
WindowCalculator win = new WindowCalculator();
win.setTitle("计算");
}
}

package text2;

import java.awt.;
import java.awt.event.ActionListener;
import javax.swing.
;
import javax.swing.event.*;

public class WindowCalculator extends JFrame implements DocumentListener{
JTextArea t

Button button1,button2,button3,button4;
Panel panel = new Panel();



WindowCalculator(){

    

    text1 = new JTextArea(8,8);
    text2 = new JTextArea(8,8);
    text3 = new JTextArea(8,8);
    
    add(text1);
    add(text2);
    add(text3);
    text1.setLineWrap(true);
    text2.setLineWrap(true);
    text3.setLineWrap(true);
    text3.setEditable(false);
    (text1.getDocument()).addDocumentListener(this);
    (text2.getDocument()).addDocumentListener(this);
    (text3.getDocument()).addDocumentListener(this);


    JButton button1 = new JButton("加法运算");
    JButton button2 = new JButton("减法运算");
    JButton button3 = new JButton("乘法运算");
    JButton button4 = new JButton("除法运算");
    panel.add(button1);
    panel.add(button2);
    panel.add(button3);
    panel.add(button4);
    add(panel);
    
    setLayout(new FlowLayout());
    setBounds(320,200,700,400);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    

    button1.addActionListener((ActionListener) this);
    button2.addActionListener((ActionListener) this);
    button3.addActionListener((ActionListener) this);
    button4.addActionListener((ActionListener) this);
}


public void insertUpdate(DocumentEvent e) {
    try{
    double A = Double.parseDouble(text1.getText());
    double B = Double.parseDouble(text2.getText());
    double result = 0;
    
       if (panel.equals("button1")){
          result = A + B;
       }
       else if (panel.equals("button2")){
           result = A - B;
       }
       else if (panel.equals("button3")){
           result = A * B;
       }
       else if (panel.equals("button4")){
           result = A / B;
       }
    text3.append("结果是:" + result);
   }
   catch(Exception exp){
       text3.append("字符错误,请删除后重新输入:");
   }
    
}


public void removeUpdate(DocumentEvent e) {
    
    changedUpdate(e);
}


public void changedUpdate(DocumentEvent e) {
    changedUpdate(e);
}

}

  • 写回答

2条回答 默认 最新

  • zcl_1991 2022-04-02 14:35
    关注

    代码都不全,

    img

    img

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月10日
  • 已采纳回答 4月2日
  • 创建了问题 4月2日

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度