求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;
}
}
}