求java计算器的程序,具体界面就像下面的图那样的,谢谢了!我这有个半成品,就是少了几个键,高手帮帮忙

package com.tjx1222.tms.helloworld;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener{
private static final long serialVersionUID = 8199443193151152362L;
private JButton bto_s=new JButton("sqrt"),bto_zf=new JButton("+/-"),bto_ce=new JButton("CE"),bto_c=new JButton("C"),bto_7=new JButton("7"),
bto_8=new JButton("8"),bto_9=new JButton("9"),bto_chu=new JButton("/"),bto_4=new JButton("4"),bto_5=new JButton("5"),
bto_6=new JButton("6"),bto_cheng=new JButton("*"),bto_1=new JButton("1"),bto_2=new JButton("2"),bto_3=new JButton("3"),
bto_jian=new JButton("-"),bto_0=new JButton("0"),bto_dian=new JButton("."),bto_deng=new JButton("="),bto_jia=new JButton("+");
JButton button[]={bto_s,bto_zf,bto_ce,bto_c,bto_7,bto_8,bto_9,bto_chu,bto_4,bto_5,bto_6,bto_cheng,bto_1,bto_2,bto_3,bto_jian,
bto_0,bto_dian,bto_deng,bto_jia};
private JTextField text_double;// = new JTextField("0");
private String operator = "="; //当前运算的运算符
private boolean firstDigit = true; // 标志用户按的是否是整个表达式的第一个数字,或者是运算符后的第一个数字
private double resultNum = 0.0; // 计算的中间结果
private boolean operateValidFlag = true; //判断操作是否合法
private JLabel stuno = new JLabel("学号:29130101016"); //学号
private JLabel stuname = new JLabel("姓名:陈昕"); //姓名
public Calculator()
{
super("Calculator");
this.setBounds(300, 300, 300, 320);
this.setResizable(false);
this.setBackground(Color.orange);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(new BorderLayout());//设置布局
JPanel topPanel = new JPanel(new BorderLayout());
topPanel.add(stuno,BorderLayout.WEST);
topPanel.add(stuname,BorderLayout.EAST);
text_double=new JTextField("0",20);//设置文本区
text_double.setHorizontalAlignment(JTextField.RIGHT);//设置水平对齐方式未右对齐
topPanel.add(text_double,BorderLayout.SOUTH);
this.getContentPane().add(topPanel,BorderLayout.NORTH);//将文本区添加到Content北部

JPanel panel=new JPanel(new GridLayout(5,4));//在内容窗口添加一个网格布局
this.getContentPane().add(panel);//添加panel面板
for(int i=0;i<button.length;i++)//在面板上添加按钮
panel.add(button[i]);

for(int i=0;i<button.length;i++)
button[i].addActionListener(this);//为按钮注册
text_double.setEditable(false);//文本框不可编辑
text_double.addActionListener(this);//

this.setVisible(true);
}
public void actionPerformed(ActionEvent e)//
{
String c= e.getActionCommand();//返回与此动作相关的命令字符串。
System.out.println("##########command is "+c);
if(c.equals("C")){
handleC(); //用户按了“C”键
}
else if (c.equals("CE")) // 用户按了"CE"键
{
text_double.setText("0");
}
else if ("0123456789.".indexOf(c) >= 0) // 用户按了数字键或者小数点键
{
handleNumber(c); // handlezero(zero);
} else //用户按了运算符键
{
handleOperator(c);
}
}
private void handleC() // 初始化计算器的各种值
{
text_double.setText("0");
firstDigit = true;
operator = "=";
}
private void handleNumber(String button) {
if (firstDigit)//输入的第一个数字
{
text_double.setText(button);
} else if ((button.equals(".")) && (text_double.getText().indexOf(".") < 0))//输入的是小数点,并且之前没有小数点,则将小数点附在结果文本框的后面
//如果字符串参数作为一个子字符串在此对象中出现,则返回第一个这种子字符串的第一个字符的索引;如果它不作为一个子字符串出现,则返回 -1
{
text_double.setText(text_double.getText() + ".");
} else if (!button.equals("."))// 如果输入的不是小数点,则将数字附在结果文本框的后面
{
text_double.setText(text_double.getText() + button);
}
// 以后输入的肯定不是第一个数字了
firstDigit = false;
}
private void handleOperator(String button) {
发不完了。。。晕!怎么办。。

第1个回答  推荐于2016-09-01
修改的,还没有美化下
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DecimalFormat;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class Calculator extends JFrame {

private JLabel stuno = new JLabel("学号:29130101016");
private JLabel stuname = new JLabel("姓名:陈昕");

private JPanel inputPanel = new JPanel();
private JPanel operationPanel = new JPanel();

private JTextArea input = new JTextArea(2, 18);

private JButton backspace = new JButton("<-");
private JButton CE = new JButton("CE ");
private JButton C = new JButton("C ");
private JButton sign = new JButton("+/-");
private JButton sqrt = new JButton("SQRT");
private JButton percentage = new JButton("%");
private JButton fraction = new JButton("1/x");

private JButton add = new JButton("+");
private JButton sub = new JButton("-");
private JButton mul = new JButton("*");
private JButton div = new JButton("/");

private JButton decimal = new JButton(".");
private JButton equal = new JButton("=");

private JButton zero = new JButton("0");
private JButton one = new JButton("1");
private JButton two = new JButton("2");
private JButton three = new JButton("3");
private JButton four = new JButton("4");
private JButton five = new JButton("5");
private JButton six = new JButton("6");
private JButton seven = new JButton("7");
private JButton eight = new JButton("8");
private JButton nine = new JButton("9");

private JTextArea blank = new JTextArea(2, 18);

private String num1 = "";
private String operator = "";

public static void main(String[] args) {
new Calculator();
}

public Calculator(){
super("Calculator");
super.setBackground(Color.black);
super.setBounds(300, 300, 300, 320);

JPanel information = new JPanel();
information.setLayout(new GridLayout(1, 2));
information.add(stuno);
information.add(stuname);

super.add(information, BorderLayout.NORTH);

inputPanel.add(input);
input.setBackground(new Color(197, 215, 216));
inputPanel.add(blank);
inputPanel.setBackground(Color.gray);
inputPanel.setLayout(new GridLayout(2, 1));
blank.setEditable(false);

super.add(inputPanel, BorderLayout.CENTER);

operationPanel.add(backspace);
operationPanel.add(CE);
operationPanel.add(C);
operationPanel.add(sign);
operationPanel.add(sqrt);

operationPanel.add(seven);
operationPanel.add(eight);
operationPanel.add(nine);
operationPanel.add(div);
operationPanel.add(percentage);

operationPanel.add(four);
operationPanel.add(five);
operationPanel.add(six);
operationPanel.add(mul);
operationPanel.add(fraction);

operationPanel.add(one);
operationPanel.add(two);
operationPanel.add(three);
operationPanel.add(sub);
operationPanel.add(decimal);

JButton dummyBtn = new JButton(" ");
dummyBtn.setVisible(false);
operationPanel.add(dummyBtn);
operationPanel.add(zero);
dummyBtn = new JButton(" ");
dummyBtn.setVisible(false);
operationPanel.add(dummyBtn);
operationPanel.add(add);
operationPanel.add(equal);

operationPanel.setLayout(new GridLayout(5, 5));

operationPanel.setBackground(Color.gray);
super.add(operationPanel, BorderLayout.SOUTH);

super.setVisible(true);
super.pack();
super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JButton[] operatAry = {backspace, CE, C, decimal, equal, sign, sqrt, percentage, fraction};
OperationMouseListener oepratorMouseListern = new OperationMouseListener();
for(JButton btnItem: operatAry){
btnItem.addMouseListener(oepratorMouseListern);
}

add.addMouseListener(new CalcMouseListener());
sub.addMouseListener(new CalcMouseListener());
mul.addMouseListener(new CalcMouseListener());
div.addMouseListener(new CalcMouseListener());

JButton[] numberBtnAry = {zero, one, two, three, four, five, six, seven, eight, nine};
NumberMouseListener numberMouseListener = new NumberMouseListener();
for(JButton btnItem: numberBtnAry){
btnItem.addMouseListener(numberMouseListener);
}

}

private class NumberMouseListener implements MouseListener{

public void mouseClicked(MouseEvent e) {
if(input.getText().trim().equals("0")){
input.setText(((JButton)e.getSource()).getText().trim());
}else{
input.setText(input.getText().concat(((JButton)e.getSource()).getText().trim()));
}
}

public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}

private class CalcMouseListener implements MouseListener{

public void mouseClicked(MouseEvent e) {
num1 = input.getText().trim();
input.setText("");
operator = ((JButton)e.getSource()).getText().trim();
}

public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}

private class OperationMouseListener implements MouseListener{

public void mouseClicked(MouseEvent e) {
if(e.getSource() == backspace){
String inputText = input.getText();
if(inputText.length() > 0){
input.setText(inputText.substring(0, inputText.length() - 1));
}
}else if(e.getSource() == C){
input.setText("0");
num1 = "";
}else if(e.getSource() == CE){
input.setText("0");
}else if(e.getSource() == decimal){
String text = input.getText().trim();

if(text.indexOf(".") == -1){
input.setText(text.concat("."));
}
}else if(e.getSource() == equal){
if(!operator.trim().equals("")){
if(!input.getText().trim().equals("")){
double result = 0D;
if(operator.equals("+")){
result = Double.parseDouble(num1) + Double.parseDouble(input.getText().trim());
}else if(operator.equals("-")){
result = Double.parseDouble(num1) - Double.parseDouble(input.getText().trim());
}else if(operator.equals("*")){
result = Double.parseDouble(num1) * Double.parseDouble(input.getText().trim());
}else if(operator.equals("/")){
result = Double.parseDouble(num1) / Double.parseDouble(input.getText().trim());
}

input.setText(new DecimalFormat("0.00").format(result));
}
}

}else if(e.getSource() == sign){
if(input.getText().length() > 0){
if(input.getText().charAt(0) == '-'){
input.setText(input.getText().substring(1));
}else{
input.setText("-".concat(input.getText()));
}
}

}else if(e.getSource() == sqrt){
double value = Double.parseDouble(input.getText());
if(value < 0){
JOptionPane.showMessageDialog(null, "Error! Can't calculate negative number for sqrt operation");
}else{
input.setText(String.valueOf(Math.sqrt(value)));
}
}else if(e.getSource() == percentage){
double value = Double.parseDouble(input.getText());
if(value != 0){
input.setText(String.valueOf(value / 100));
}
}else if(e.getSource() == fraction){
double value = Double.parseDouble(input.getText());
if(value != 0){
input.setText(String.valueOf(1 / value));
}
}

}

public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}

}本回答被提问者采纳
第2个回答  2011-12-30
自己回答一下问题,把所有的代码贴出来
相似回答