求 java eclipse 简单计算器代码

直接复制上去就OK的.谢谢.摸班就是电脑附带的那个版本... 直接复制上去就OK的.谢谢.摸班就是电脑附带的那个版本 展开
 我来答
tower888
推荐于2016-06-04 · TA获得超过611个赞
知道小有建树答主
回答量:397
采纳率:0%
帮助的人:278万
展开全部
package javatest;

import java.awt.Frame;

import java.awt.TextField;
import java.awt.Rectangle;
import java.awt.Button;
import java.awt.ComponentOrientation;
import java.awt.Dimension;

import javax.swing.JComponent;
import javax.swing.JToolTip;
import javax.swing.ToolTipManager;

public class 计算器 extends Frame {

private static final long serialVersionUID = 1L;
private TextField textField = null;
private Button button = null;
private Button button1 = null;
private Button button2 = null;
private Button button3 = null;
private Button button4 = null;
private Button button5 = null;
private Button button6 = null;
private Button button7 = null;
private Button button8 = null;
private Button button9 = null;
private Button button10 = null;
private Button button11 = null;
private Button button12 = null;
private Button button13 = null;
private Button button14 = null;
private Button button15 = null;
private TextField textField1 = null;
private TextField textField2 = null;
private TextField textField3 = null;
private Button button16 = null;
/**
* This method initializes textField
*
* @return java.awt.TextField
*/
private TextField getTextField() {
if (textField == null) {
textField = new TextField();
textField.setBounds(new Rectangle(45, 30, 226, 16));
textField.setComponentOrientation(ComponentOrientation.UNKNOWN);
textField.setName("textfield");

}
return textField;
}

/**
* This method initializes button
*
* @return java.awt.Button
*/
private Button getButton() {
if (button == null) {
button = new Button();
button.setBounds(new Rectangle(30, 72, 32, 33));
button.setLabel("0");
button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()

if(textField2.getText().equals(""))//必须加equals,不能写成if(textField2.getText=="")
{
textField1.setText(textField1.getText()+"0");
textField.setText(textField.getText()+"0");
}
else
{
textField3.setText(textField3.getText()+"0");
textField.setText(textField.getText()+"0");
}
}
});
}
return button;
}

/**
* This method initializes button1
*
* @return java.awt.Button
*/
private Button getButton1() {
if (button1 == null) {
button1 = new Button();
button1.setBounds(new Rectangle(91, 72, 32, 33));
button1.setLabel("1");
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()

if(textField2.getText().equals(""))
{
textField1.setText(textField1.getText()+"1");
textField.setText(textField.getText()+"1");
}
else
{
textField3.setText(textField3.getText()+"1");
textField.setText(textField.getText()+"1");
}

}
});
}
return button1;
}

/**
* This method initializes button2
*
* @return java.awt.Button
*/
private Button getButton2() {
if (button2 == null) {
button2 = new Button();
button2.setBounds(new Rectangle(146, 72, 32, 33));
button2.setLabel("2");
button2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
if(textField2.getText().equals(""))
{
textField1.setText(textField1.getText()+"2");
textField.setText(textField.getText()+"2");
}
else
{
textField3.setText(textField3.getText()+"2");
textField.setText(textField.getText()+"2");
}

}
});
}
return button2;
}

/**
* This method initializes button3
*
* @return java.awt.Button
*/
private Button getButton3() {
if (button3 == null) {
button3 = new Button();
button3.setBounds(new Rectangle(30, 114, 32, 33));
button3.setLabel("3");
button3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()

if(textField2.getText().equals(""))
{
textField1.setText(textField1.getText()+"3");
textField.setText(textField.getText()+"3");
}
else
{
textField3.setText(textField3.getText()+"3");
textField.setText(textField.getText()+"3");
}
}
});
}
return button3;
}

/**
* This method initializes button4
*
* @return java.awt.Button
*/
private Button getButton4() {
if (button4 == null) {
button4 = new Button();
button4.setBounds(new Rectangle(91, 114, 32, 33));
button4.setLabel("4");
button4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()

if(textField2.getText().equals(""))
{
textField1.setText(textField1.getText()+"4");
textField.setText(textField.getText()+"4");
}

else
{
textField3.setText(textField3.getText()+"4");
textField.setText(textField.getText()+"4");
}

}
});
}
return button4;
}

