下面两个可以么,是我做实验答辩时用到的!
import java.awt.*;//AWT核心包
import java.awt.event.*;//提供事件类和监听器
public class Counter extends Frame implements ActionListener
{
TextField t=new TextField("");//文本框
Panel p1=new Panel();//new一个panel,用于存放数字键和符号键。
Panel p2=new Panel();//new一个panel,用于存放开方、平方、和清除键。
Button[] b=new Button[10];//实例化Button对象
Button bAdd=new Button("加");
Button bSub=new Button("减");
Button bMul=new Button("乘以");
Button bPoint=new Button(".");
Button bDiv=new Button("除以");
Button bEqual=new Button("等於");
Button bSqrt=new Button("开方");
Button bPow=new Button("平方");
Button bNull=new Button("清除");
String str1=""; //str1和str2存放两个输入的数
String str2="";
String operator=null; //存放加减乘除以及开平方的符号
boolean first=true; //检验输入的是否为第一个数
int countOper=0; //累计输入符号的个数,连加连减等操作中会用到
double result=0.0; //暂存结果
double num1=0.0,num2=0.0; //两个输入的数做运算时转化为double存放
boolean error=false; //检验除数是否为0
//构造方法
public Counter()
{
Frame s=new Frame("计算器");//创建Frame
for(int i=0;i<10;i++)//利用for循环将数字键添加进p1中
{
b[i]=new Button(String.valueOf(i));
p1.add(b[i]);
b[i].setActionCommand("number");
b[i].setForeground(new Color(150,20,20));
b[i].addActionListener(this);//调用addActionListener()方法注册事件监听器
}
p1.add(bPoint);
bPoint.setActionCommand("number");
p1.add(bAdd); //数字键,符号键放置在panel的p1中
p1.add(bSub);
p1.add(bMul);
p1.add(bDiv);
p1.add(bEqual);
p2.add(bSqrt);//开方键,平方键,清除键放置在panel的p2中
p2.add(bPow);
p2.add(bNull);
bAdd.setActionCommand("oper");
bSub.setActionCommand("oper");
bMul.setActionCommand("oper");
bDiv.setActionCommand("oper");
bAdd.setForeground(Color.red);//为组键设计颜色
bSub.setForeground(Color.red);
bMul.setForeground(Color.red);
bDiv.setForeground(Color.red);
bPoint.setForeground(Color.black);
bEqual.setForeground(Color.red);
bSqrt.setForeground(Color.blue);
bPow.setForeground(Color.blue);
bNull.setForeground(Color.blue);
bAdd.addActionListener(this);//调用addActionListener()方法注册事件监听器
bSub.addActionListener(this);
bMul.addActionListener(this);
bDiv.addActionListener(this);
bPoint.addActionListener(this);
bEqual.addActionListener(this);
bSqrt.addActionListener(this);
bPow.addActionListener(this);
bNull.addActionListener(this);
p1.setLayout(new GridLayout(4,4,5,5));//网格布局管理器,把容器根据行数和列数分成同样大小的单元,
//每个单元可容纳一个组件,并且此组件会填满网格单元,不能控
//制其占据网格的大小。4、4为网格的行、列数。5、5为组建之间的
//间距
p2.setLayout(new FlowLayout());//用FlowLayout布局管理器将组建默认剧中排放,默认间隙为5个像素
add(t,"North"); //frame的north放置输入框,panel放置在center和south
add(p1,"Center");//将p1添加到Center中
add(p2,"South");//将p2添加到South中
setLocation(400,200);//设计按钮尺寸
setSize(200,200);//设计窗口尺寸
setBackground(new Color(20,200,10));//设置Frame的背景,默认为白色
setVisible(true);//设置Frame设置为可见
addWindowListener(new WindowAdapter(){ //关闭窗口功能
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
//实现接口ActionListener
public void actionPerformed(ActionEvent e)
{
Button temp=(Button)e.getSource();
if(e.getActionCommand().equals("number"))
{
if(first)
{
str1=str1+temp.getLabel();
t.setText(str1);//将输入的str1显示在文本框中
}
else
{
str2=str2+temp.getLabel();
t.setText(str2);//将输入的str2显示在文本框中
}
}
else if(e.getActionCommand().equals("oper"))
{
if(str1=="") //如果还没有输入数就点击运算符执行if
{
countOper=0;//若此,则将计数清零
first=true;
}
else
{
countOper++;//计算输入符号的个数
if(countOper>1)//若输入的符号个数多余一个,则可以进行计算
{
getResult();
}
operator=temp.getLabel();//存放加减乘除以及开方、平方的符号
first=false;
}
}
else if(e.getActionCommand().equals("开方"))
{
double d=Math.sqrt(Double.parseDouble(str1));
str1=String.valueOf(d);//将计算出来的结果再次传给str1,为连计算准备
t.setText(String.valueOf(d));//将计算出来的结果传至文本框中
first=false;//置为false,即已输入第一个数
}
else if(e.getActionCommand().equals("平方"))
{
double f=Math.pow(Double.parseDouble(str1),2);
str1=String.valueOf(f);
t.setText(String.valueOf(f));
first=false;
}
else if(e.getActionCommand().equals("清除"))
{
str1="";//清空
str2="";
t.setText("");//将文本框清空
countOper=0;//将按键计数器清零
first=true;
error=false;
}
else if(e.getActionCommand().equals("等於"))
{
if((str1=="")||(str2=="")) //两个数没有输全就点击等号,执行if
{
countOper=0;//将按键计数器清零
first=true;
}
else
{
getResult();
countOper=0;
}
}
}
//运算结果的方法
public void getResult()
{
num1=Double.parseDouble(str1);
num2=Double.parseDouble(str2);
if(operator.equals("加"))
{
result=num1+num2;
}
else if(operator.equals("减"))
{
result=num1-num2;
}
else if(operator.equals("乘以"))
{
result=num1*num2;
}
else if(operator.equals("除以"))
{
if(num2==0.0) //除数为0的处理方法
{
error=true;
}
else
{
result=num1/num2;
}
}
if(error)
{
t.setText("error");
}
else
{
t.setText(String.valueOf(result));
str1=String.valueOf(result); //运算后把结果放入str1中,str2清空,为连加连减等操作做准备
str2="";
}
}
//主方法
public static void main(String[] args)
{
new Counter();//创建一个对象"计算器"
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class CalculatorPanel extends JPanel
implements ActionListener
{ public CalculatorPanel()
{ setLayout(new BorderLayout());
display = new JTextField("0");
display.setEditable(false);
add(display, "North");
JPanel p = new JPanel();
p.setLayout(new GridLayout(4, 4));
String buttons = "789/456*123-0.=+";
for (int i = 0; i < buttons.length(); i++)
addButton(p, buttons.substring(i, i + 1));
add(p, "Center");
}
private void addButton(Container c, String s)
{ JButton b = new JButton(s);
c.add(b);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{ String s = evt.getActionCommand();
if ('0' <= s.charAt(0) && s.charAt(0) <= '9'
|| s.equals("."))
{ if (start) display.setText(s);
else display.setText(display.getText() + s);
start = false;
}
else
{ if (start)
{ if (s.equals("-"))
{ display.setText(s); start = false; }
else op = s;
}
else
{ calculate(Double.parseDouble(display.getText()));
op = s;
start = true;
}
}
}
public void calculate(double n)
{ if (op.equals("+")) arg += n;
else if (op.equals("-")) arg -= n;
else if (op.equals("*")) arg *= n;
else if (op.equals("/")) arg /= n;
else if (op.equals("=")) arg = n;
display.setText("" + arg);
}
private JTextField display;
private double arg = 0;
private String op = "=";
private boolean start = true;
}
public class CalculatorApplet extends JApplet
{ public void init()
{ Container contentPane = getContentPane();
contentPane.add(new CalculatorPanel());
}
}
希望能帮上你``
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
//import java.awt.event.MouseEvent;
//import java.awt.event.MouseListener;
//import java.awt.event.MouseMotionListener;
public class CalculatorFrame extends JFrame implements ActionListener
{
public static void main(String args[]){
new CalculatorFrame();
}
// private String strEvent;
private double a1,a2,a3;
private int flag = 0 ;
private JTextField text;
private JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bce,bdop,bdiv,bplus,bsub,bch,bfh,bden,besc,bsqr;
private JDialog dialog;
private JDialog dialog1;
private JLabel label;
private JLabel label1;
CalculatorFrame(){
super("黄金计算器");
setSize(300,300);
setLocation(300,240);
this.setLayout(new GridLayout(6, 1 )); //各个组件呈网格状分布`` 参数指定行和列``
text = new JTextField(40);
text.setEditable(false);
this.add(text);
dialog = new JDialog(this,"");
dialog.setSize(300,120);
label = new JLabel("",JLabel.CENTER);
dialog.add(label);
dialog.setDefaultCloseOperation(HIDE_ON_CLOSE);
// dialog1 = new JDialog(this,"用户论坛");
// dialog1.setSize(300,120);
// label1 = new JLabel("",JLabel.CENTER);
// dialog1.add(label1);
// dialog1.setDefaultCloseOperation(HIDE_ON_CLOSE);
b1 = new JButton("1"); //设置按钮并添加监听器``
b1.addActionListener(this);
b2 = new JButton("2");
b2.addActionListener(this);
b3 = new JButton("3");
b3.addActionListener(this);
b4 = new JButton("4");
b4.addActionListener(this);
b5 = new JButton("5");
b5.addActionListener(this);
b6 = new JButton("6");
b6.addActionListener(this);
b7 = new JButton("7");
b7.addActionListener(this);
b8 = new JButton("8");
b8.addActionListener(this);
b9 = new JButton("9");
b9.addActionListener(this);
bch = new JButton("*");
bch.addActionListener(this);
bplus = new JButton("+");
bplus.addActionListener(this);
bsub = new JButton("-");
bsub.addActionListener(this);
bce = new JButton("清零");
bce.addActionListener(this);
b0 = new JButton("0");
b0.addActionListener(this);
bdop = new JButton(".");
bdop.addActionListener(this);
bdiv = new JButton("/");
bdiv.addActionListener(this);
bfh = new JButton("+/-");
bfh.addActionListener(this);
bden = new JButton("=");
bden.addActionListener(this);
besc = new JButton("关闭");
besc.addActionListener(this);
bsqr = new JButton("sqrt");
bsqr.addActionListener(this);
JPanel panel1 = new JPanel(new GridLayout(1, 4,3,3)); //后两个参数指定组件之间水平和垂直的间距``
this.add(panel1);
panel1.add(b1);
panel1.add(b2);
panel1.add(b3);
panel1.add(bplus);
JPanel panel2 = new JPanel(new GridLayout(1, 4,3,3));
this.add(panel2);
panel2.add(b4);
panel2.add(b5);
panel2.add(b6);
panel2.add(bsub);
JPanel panel3 = new JPanel(new GridLayout(1, 4,3,3));
this.add(panel3);
panel3.add(b7);
panel3.add(b8);
panel3.add(b9);
panel3.add(bch);
JPanel panel4 = new JPanel(new GridLayout(1, 4,3,3));
this.add(panel4);
panel4.add(bce);
panel4.add(b0);
panel4.add(bdop);
panel4.add(bdiv);
JPanel panel5 = new JPanel(new GridLayout(1, 4,3,3));
this.add(panel5);
panel5.add(besc);
panel5.add(bsqr);
panel5.add(bfh);
panel5.add(bden);
this.addmyMenu();
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
try{
if (e.getActionCommand() == "退出"){
System.exit(0);
}
if(e.getActionCommand() == "关于计算器"){
// dialog = new JDialog(this,"关于计算器");
label.setText("本产品使用权 : 计算机班 黄金堂 12007247570");
dialog.setLocation(this.getX()+300,this.getY()+100);
dialog.setVisible(true);
}
if(e.getActionCommand() == "用户论坛"){
// dialog = new JDialog(this,"用户论坛");
label.setText("请先注册");
dialog.setLocation(this.getX()+300,this.getY()+100);
dialog.setVisible(true);
}
if(e.getActionCommand() == "帮助中心"){
label.setText("每次运算前请先清零``");
dialog.setLocation(this.getX()+300,this.getY()+100);
dialog.setVisible(true);
}
if(e.getActionCommand() == "复制"){
label.setText("已复制到粘贴板``");
dialog.setLocation(this.getX()+300,this.getY()+100);
dialog.setVisible(true);
}
if(e.getActionCommand() == "粘贴"){
label.setText("请先注册``");
dialog.setLocation(this.getX()+300,this.getY()+100);
dialog.setVisible(true);
}
if(e.getActionCommand() == "打开"){
label.setText("维护中,请与管理员联系``");
dialog.setLocation(this.getX()+300,this.getY()+100);
dialog.setVisible(true);
}
if(e.getActionCommand() == "保存"){
label.setText("维护中,请与管理员联系``");
dialog.setLocation(this.getX()+300,this.getY()+100);
dialog.setVisible(true);
}
if(e.getSource() == b0){
text.setText(text.getText()+e.getActionCommand());
System.out.print(e.getActionCommand());
}
if(e.getSource() == b1){
text.setText(text.getText()+e.getActionCommand());
System.out.print(e.getActionCommand());
}
if(e.getSource() == b2){
text.setText(text.getText()+e.getActionCommand());
System.out.print(e.getActionCommand());
}
if(e.getSource() == b3){
text.setText(text.getText()+e.getActionCommand());
System.out.print(e.getActionCommand());
}
if(e.getSource() == b4){
text.setText(text.getText()+e.getActionCommand());
System.out.print(e.getActionCommand());
}
if(e.getSource() == b5){
text.setText(text.getText()+e.getActionCommand());
System.out.print(e.getActionCommand());
}
if(e.getSource() == b6){
text.setText(text.getText()+e.getActionCommand());
System.out.print(e.getActionCommand());
}
if(e.getSource() == b7){
text.setText(text.getText()+e.getActionCommand());
System.out.print(e.getActionCommand());
}
if(e.getSource() == b8){
text.setText(text.getText()+e.getActionCommand());
System.out.print(e.getActionCommand());
}
if(e.getSource() == b9){
text.setText(text.getText()+e.getActionCommand());
System.out.print(e.getActionCommand());
}
if(e.getSource() == bdop){
text.setText(text.getText()+".");
System.out.print(e.getActionCommand());
}
if(e.getSource() == bfh){
text.setText("-"+text.getText());
System.out.println("");
System.out.print(text.getText());
}
if(e.getSource() == bce){
text.setText("");
System.out.println("");
}
if(e.getSource()==bplus){
a1=Double.parseDouble(text.getText());
text.setText("");
System.out.print("+");
flag = 1;
}
if(e.getSource()==bsub){
a1=Double.parseDouble(text.getText());
text.setText("");
System.out.print("-");
flag = 2;
}
if(e.getSource()==bch){
a1=Double.parseDouble(text.getText());
text.setText("");
System.out.print("*");
flag = 3;
}
if(e.getSource()==bdiv){
a1=Double.parseDouble(text.getText());
text.setText("");
System.out.print("/");
flag = 4;
}
if(e.getSource()==besc){
System.exit(0);
}
if(e.getSource()==bsqr){
String s = text.getText();
if(s.charAt(0)=='-') {
JOptionPane.showMessageDialog(this,"负数没有开方,请按'清零'键重新输入!!","警告对话框",JOptionPane.WARNING_MESSAGE);
}
else
System.out.println("的根号="+Double.toString(java.lang.Math.sqrt(Double.parseDouble(text.getText()))));
text.setText(Double.toString(java.lang.Math.sqrt(Double.parseDouble(text.getText()))));
}
if(e.getSource()==bden){
a2=Double.parseDouble(text.getText());
System.out.print("=");
if(flag == 1){
a3 = a1+a2;
System.out.println(a3);
}
if(flag == 2){
a3 = a1-a2;
System.out.println(a3);
}
if(flag == 3){
a3 = a1*a2;
System.out.println(a3);
}
if(flag == 4){
if (a2 == 0){
JOptionPane.showMessageDialog(this,"除数不能为0,请按'清零'键重新输入!!","警告对话框",JOptionPane.WARNING_MESSAGE);
}
else {
// try{
a3 = a1/a2;
System.out.println(a3);
}
// catch(ArithmeticException ex){
// }
// }
}
text.setText(Double.toString(a3));
}
}
catch(Exception ex){
JOptionPane.showMessageDialog(this,"对不起,您输错了!!","警告对话框",JOptionPane.WARNING_MESSAGE);
System.out.println("请按清零键重新输入");
}
}
// public void mouseEntered(MouseEvent e){ //当鼠标指向源对象范围
// }
private void addmyMenu(){
JMenuBar menubar = new JMenuBar();
this.setJMenuBar(menubar);
JMenu file = new JMenu("文件");
menubar.add(file);
JMenuItem dakai = new JMenuItem("打开");
file.add(dakai);
dakai.addActionListener(this);
JMenuItem baocun = new JMenuItem("保存");
file.add(baocun);
baocun.addActionListener(this);
file.addSeparator();
JMenuItem exit = new JMenuItem("退出");
file.add(exit);
exit.addActionListener(this);
// JMenuBar menubar = new JMenuBar();
// this.setJMenuBar(menubar);
JMenu bianji = new JMenu("编辑");
menubar.add(bianji);
JMenuItem fuzhi = new JMenuItem("复制");
bianji.add(fuzhi);
fuzhi.addActionListener(this);
JMenuItem zhantie = new JMenuItem("粘贴");
bianji.add(zhantie);
zhantie.addActionListener(this);
// bianji.add(new JMenuItem("复制"));
// bianji.add(new JMenuItem("粘贴"));
// bianji.addSeparator();
// JMenuItem shuxing = new JMenuItem("属性");
// bianji.add(shuxing);
// shuxing.addActionListener(this);
JMenu help = new JMenu("服务");
menubar.add(help);
JMenuItem zhongxin = new JMenuItem("帮助中心");
help.add(zhongxin);
zhongxin.addActionListener(this);
JMenuItem luntan = new JMenuItem("用户论坛");
help.add(luntan);
luntan.addActionListener(this);
help.addSeparator();
JMenuItem guanyu = new JMenuItem("关于计算器");
help.add(guanyu);
guanyu.addActionListener(this);
}
}