求"用JAVA编写的计算器程序代码"

网上有许多资源,但好多不能运行,而且与我要的代码不太相符.我希望这个代码是用JFrame,JButton等来编写的,而不是用Frame,Button编的,最好能够加上说明,谢谢!(刚学JAVA没多久,编程方面很差...)
对了,最好能进行开方运算(sqrt),谢谢!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class jsq
{
public static void main(String[] str)
{
jisuanqi jsq=new jisuanqi();
jsq.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jsq.setVisible(true);jsq.setSize(300,300);
}
}
class jisuanqi extends JFrame implements ActionListener
{
double a=0,b=0,c=0,fuhao=5;
Boolean has=false;

Container A;
JTextField tf1;
JButton bt1,bt2,bt3,bt4,bt5,bt6,bt7,bt8,bt9,bt0,bt_dian;
JButton bt_jia,bt_jian,bt_cheng,bt_chu,bt_fuhao;
JButton bt_clear,bt_jisuan;
public jisuanqi()
{
setTitle("简单计算器");
A=getContentPane();
GridBagLayout f=new GridBagLayout();
A.setLayout(f);
GridBagConstraints g=new GridBagConstraints();
g.fill=GridBagConstraints.BOTH;
g.gridwidth=1;
g.gridheight=1;
//面板的实例化

//显示框的实例化
tf1=new JTextField(10);tf1.setEditable(false);
//数字键的实例化
bt1=new JButton("1");bt2=new JButton("2");bt3=new JButton("3");
bt4=new JButton("4");bt5=new JButton("5");bt6=new JButton("6");
bt7=new JButton("7");bt8=new JButton("8");bt9=new JButton("9");
bt0=new JButton("0");bt_dian=new JButton(".");bt_fuhao=new JButton("+/-");
//计算符号的实例化
bt_jia=new JButton("+");bt_jian=new JButton("-");
bt_cheng=new JButton("*");bt_chu=new JButton("/");
//清空和计算按钮的实例化
bt_clear=new JButton("clear");bt_jisuan=new JButton("=");
//布局

g.gridx=1;g.gridy=0;A.add(tf1,g);

g.gridx=0;g.gridy=1;A.add(bt_clear,g);
g.gridx=3;g.gridy=1;A.add(bt_jisuan,g);

g.gridx=0;g.gridy=2;A.add(bt7,g);
g.gridx=1;g.gridy=2;A.add(bt8,g);
g.gridx=2;g.gridy=2;A.add(bt9,g);
g.gridx=3;g.gridy=2;A.add(bt_jia,g);

g.gridx=0;g.gridy=3;A.add(bt4,g);
g.gridx=1;g.gridy=3;A.add(bt5,g);
g.gridx=2;g.gridy=3;A.add(bt6,g);
g.gridx=3;g.gridy=3;A.add(bt_jian,g);

g.gridx=0;g.gridy=4;A.add(bt1,g);
g.gridx=1;g.gridy=4;A.add(bt2,g);
g.gridx=2;g.gridy=4;A.add(bt3,g);
g.gridx=3;g.gridy=4;A.add(bt_cheng,g);

g.gridx=0;g.gridy=5;A.add(bt0,g);
g.gridx=1;g.gridy=5;A.add(bt_fuhao,g);
g.gridx=2;g.gridy=5;A.add(bt_dian,g);
g.gridx=3;g.gridy=5;A.add(bt_chu,g);

//添加监听
bt1.addActionListener(this);
bt2.addActionListener(this);
bt3.addActionListener(this);
bt4.addActionListener(this);
bt5.addActionListener(this);
bt6.addActionListener(this);
bt7.addActionListener(this);
bt8.addActionListener(this);
bt9.addActionListener(this);
bt0.addActionListener(this);
//清除、小数点、符号添加监听
bt_clear.addActionListener(this);
bt_dian.addActionListener(this);
bt_fuhao.addActionListener(this);
//符号添加监听
bt_jia.addActionListener(this);
bt_jian.addActionListener(this);
bt_cheng.addActionListener(this);
bt_chu.addActionListener(this);
//计算符号添加监听
bt_jisuan.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
//数字键
if(e.getSource()==bt1)
{tf1.setText(tf1.getText()+"1");}
else if(e.getSource()==bt2)
{
if(!tf1.getText().equals("0"))
tf1.setText(tf1.getText()+"2");
}
else if(e.getSource()==bt3)
{
if(!tf1.getText().equals("0"))
tf1.setText(tf1.getText()+"3");
}
else if(e.getSource()==bt4)
{
if(!tf1.getText().equals("0"))
tf1.setText(tf1.getText()+"4");
}
else if(e.getSource()==bt5)
{
if(!tf1.getText().equals("0"))
tf1.setText(tf1.getText()+"5");
}
else if(e.getSource()==bt6)
{
if(!tf1.getText().equals("0"))
tf1.setText(tf1.getText()+"6");
}
else if(e.getSource()==bt7)
{
if(!tf1.getText().equals("0"))
tf1.setText(tf1.getText()+"7");
}
else if(e.getSource()==bt8)
{
if(!tf1.getText().equals("0"))
tf1.setText(tf1.getText()+"8");
}
else if(e.getSource()==bt9)
{
if(!tf1.getText().equals("0"))
tf1.setText(tf1.getText()+"9");
}
else if(e.getSource()==bt0)
{
if(!tf1.getText().equals("0"))
{
tf1.setText(tf1.getText()+"0");
}
}
else if(e.getSource()==bt_dian)//小数点符号
{
if(tf1.getText().indexOf(".")==-1&&tf1.getText().length()>0)
{
tf1.setText(tf1.getText()+".");
}
}

//运算符号
//fuhao的0,1,2,3分别表示加、减、乘、除
else if(e.getSource()==bt_jia)
{
if(tf1.getText().trim().length()>0)
{ a=Double.valueOf(tf1.getText());
tf1.setText("");fuhao=0;
has=true;
}

}
else if(e.getSource()==bt_jian)
{
if(tf1.getText().trim().length()>0)
{
a=Double.valueOf(tf1.getText());
tf1.setText("");fuhao=1;
has=true;
}
}
else if(e.getSource()==bt_cheng)
{
if(tf1.getText().trim().length()>0)
{
a=Double.valueOf(tf1.getText());
tf1.setText("");fuhao=2;
has=true;
}
}
else if(e.getSource()==bt_chu)
{
if(tf1.getText().trim().length()>0)
{
a=Double.valueOf(tf1.getText());
tf1.setText("");fuhao=3;
has=true;
}
}

//计算、符号、清空
else if(e.getSource()==bt_jisuan)//计算结果
{
try
{
if(tf1.getText().length()>0&&has)
{
b=Double.valueOf(tf1.getText());
if(fuhao==0)
{
c=a+b;
tf1.setText(String.valueOf(c));
has=false;
}
else if(fuhao==1)
{
c=a-b;
tf1.setText(String.valueOf(c));
has=false;
}
else if(fuhao==2)
{
c=a*b;
tf1.setText(String.valueOf(c));
has=false;
}
else if(fuhao==3)
{
c=a/b;
tf1.setText(String.valueOf(c));
has=false;
}
}
}
catch(Exception ex)
{tf1.setText(ex.getMessage().toString());}

}
else if(e.getSource()==bt_fuhao)
{
if(tf1.getText().indexOf("-")==-1&&tf1.getText().length()>0)
{
String s=tf1.getText();
tf1.setText("-"+s);
}
else
{
if(tf1.getText().length()>0)
{
String s=tf1.getText().substring(1);
tf1.setText(s);
}

}
}
else if(e.getSource()==bt_clear)
{
tf1.setText("");
}

}

}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-10-16
import java.awt.*;//引入包java.awt中所有的类
import java.awt.event.*;//引入包java.awt.event中所有的类.
public class Calculator extends WindowAdapter implements ActionListener//创建Calculator类,