/**
* This method initializes button5
*
* @return java.awt.Button
*/
private Button getButton5() {
if (button5 == null) {
button5 = new Button();
button5.setBounds(new Rectangle(146, 114, 32, 33));
button5.setLabel("5");
button5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()

if(textField2.getText().equals(""))
{
textField1.setText(textField1.getText()+"5");
textField.setText(textField.getText()+"5");
}
else
{
textField3.setText(textField3.getText()+"5");
textField.setText(textField.getText()+"5");
}
}
});
}
return button5;
}

/**
* This method initializes button6
*
* @return java.awt.Button
*/
private Button getButton6() {
if (button6 == null) {
button6 = new Button();
button6.setBounds(new Rectangle(30, 155, 32, 33));
button6.setLabel("6");
button6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()

if(textField2.getText().equals(""))
{
textField1.setText(textField1.getText()+"6");
textField.setText(textField.getText()+"6");
}
else
{
textField3.setText(textField3.getText()+"6");
textField.setText(textField.getText()+"6");
}
}
});
}
return button6;
}

/**
* This method initializes button7
*
* @return java.awt.Button
*/
private Button getButton7() {
if (button7 == null) {
button7 = new Button();
button7.setBounds(new Rectangle(91, 155, 32, 33));
button7.setLabel("7");
button7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()

if(textField2.getText().equals(""))
{
textField1.setText(textField1.getText()+"7");
textField.setText(textField.getText()+"7");
}
else
{
textField3.setText(textField3.getText()+"7");
textField.setText(textField.getText()+"7");
}
}
});
}
return button7;
}

/**
* This method initializes button8
*
* @return java.awt.Button
*/
private Button getButton8() {
if (button8 == null) {
button8 = new Button();
button8.setBounds(new Rectangle(146, 155, 32, 33));
button8.setLabel("8");
button8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()

if(textField2.getText().equals(""))
{
textField1.setText(textField1.getText()+"8");
textField.setText(textField.getText()+"8");
}
else
{
textField3.setText(textField3.getText()+"8");
textField.setText(textField.getText()+"8");
}
}
});
}
return button8;
}

/**
* This method initializes button9
*
* @return java.awt.Button
*/
private Button getButton9() {
if (button9 == null) {
button9 = new Button();
button9.setBounds(new Rectangle(30, 201, 32, 33));
button9.setLabel("9");
button9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()

if(textField2.getText().equals(""))
{
textField1.setText(textField1.getText()+"9");
textField.setText(textField.getText()+"9");
}
else
{
textField3.setText(textField3.getText()+"9");
textField.setText(textField.getText()+"9");
}
}
});
}
return button9;
}

/**
* This method initializes button10
*
* @return java.awt.Button
*/
private Button getButton10() {
if (button10 == null) {
button10 = new Button();
button10.setBounds(new Rectangle(91, 201, 32, 33));
button10.setLabel(".");
button10.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()

if(textField2.getText().equals(""))
textField1.setText(textField1.getText()+".");
else
textField3.setText(textField3.getText()+".");
}
});
}
return button10;
}

/**
* This method initializes button11
*
* @return java.awt.Button
*/
private Button getButton11() {
if (button11 == null) {
button11 = new Button();
button11.setBounds(new Rectangle(270, 73, 32, 30));
button11.setLabel("+");
button11.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()

textField2.setText("+");
textField.setText("");
}
});
}
return button11;
}

/**
* This method initializes button12
*
* @return java.awt.Button
*/
private Button getButton12() {
if (button12 == null) {
button12 = new Button();
button12.setBounds(new Rectangle(270, 115, 32, 30));
button12.setLabel("-");
button12.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()

textField2.setText("-");
textField.setText("");
}
});
}
return button12;
}

/**
* This method initializes button13
*
* @return java.awt.Button
*/
private Button getButton13() {
if (button13 == null) {
button13 = new Button();
button13.setBounds(new Rectangle(270, 157, 32, 30));
button13.setLabel("*");
button13.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()

textField2.setText("*");
textField.setText("");
}
});
}
return button13;
}

