问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

求JAVA编程 计算器

发布网友 发布时间:2022-11-19 12:20

我来回答

3个回答

热心网友 时间:2024-10-21 09:26

一个加法计算器,其他的运算可以类似做出来。

import javax.swing.*;
import java.awt.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.*;

public class PlusCaculator
extends JFrame {
JTextField tfNum1 = new JTextField();
JTextField tfNum2 = new JTextField();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JTextField tfAnswer = new JTextField();
JButton btnCaculate = new JButton();

// 下面两句比较关键
DefaultTableModel tableModel = new DefaultTableModel(); //存放jtable中的数据的 tableModel
JTable tabAnswer = new JTable(tableModel);

JScrollPane jScrollPane1 = new JScrollPane();

public PlusCaculator() {
try {
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
}
}

void jbInit() throws Exception {
jScrollPane1.setBounds(new Rectangle(24, 57, 354, 230));
this.getContentPane().setLayout(null);
tfNum1.setMinimumSize(new Dimension(11, 21));
tfNum1.setPreferredSize(new Dimension(11, 21));
tfNum1.setText("");
tfNum1.setBounds(new Rectangle(32, 21, 66, 21));
tfNum2.setText("");
tfNum2.setBounds(new Rectangle(126, 22, 65, 21));
jLabel1.setText("+");
jLabel1.setBounds(new Rectangle(105, 25, 17, 16));
jLabel2.setText("=");
jLabel2.setBounds(new Rectangle(205, 27, 16, 16));
tfAnswer.setText("");
tfAnswer.setBounds(new Rectangle(228, 23, 64, 21));
btnCaculate.setBounds(new Rectangle(306, 21, 68, 25));
btnCaculate.setText("计算");
btnCaculate.addActionListener(new
PlusCaculator_btnCaculate_actionAdapter(this));
this.setLocale(java.util.Locale.getDefault());
this.getContentPane().add(tfNum2, null);
this.getContentPane().add(jScrollPane1, null);
this.getContentPane().add(tfNum1, null);
this.getContentPane().add(jLabel1, null);
this.getContentPane().add(tfAnswer, null);
this.getContentPane().add(btnCaculate, null);
this.getContentPane().add(jLabel2, null);
jScrollPane1.getViewport().add(tabAnswer, null);

// 初始化 tableModel, 一共有3列
tableModel.addColumn("加数1");
tableModel.addColumn("加数2");
tableModel.addColumn("结果");
}

public static void main(String[] args) {
PlusCaculator plusCaculator = new PlusCaculator();
plusCaculator.setTitle("简单加法计算器");
plusCaculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
plusCaculator.setLocation(200, 200);
plusCaculator.setSize(500, 400);
plusCaculator.setVisible(true);
}

// 点击 计算按钮执行的方法
void btnCaculate_actionPerformed(ActionEvent e) {
try {
int num1 = Integer.parseInt(tfNum1.getText().trim()); // 加数1
int num2 = Integer.parseInt(tfNum2.getText().trim()); //加数2
int sum = num1 + num2;
tfAnswer.setText("" + sum);

// 将结果加到 Jtable中
tableModel.addRow(new String[] {"" + num1, "" + num2, "" + sum});
}
catch (Exception ex) {
ex.printStackTrace();
}

}
}

class PlusCaculator_btnCaculate_actionAdapter
implements java.awt.event.ActionListener {
PlusCaculator adaptee;

PlusCaculator_btnCaculate_actionAdapter(PlusCaculator adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.btnCaculate_actionPerformed(e);
}
}

热心网友 时间:2024-10-21 09:26