实现ActionListener接口.
{
private double result=0,data1=0,radixPointDepth=1;//定义变量
private boolean radixPointIndicate=false,resultIndicate=false;
private char prec='+';//创建优先默认字符"+"
private Frame f;//创建窗口
private TextField tf;//创建文本框
private Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17;//创建按钮
private Panel p;//创建/面板容器
static public void main(String args[])//main方法,创建calGUI(图形用户界面),完成初试化
{
//构造器
Calculator de=new Calculator();//创建构造方法
de.go();
}
public void go()
{
f=new Frame("计算器");
p=new Panel();//运算界面p
p.setLayout(new GridLayout(4,4)); // 设置p的布局为GridLayout,四行四列
tf=new TextField(30);
//实例化按钮

b1=new Button("7");
b2=new Button("8");
b3=new Button("9");
b4=new Button("+");
b5=new Button("4");
b6=new Button("5");
b7=new Button("6");
b8=new Button("-");
b9=new Button("1");
b10=new Button("2");
b11=new Button("3");
b12=new Button("*");
b13=new Button("0");
b14=new Button(".");
b15=new Button("=");
b16=new Button("/");
b17=new Button("清零");
f.add(tf,"North"); //把文本区域添加到框架的上方
f.add(p,"Center"); //把面版添加到框架的中间
f.add(b17,"South"); //把按钮(清零)添加到框架的下方
//把按钮添加到面版上
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(b5);
p.add(b6);
p.add(b7);
p.add(b8);
p.add(b9);
p.add(b10);
p.add(b11);
p.add(b12);
p.add(b13);
p.add(b14);
p.add(b15);
p.add(b16);
//为按钮添加监听
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
b17.addActionListener(this);
f.addWindowListener(this); //为框架添加监听
f.setSize(300,190);//设置框架的大小
f.setVisible(true);//设置框架为可见
}
//监听程序
public void actionPerformed(ActionEvent e)
{
String s;
s=e.getActionCommand();
//SWITCH开关
switch(s.charAt(0))
{
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case

'7': case '8': case '9'://按了“0-9”,就执行下面
if(resultIndicate)
{
result=0;
data1=0;
prec='+';
}
Integer Int1=new Integer(s);
if(radixPointIndicate)
{
radixPointDepth=radixPointDepth/10;
data1=data1+(Int1.intValue())*radixPointDepth;
}
else
{
data1=data1*10+(Int1.intValue());
}
Double displayNumber=new Double(data1);
tf.setText(displayNumber.toString());
resultIndicate=false;
break;
case '+': case '-':case '*':case '/':case '='://按了“+、-、*、/”,就

执行下面
if(s.charAt(0)!='='&&resultIndicate)
{
prec=s.charAt(0);
resultIndicate=false;
}
else
{
//用SWITCH开关运算出执行了“+、-、*、/”的结果
switch(prec)
{
case '+':
result=result+data1;
break;
case '-':
result=result-data1;
break;
case '*':
result=result*data1;
break;
case '/':
result=result/data1;
break;
}
}
radixPointIndicate=false;
radixPointDepth=1;
displayNumber=new Double(result);
tf.setText(displayNumber.toString());
//监听是否按了“=”
if(s.charAt(0)!='=')
{
data1=0;
prec=s.charAt(0);
}
else
{
resultIndicate=true;
}
break;
case '.':
radixPointIndicate=true;
break;
}
//监听是否按了为“清零”,是则对各数据清零
if(s.equals("清零"))
{
result=0;
data1=0;
radixPointDepth=1;
tf.setText("");
}
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
第2个回答  2009-10-16
import java.util.*;

public class AddServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";

//Initialize global variables
public void init() throws ServletException {
}

//Process the HTTP Get request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();

//提取表单信息
String name = request.getParameter("name").trim();
String school = request.getParameter("school").trim();
//封装信息
StudentForm sf = new StudentForm();
sf.setName(name);
sf.setScholl(school);
//保存信息
StudentBean studentBean = new StudentBean();
studentBean.addStudent(sf);
response.sendRedirect("searchservlet");
out.close();
}

//Clean up resources
public void destroy() {
}
}

package com.jimex;
import java.sql.*;
import oracle.jdbc.pool.OracleDataSource;
public class DBConnection {
private static OracleDataSource ods = null;
private static Connection con = null;
public DBConnection(){
try {
ods = new OracleDataSource();
ods.setURL("jdbc:oracle:thin:@localhost:1521:ldy");
ods.setUser("scott");
ods.setPassword("tiger");
con = ods.getConnection();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static Connection getConnection(){
try {
if(con ==null){
new DBConnection();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return con;
}
public static void main(String [] args){
try {
Connection con = DBConnection.getConnection();
System.out.println("con=="+con);
} catch (Exception ex) {

}
}

}
相似回答