/**
* This method initializes button14
*
* @return java.awt.Button
*/
private Button getButton14() {
if (button14 == null) {
button14 = new Button();
button14.setBounds(new Rectangle(270, 199, 32, 30));
button14.setLabel("/");
button14.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()

textField2.setText("/");
textField.setText("");
}
});
}
return button14;
}

/**
* This method initializes button15
*
* @return java.awt.Button
*/
private Button getButton15() {
if (button15 == null) {
button15 = new Button();
button15.setBounds(new Rectangle(146, 201, 32, 33));
button15.setLabel("=");
button15.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()

//double result;
double result=0;
double a=Double.parseDouble(textField1.getText());
double b=Double.parseDouble(textField3.getText());

if(textField2.getText().equals("+"))
result=a+b;
else
if(textField2.getText().equals("-"))
result=a-b;
else
if(textField2.getText().equals("*"))
result=a*b;
else
if(textField2.getText().equals("/")&&b!=0)
result=a/b;
textField2.setText("");
textField3.setText("");
String str=String.valueOf(result);
textField.setText(str);
textField1.setText(str);

}
});
}
return button15;
}

/**
* This method initializes textField1
*
* @return java.awt.TextField
*/
private TextField getTextField1() {
if (textField1 == null) {
textField1 = new TextField();
textField1.setBounds(new Rectangle(47, 242, 81, 24));
textField1.setText("");
textField1.setName("textfield1");
textField1.setVisible(true);
}
return textField1;
}

/**
* This method initializes textField2
*
* @return java.awt.TextField
*/
private TextField getTextField2() {
if (textField2 == null) {
textField2 = new TextField();
textField2.setBounds(new Rectangle(154, 242, 27, 24));
textField2.setName("textfield2");
}
return textField2;
}

/**
* This method initializes textField3
*
* @return java.awt.TextField
*/
private TextField getTextField3() {
if (textField3 == null) {
textField3 = new TextField();
textField3.setBounds(new Rectangle(211, 241, 81, 24));
textField3.setName("textfield3");
}
return textField3;
}

/**
* This method initializes button16
*
* @return java.awt.Button
*/
private Button getButton16() {
if (button16 == null) {
button16 = new Button();
button16.setBounds(new Rectangle(286, 29, 41, 29));
button16.setLabel("清空");
button16.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()

textField.setText("");
textField1.setText("");
textField2.setText("");
textField3.setText("");
}
});
button16.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent e) {
System.out.println("mouseEntered()"); // TODO Auto-generated Event stub mouseEntered()

JToolTip jtt=new JToolTip();//工具提示
jtt.setTipText("这是一个清空按钮");

}

});

}
return button16;
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new 计算器();
}

/**
* This is the default constructor
*/
public 计算器() {
super();
initialize();

show();
}

/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setLayout(null);
this.setSize(339, 278);
this.setTitle("Frame");

this.add(getTextField(), null);
this.add(getButton(), null);
this.add(getButton1(), null);
this.add(getButton2(), null);
this.add(getButton3(), null);
this.add(getButton4(), null);
this.add(getButton5(), null);
this.add(getButton6(), null);
this.add(getButton7(), null);
this.add(getButton8(), null);
this.add(getButton9(), null);
this.add(getButton10(), null);
this.add(getButton11(), null);
this.add(getButton12(), null);
this.add(getButton13(), null);
this.add(getButton14(), null);
this.add(getButton15(), null);
this.add(getTextField1(), null);
this.add(getTextField2(), null);
this.add(getTextField3(), null);
this.add(getButton16(), null);
}

} // @jve:decl-index=0:visual-constraint="96,21"
dawanghai
2009-06-04 · 超过11用户采纳过TA的回答
知道答主
回答量:128
采纳率:0%
帮助的人:64.5万
展开全部
你怎么自己不编呢?
想学好java别发懒!!!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
zwb1101
2009-06-04 · 超过10用户采纳过TA的回答
知道答主
回答量:52
采纳率:0%
帮助的人:38.7万
展开全部
作业吧……唉……
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式