//加减乘除//10分很便宜啊
import java.awt.*;
import java.awt.event.*;
class Window extends Frame implements ActionListener
{ TextField text1,text2,text3;
Label label1,label2;
Button button1,button2,button3,button4;
Window()
{ setLayout(new FlowLayout());
text1=new TextField(6);
text2=new TextField(6);
text3=new TextField(10);
Panel p1=new Panel();
p1.setBackground(Color.pink);
label1=new Label();
label2=new Label("=");
p1.add(text1);
p1.add(label1);
p1.add(text2);
p1.add(label2);
p1.add(text3);
text3.setEditable(false);

Panel p2=new Panel();
p2.setBackground(Color.pink);
button1=new Button("加");
button2=new Button("减");
button3=new Button("乘");
button4=new Button("除");
p2.add(button1);
p2.add(button2);
p2.add(button3);
p2.add(button4);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
add(p1);
add(p2);
setBounds(120,125,350,300);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e)
{
String a=text1.getText();
String b=text2.getText();
if(e.getSource()==button1)
{
try
{
int n=Integer.parseInt(a);
int m=Integer.parseInt(b);
int sum=n+m;
label1.setText("+");
text3.setText(""+sum);
}
catch(NumberFormatException event)
{
text3.setText("error");
}
}
if(e.getSource()==button2)
{
try
{
int n=Integer.parseInt(a);
int m=Integer.parseInt(b);
int sub=n-m;
label1.setText("-");
text3.setText(""+sub);
}
catch(NumberFormatException event)
{
text3.setText("error");
}
}
if(e.getSource()==button3)
{
try
{
int n=Integer.parseInt(a);
int m=Integer.parseInt(b);
int mul=n*m;
label1.setText("*");
text3.setText(""+mul);
}
catch(NumberFormatException event)
{
text3.setText("error");
}
}
if(e.getSource()==button4)
{
try
{
int n=Integer.parseInt(a);
int m=Integer.parseInt(b);
int div=n/m;
label1.setText("/");
text3.setText(""+div);
}
catch(NumberFormatException event)
{
text3.setText("error");
}
}
}
}
public class Test3
{ public static void main(String args[])
{
new Window();
}
}

