求JAVA用Eclipse写个的计算器实现键盘功能代码。

实现键盘功能,跪求。... 实现键盘功能,跪求。 展开
 我来答
给我一根雪茄
推荐于2016-10-10
知道答主
回答量:16
采纳率:0%
帮助的人:6.8万
展开全部
以前写的,自己改下吧
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CalculatorJFrame extends JFrame implements ActionListener
{
private boolean clickBeforeOperator; //点击"+,-,*,/"等数学运算符和清除回车时 clickBeforeOperator = true
private int firstInputChar; //判断是否是第一次输入字符,firstInputChar = 0时是第一次输入
private double answer; //文本框中结果
private int Operator;
private DisplayPanel displaypanel = new DisplayPanel();
private ButtonPanel buttonpanel = new ButtonPanel();

private class DisplayPanel extends JPanel
{
private JTextField text;
public DisplayPanel()
{
text = new JTextField("",30);
text.setBackground(Color.white); //设置文本颜色
text.setHorizontalAlignment(JTextField.RIGHT); //设置右对齐
text.setEditable(false); //设置不可编辑
this.setLayout(new BorderLayout()); //面板设置为边布局
this.add(text,BorderLayout.CENTER); //text文本行放在面板DisplayPanel
}
}

public class ButtonPanel extends JPanel
{
private String button_str[] = { "7","8","9","sqrt","sin","Clear","退格","*", //字符串数组
"4","5","6",".","cos","倒数","/","-", //做JButton的标签
"1","2","3","0","tan","n!","=","+" } ;
private JButton button[] = new JButton[button_str.length]; //创建JButton按钮
public ButtonPanel()
{
this.setLayout(new GridLayout(3,8,5,5)); //面板布局管理器设置为网格布局管理器
this.setBackground(Color.pink);

for(int i = 0; i < button_str.length; i++)
{
button[i] = new JButton(button_str[i]);
this.add(button[i]); //在面板上添加button组件
}

//设置button颜色
for(int a1 = 0; a1 <= 2; a1++){
button[a1].setBackground(Color.yellow); //设置数字7~9的颜色
}
button[3].setBackground(Color.green);
button[4].setBackground(Color.green);
button[5].setBackground(Color.ORANGE);
button[6].setBackground(Color.ORANGE);
// button[7].setBackground(Color.magenta);
for(int b1 = 8; b1 <= 11; b1++){
button[b1].setBackground(Color.yellow); //设置数字4~5的颜色
}
button[12].setBackground(Color.green);
button[13].setBackground(Color.green);
// button[14].setBackground(Color.magenta);
// button[15].setBackground(Color.magenta);
for(int c1 = 16; c1 <= 19; c1++){
button[c1].setBackground(Color.yellow); //设置数字1~3的颜色
}
button[20].setBackground(Color.green);
button[21].setBackground(Color.green);
// button[22].setBackground(Color.magenta);
// button[23].setBackground(Color.magenta);
}
}

public CalculatorJFrame(){ //构造方法
super("颜氏计算器3代");
answer = 0;
clickBeforeOperator = false; //指示器
firstInputChar = 0;
Operator = -1;

Dimension dim = getToolkit().getScreenSize();
this.setBounds(dim.width/8, dim.height/8, dim.width/2, dim.height/2); //窗口居中
this.setBackground(Color.LIGHT_GRAY);
//this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE); //(水平间距hgap = 0,垂直间距vgap =10)
this.getContentPane().setLayout(new BorderLayout(0,10)); // 框架默认变布局管理器,可省略

this.getContentPane().add(displaypanel,"North"); //框架"北"放置文本行text
this.getContentPane().add(buttonpanel,"Center"); //框架"中"放置面板组件
for(int i = 0; i < buttonpanel.button_str.length; i++)
{
buttonpanel.button[i].addActionListener(this);
}
// this.pack();
this.setVisible(true);
}

public void actionPerformed(ActionEvent e){
String button_label = e.getActionCommand();

//******************************输入double型数据********************************
if((button_label.charAt(0) >= '0' && button_label.charAt(0) <= '9') || button_label == "."){
if(clickBeforeOperator == true || firstInputChar == 0){ //如果点击了运算符,清除回车等按键,再点击数字键
displaypanel.text.setText("" + button_label);
clickBeforeOperator = false;
firstInputChar = 1;
}
else{
if(!(button_label == ".")) //如果输入数字
{
displaypanel.text.setText(displaypanel.text.getText()+button_label);
}
if(button_label == "."){ //如果输入点
boolean isContainerPoint = false; //判断逻辑量
if(displaypanel.text.getText().equals("")) //如果文本行中字符串为空
{
isContainerPoint = true;
displaypanel.text.setText("0.");
}
for(int i = 0; i < displaypanel.text.getText().length(); i++) //判断文本行中字符串是否已含小数点
{
if(displaypanel.text.getText().charAt(i) == '.'){
isContainerPoint =true;
}
}
if(isContainerPoint == false)
{
displaypanel.text.setText(displaypanel.text.getText() + ".");
}
}
}
}
//******************************开根号运算*************************************
try{
if(button_label == "sqrt" ){

if(displaypanel.text.getText().charAt(0) == '-')
{
// displaypanel.text.setText("负数不能开根号");
JOptionPane.showMessageDialog(this, "负数不能开根号");
}
else{
clickBeforeOperator = true;
firstInputChar = 0;
double sqrtX = Math.sqrt(Double.parseDouble(displaypanel.text.getText()));
displaypanel.text.setText(""+sqrtX);
}

}
}
catch(StringIndexOutOfBoundsException sioobe)
{
JOptionPane.showMessageDialog(this, "已空,无法进行开根号操作");
}
finally
{
}
//*******************************三角函数运算**************************************
try{
if(button_label == "sin"){
clickBeforeOperator = true;
firstInputChar = 0;
double sinX = Math.sin(Double.parseDouble(displaypanel.text.getText()));
displaypanel.text.setText(""+sinX);
}
if(button_label == "cos"){
clickBeforeOperator = true;
firstInputChar = 0;
double cosX = Math.cos(Double.parseDouble(displaypanel.text.getText()));
displaypanel.text.setText(""+cosX);
}
if(button_label == "tan"){
clickBeforeOperator = true;
firstInputChar = 0;
double tanX = Math.tan(Double.parseDouble(displaypanel.text.getText()));
displaypanel.text.setText(""+tanX);
}
//***********************************求倒数******************************************
if(button_label == "倒数"){
if(Double.parseDouble(displaypanel.text.getText()) == 0)
{
// displaypanel.text.setText("零没有倒数");
JOptionPane.showMessageDialog(this, "零没有倒数");
}
else
{
clickBeforeOperator = true;
firstInputChar = 0;
double daoshuX = 1.0/(Double.parseDouble(displaypanel.text.getText()));
displaypanel.text.setText(""+daoshuX);
}
}
//************************************求阶乘**************************************
if(button_label == "n!"){
int i = Integer.parseInt(displaypanel.text.getText());
int sum = 1;
for(int k = 1; k<= i; k++)
sum *= k;
displaypanel.text.setText(""+sum);
clickBeforeOperator = true;
firstInputChar = 0;
}
}
catch(NumberFormatException nfe)
{
JOptionPane.showMessageDialog(this, "已空,无法进行相应操作");
}
finally
{
}
//**************************************清除键*************************************
if(button_label == "Clear"){
displaypanel.text.setText("");
clickBeforeOperator = true;
firstInputChar = 0;
213 }
//**************************************回车键*************************************
try //异常处理
{
if(button_label == "退格"){
String str = displaypanel.text.getText();
displaypanel.text.setText(str.substring(0,str.length()-1)); //最初此语句无效????why,当把回车键标签名设置为BackSpace时,此语句失效
clickBeforeOperator = true;
firstInputChar = 0; //BackSpace字符串长度过长,导致界面上无法显示全部字符串,故无法实现此功能
// if(displaypanel.text.getText() == "")
// throw new Exception("NullException");
}
}
catch(StringIndexOutOfBoundsException sioobe)
{
JOptionPane.showMessageDialog(this, "已空,无法继续删除");
}
// catch(Exception ex)
// {
// if(ex.getMessage() == "NullException")
// JOptionPane.showMessageDialog(this, "已空,无法继续删除");
// }
finally
{

}
/* for(int i = 0; i < str.length() - 1; i++){
char a = str.charAt(i);
text.setText(text.getText() + a);
} */
//***************************************加减乘除运算*****************************************

if(button_label == "+" || button_label == "-" || button_label == "*" || button_label =="/")
{
if(button_label == "+"){
clickBeforeOperator = true;
firstInputChar = 0;
answer = Double.parseDouble(displaypanel.text.getText());
Operator = 0;
}
if(button_label == "-"){
clickBeforeOperator = true;
firstInputChar = 0;
answer = Double.parseDouble(displaypanel.text.getText());
Operator = 1;
}
if(button_label == "*"){
clickBeforeOperator = true;
firstInputChar = 0;
answer = Double.parseDouble(displaypanel.text.getText());
Operator = 2;
}
if(button_label == "/"){
clickBeforeOperator = true;
firstInputChar = 0;
answer = Double.parseDouble(displaypanel.text.getText());
Operator = 3;
}
}
if(button_label == "="){
switch(Operator)
{
case 0:
answer += Double.parseDouble(displaypanel.text.getText());
displaypanel.text.setText(""+answer);
firstInputChar = 0;
break;
case 1:
answer -= Double.parseDouble(displaypanel.text.getText());
displaypanel.text.setText(""+answer);
firstInputChar = 0;
break;
case 2:
answer *= Double.parseDouble(displaypanel.text.getText());
displaypanel.text.setText(""+answer);
288 firstInputChar = 0;
break;
case 3:
if(Double.parseDouble(displaypanel.text.getText()) == 0){
// displaypanel.text.setText("除数不能为零");
JOptionPane.showMessageDialog(this, "除数不能为零");
}
else
{
answer /= Double.parseDouble(displaypanel.text.getText());
displaypanel.text.setText(""+answer);
firstInputChar = 0;
}
break;
}
}
}

public static void main(String args[]){
new CalculatorJFrame();
}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
517052452
2013-01-22 · 超过21用户采纳过TA的回答
知道答主
回答量:82
采纳率:0%
帮助的人:46.9万
展开全部
分好少啊。不想写了。
追问
不好意思阿  没分了 跪求。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
想起的幻觉
2013-01-23
知道答主
回答量:10
采纳率:0%
帮助的人:3.7万
展开全部
监听键盘呀
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 2条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式