热心网友 时间:2024-10-21 09:27

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.Color;
public class W1 extends Frame implements ActionListener
{
Button number0,number1,number2,number3,number4,number5,number6,number7,number8,number9;
Button backspace,clear,point,oper_add,oper_sub,oper_mul,oper_div,sqrt,postive,reflect,abs,amount;
TextField display;
String s_operator1="",s;
String s_operator2="";
boolean oper2_begin=false;
boolean pointed=false;
char pressoper;
W1()
{
super("科学计算器");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
setLayout(null);
setSize(380,310);
number0=new Button("0");
number1=new Button("1");
number2=new Button("2");
number3=new Button("3");
number4=new Button("4");
number5=new Button("5");
number6=new Button("6");
number7=new Button("7");
number8=new Button("8");
number9=new Button("9");
backspace=new Button("backspace");
clear=new Button("clear");
oper_add=new Button("+");
oper_sub=new Button("-");
oper_mul=new Button("x");
oper_div=new Button("/");
postive=new Button("+/-");
reflect=new Button("1/x");
abs=new Button("abs");
sqrt=new Button("sqrt");
point=new Button(".");
amount=new Button("=");
display=new TextField("");
display.setText("0.");
number0.setForeground(Color.blue);
number1.setForeground(Color.blue);
number2.setForeground(Color.blue);
number3.setForeground(Color.blue);
number4.setForeground(Color.blue);
number5.setForeground(Color.blue);
number6.setForeground(Color.blue);
number7.setForeground(Color.blue);
number8.setForeground(Color.blue);
number9.setForeground(Color.blue);
oper_add.setForeground(Color.red);
oper_sub.setForeground(Color.red);
oper_mul.setForeground(Color.red);
oper_div.setForeground(Color.red);
postive.setForeground(Color.blue);
reflect.setForeground(Color.blue);
abs.setForeground(Color.blue);
sqrt.setForeground(Color.blue);
point.setForeground(Color.blue);
amount.setForeground(Color.red);
backspace.setForeground(Color.red);
clear.setForeground(Color.red);
add(display);
add(backspace);
add(clear);
add(number7);
add(number8);
add(number9);
add(oper_div);
add(sqrt);
add(number4);
add(number5);
add(number6);
add(oper_mul);
add(abs);
add(number1);
add(number2);
add(number3);
add(oper_sub);
add(reflect);
add(number0);
add(postive);
add(point);
add(oper_add);
add(amount);
display.setBounds(50,60,290,30);
backspace.setBounds(50,100,80,30);
clear.setBounds(260,100,80,30);
number7.setBounds(50,140,50,30);
number8.setBounds(110,140,50,30);
number9.setBounds(170,140,50,30);
oper_div.setBounds(230,140,50,30);
sqrt.setBounds(290,140,50,30);
number4.setBounds(50,180,50,30);
number5.setBounds(110,180,50,30);
number6.setBounds(170,180,50,30);
oper_mul.setBounds(230,180,50,30);
abs.setBounds(290,180,50,30);
number1.setBounds(50,220,50,30);
number2.setBounds(110,220,50,30);
number3.setBounds(170,220,50,30);
oper_sub.setBounds(230,220,50,30);
reflect.setBounds(290,220,50,30);
number0.setBounds(50,260,50,30);
postive.setBounds(110,260,50,30);
point.setBounds(170,260,50,30);
oper_add.setBounds(230,260,50,30);
amount.setBounds(290,260,50,30);
number0.addActionListener(this);
number1.addActionListener(this);
number2.addActionListener(this);
number3.addActionListener(this);
number4.addActionListener(this);
number5.addActionListener(this);
number6.addActionListener(this);
number7.addActionListener(this);
number8.addActionListener(this);
number9.addActionListener(this);
oper_add.addActionListener(this);
oper_sub.addActionListener(this);
oper_mul.addActionListener(this);
oper_div.addActionListener(this);
amount.addActionListener(this);
point.addActionListener(this);
abs.addActionListener(this);
sqrt.addActionListener(this);
reflect.addActionListener(this);
postive.addActionListener(this);
backspace.addActionListener(this);
clear.addActionListener(this);
this.setResizable(false);
setBounds(234,200,380,310);
show();
}
public static void main(String args[])
{
new W1();
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="1")
{
if(oper2_begin==true)
{
s_operator2+=e.getActionCommand();
display.setText(s_operator2);
}
else
{
s_operator1+=e.getActionCommand();
display.setText(s_operator1);
}
}
if(e.getActionCommand()=="2")
{
if(oper2_begin==true)
{
s_operator2+=e.getActionCommand();
display.setText(s_operator2);
}
else
{
s_operator1+=e.getActionCommand();
display.setText(s_operator1);
}
}
if(e.getActionCommand()=="3")
{
if(oper2_begin==true)
{
s_operator2+=e.getActionCommand();
display.setText(s_operator2);
}
else
{
s_operator1+=e.getActionCommand();
display.setText(s_operator1);
}
}
if(e.getActionCommand()=="4")
{
if(oper2_begin==true)
{
s_operator2+=e.getActionCommand();
display.setText(s_operator2);
}
else
{
s_operator1+=e.getActionCommand();
display.setText(s_operator1);
}
}
if(e.getActionCommand()=="5")
{
if(oper2_begin==true)
{
s_operator2+=e.getActionCommand();
display.setText(s_operator2);
}
else
{
s_operator1+=e.getActionCommand();
display.setText(s_operator1);
}
}
if(e.getActionCommand()=="6")
{
if(oper2_begin==true)
{
s_operator2+=e.getActionCommand();
display.setText(s_operator2);
}
else
{
s_operator1+=e.getActionCommand();
display.setText(s_operator1);
}
}
if(e.getActionCommand()=="7")
{
if(oper2_begin==true)
{
s_operator2+=e.getActionCommand();
display.setText(s_operator2);
}
else
{
s_operator1+=e.getActionCommand();
display.setText(s_operator1);
}
}
if(e.getActionCommand()=="8")
{
if(oper2_begin==true)
{
s_operator2+=e.getActionCommand();
display.setText(s_operator2);
}
else
{
s_operator1+=e.getActionCommand();
display.setText(s_operator1);
}
}
if(e.getActionCommand()=="9")
{
if(oper2_begin==true)
{
s_operator2+=e.getActionCommand();
display.setText(s_operator2);
}
else
{
s_operator1+=e.getActionCommand();
display.setText(s_operator1);
}
}
if(e.getActionCommand()=="0")
{
if(oper2_begin==true)
{
s_operator2+=e.getActionCommand();
display.setText(s_operator2);
}
else
{
s_operator1+=e.getActionCommand();
display.setText(s_operator1);
}
}
if(e.getActionCommand()=="+")
{
s_operator1=display.getText();
display.setText("");
s_operator2="";
oper2_begin=true;
pointed=false;
pressoper='+';
}
if(e.getActionCommand()=="-")
{
s_operator1=display.getText();
display.setText("");
s_operator2="";
oper2_begin=true;
pointed=false;
pressoper='-';
}
if(e.getActionCommand()=="x")
{
s_operator1=display.getText();
s_operator2="";
display.setText("");
oper2_begin=true;
pointed=false;
pressoper='*';
}
if(e.getActionCommand()=="/")
{
s_operator1=display.getText();
s_operator2="";
display.setText("");
oper2_begin=true;
pointed=false;
pressoper='/';
}
if(e.getActionCommand()=="sqrt")
{
display.setText(String.valueOf(Math.sqrt(Float.parseFloat(display.getText()))));
s_operator1=display.getText();
s_operator2="";
pressoper=' ';
pointed=false;
oper2_begin=true;
}
if(e.getActionCommand()=="1/x")
{
if(Float.parseFloat(display.getText())!=0.0f)
display.setText(String.valueOf(1.0/Float.parseFloat(display.getText())));
s_operator1=display.getText();
s_operator2="";
pressoper=' ';
oper2_begin=true;
}
if(e.getActionCommand()=="abs")
{
display.setText(String.valueOf(Math.abs(Float.parseFloat(display.getText()))));
s_operator1=display.getText();
s_operator2="";
pressoper=' ';
oper2_begin=true;
}
if(e.getActionCommand()=="+/-")
{ s=display.getText();
s=String.valueOf(0.0-Float.parseFloat(s));
display.setText(s);
if(oper2_begin==false)
s_operator1=s;
}
if(e.getActionCommand()=="=")
if(s_operator1!="" && s_operator2!="")
{
switch(pressoper)
{
case'+':
display.setText(String.valueOf(Float.parseFloat(s_operator1)+Float.parseFloat(s_operator2)));
break;
case'-':
display.setText(String.valueOf(Float.parseFloat(s_operator1)-Float.parseFloat(s_operator2)));
break;
case'*':
display.setText(String.valueOf(Float.parseFloat(s_operator1)*Float.parseFloat(s_operator2)));
break;
case'/':
display.setText(String.valueOf(Float.parseFloat(s_operator1)/Float.parseFloat(s_operator2)));
break;
}
s_operator1=display.getText();
s_operator2="";
pressoper=' ';
oper2_begin=true;
}
if(e.getActionCommand()=="." && pointed==false)
{
if(oper2_begin==false)
{ if(s_operator1=="")
{
pointed=true;
display.setText(s_operator1="0"+e.getActionCommand());
}
else
{
pointed=true;
display.setText(s_operator1+=e.getActionCommand());
}
}
else
{
if(s_operator2=="")
{
pointed=true;
display.setText(s_operator2="0"+e.getActionCommand());
}
else
{
pointed=true;
display.setText(s_operator2+=e.getActionCommand());
}
}
}
if(e.getActionCommand()=="backspace")
{
s=display.getText();
s=s.substring(0,s.length()-1);
display.setText(s);
if(oper2_begin==false)
s_operator1=s;
}
if(e.getActionCommand()=="clear")
{
s_operator1="";
s_operator2="";
display.setText("0.");
oper2_begin=false;
pointed=false;
}
}
}
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
孩子依赖性太强,妈妈怎么办?目录 孩子对妈妈依赖性太强怎么办 介绍权利的游戏里龙之母全名是什么? 澳洲血橙胶原蛋白是玻璃瓶的吗? 梦见母亲侄子妹妹舅妈电话破屋的预兆 梦见坐别人的车什么意思 有多少人去拜过“龙母”,又有几人知道“龙母”是真的存在过 龙母庙地址在哪里? 龙母像地址在哪里? 龙姥姥是龙母娘娘吗 what is factory press operator upx脱壳后如何再加壳?加什么壳? 天然气有没有好坏之分 燃气灶怎么看好坏? 燃气灶怎么看好坏 天然气分好坏么 LNG天然气质量好坏区别在那儿 yii2 datepicker 哪个好用 关于学习PID控制 你可以加我的微信,我的是。。。用英语怎么说。谢谢 三相电有L,N,E,W吗? 三相电颜色分别是啥? ...但是可以保证针头肉眼看上去很干净,·不知道有没有必要 车载转换器能用洗车机吗 车载转换器能转换36V电压吗 英语白雪公主作文初中水平,简短写 i am poisonous是什么意思 withapoisonousapple什么意思 手机电池发胀,是什么原因呀? 左转弯待转区什么意思? 什么叫丝印首件,为什么要做丝印首件 ipqc做首件为了什么 ...小程序路径为空,如何设置才能实现正常跳转? s7-1200粮库监测怎么样 面试中国人寿扫码填的信息是 测试性格完成要扫码吗 电气火灾监控系统(属于消防范畴)项目的销售工作该怎么进行说的详细点... ...和SOP-8是一个东西么? SOP SOIC 有封装有什么区别? 谢谢 bq2057封装问题,sop8与SOIC、SMOP有什么不同 东阳魏山镇有共享单车吗? 共享东阳自行车巡检员待遇怎么样 欧拉r1充电桩多少钱? 南京菲拉特电力科技有限公司怎么样? 平湖生态园可以骑共享单车吗 平湖叮嗒出行和公共自行车一样吗 当前阶段,音质最好的MP3品牌有哪些? 有没有比较好的mp3,牌子货。不求便宜,求音质好。 戴尔Precision 7720移动工作站怎么样 dell precision 7720 怎么卸载显卡后找不回来了 光通信哪个研究方向就业前景好