java问题

3、Windows操作系统自带的计算器是个很方便的小工具,利用Java的GUI编程,实现一个JavaGUI计算器应用程序界面,窗口标题为“计算器”,窗口布局如下图所示,在... 3、 Windows操作系统自带的计算器是个很方便的小工具,利用Java的GUI编程,实现一个Java GUI计算器应用程序界面,窗口标题为“计算器”,窗口布局如下图所示,在此计算器应用程序中实现“+、-、*、/”运算操作。(25分) 展开
 我来答
钺一籁5801
2009-09-26 · TA获得超过422个赞
知道小有建树答主
回答量:112
采纳率:0%
帮助的人:87.4万
展开全部
import java.awt.*;
import java.awt.event.*;

public class CalcAppDemo extends Frame{
private TextField t_result;
private Panel p_main;
private Panel p_num;
private Panel p_oper;
private Panel p_show;
private Button b_num[];
private Button b_oper[];

public CalcAppDemo(String title){
setTitle(title);
setLocation(500,300);
t_result=new TextField("0.0",23);
p_main=new Panel();
p_num=new Panel();
p_oper=new Panel();
p_show=new Panel();
p_main.setLayout(new BorderLayout());
p_num.setLayout(new GridLayout(4,3,1,1));
p_oper.setLayout(new GridLayout(4,2,1,1));

b_num=new Button[12];
for(int i=0;i<9;i++){
b_num[i]=new Button(new Integer(i+1).toString());
}
b_num[9]=new Button(" 0");
b_num[10]=new Button(" cls");
b_num[11]=new Button(" .");
for(int i=0;i<12;i++){
p_num.add(b_num[i]);
}

b_oper=new Button[8];
b_oper[0]=new Button(" +");
b_oper[1]=new Button(" -");
b_oper[2]=new Button(" *");
b_oper[3]=new Button(" /");
b_oper[4]=new Button(" pow");
b_oper[5]=new Button(" sqrt");
b_oper[6]=new Button(" +/-");
b_oper[7]=new Button(" =");
for(int i=0;i<8;i++){
p_oper.add(b_oper[i]);
}

t_result.setEditable(false);
p_show.add(t_result,BorderLayout.NORTH);
p_main.add(p_show,BorderLayout.NORTH);
p_main.add(p_num,BorderLayout.WEST);
p_main.add(p_oper,BorderLayout.EAST);
this.add(p_main,BorderLayout.CENTER);
setSize(400,400);
setResizable(false);
pack();

this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
ButtonListener bl=new ButtonListener();
for(int i=0;i<12;i++){
b_num[i].addActionListener(bl);
}
for(int i=0;i<8;i++){
b_oper[i].addActionListener(bl);
}
}

class ButtonListener implements ActionListener{
private String lastOp;
private String strVal;
private double total;
private double number;
private boolean firsttime;
private boolean operatorPressed;

ButtonListener(){
firsttime=true;
strVal="";
}

public void actionPerformed(ActionEvent e){
String s=((Button)e.getSource()).getLabel().trim();

if(Character.isDigit(s.charAt(0))){
handleNumber(s);
}
else{
calculate(s);
}
}

void calculate(String op){
operatorPressed=true;

if(firsttime&&!isUnary(op)){
total=getNumberOnDisplay();
firsttime=false;
}
if(isUnary(op)){
handleUnaryOp(op);
}
else if(lastOp!=null){
handleBinaryOp(lastOp);
}
if(!isUnary(op)){
lastOp=op;
}
}

boolean isUnary(String s){
return s.equals("=")||s.equals("cls")||s.equals("sqrt")||s.equals("+/-")||s.equals(".");
}

void handleUnaryOp(String op){
if(op.equals("+/-")){
number=negate(getNumberOnDisplay()+"");
t_result.setText("");
t_result.setText(number+"");
return;
}
else if(op.equals(".")){
handleDecPoint();
return;
}
else if(op.equals("sqrt")){
number=Math.sqrt(getNumberOnDisplay());
t_result.setText("");
t_result.setText(number+"");
return;
}
else if(op.equals("=")){
if(lastOp!=null&&!isUnary(lastOp)){
handleBinaryOp(lastOp);
}
lastOp=null;
firsttime=true;
return;
}
else{
clear();
}
}

void handleBinaryOp(String op){
if(op.equals("+")){
total+=number;
}
else if(op.equals("-")){
total-=number;
}
else if(op.equals("*")){
total*=number;
}
else if(op.equals("/")){
try{
total/=number;
}catch(ArithmeticException ae){
}
}
else if(op.equals("pow")){
total=Math.pow(total,number);
}
t_result.setText("");
lastOp=null;
strVal="";
number=0;
t_result.setText(total+"");
}

void handleNumber(String s){
if(!operatorPressed){
strVal+=s;
}
else{
operatorPressed=false;
strVal=s;
}
number=new Double(strVal).doubleValue();
t_result.setText("");
t_result.setText(strVal);
}

void handleDecPoint(){
operatorPressed=false;
if(strVal.indexOf(".")<0){
strVal+=".";
}
t_result.setText("");
t_result.setText(strVal);
}

double negate(String s){
operatorPressed=false;
if(number==(int)number){
s=s.substring(0,s.indexOf("."));
}
if(s.indexOf("-")<0){
strVal="-"+s;
}
else{
strVal=s.substring(1);
}
return new Double(strVal).doubleValue();
}

double getNumberOnDisplay(){
return new Double(t_result.getText()).doubleValue();
}

void clear(){
firsttime=true;
lastOp=null;
strVal="";
total=0;
number=0;
t_result.setText("0");
}
}

public static void main(String args[]){
CalcAppDemo c=new CalcAppDemo("简单计算器");
c.setVisible(true);
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
wxflq2006
2009-10-08
知道答主
回答量:17
采纳率:0%
帮助的人:0
展开全部
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Computer implements ActionListener{
String str;
String fuhao;
double count1=0;
double count2=0;
JFrame jf =new JFrame("计算器");
JTextField jtf =new JTextField(10);
int a,b;
public Computer(){

JPanel jp =new JPanel();
String[] lab ={"backs","ce","c","=",
"7","8","9","+",
"4","5","6","-",
"1","2","3","*",
"0",".","+/-","/"};

jp.setLayout(new GridLayout(5,4));
JButton[] jb =new JButton[lab.length];
jf.add(jtf,BorderLayout.NORTH);
for(int i=0;i<jb.length;i++){
jb[i]=new JButton(lab[i]);
jp.add(jb[i]);
}

for (int i=0;i<20;i++)
{jb[i].addActionListener(this);
}
jf.add(jp);//默认放在center
jf.setSize(300,200);
jf.setLocation(350,250);
jf.setResizable(false);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
public void actionPerformed(ActionEvent ae) {
String com = ae.getActionCommand();
double result = 0;

if(com.equals("+/-"))
{
jtf.setText("-"+jtf.getText());
}

if (com.equals("backs")){
String s=jtf.getText();
if(str.length()>0){
jtf.setText(s.substring(0, s.length()-1));
}
}
if(com.equals("ce")||com.equals("c")){
jtf.setText("0");
}
if("+".equals(com)){
fuhao=com;
count1=Double.parseDouble(jtf.getText());
str=jtf.getText()+com;
jtf.setText(str);
}
if("-".equals(com)){
fuhao=com;
count1=Double.parseDouble(jtf.getText());
str=jtf.getText()+com;
jtf.setText(str);
}
if("*".equals(com)){
fuhao=com;
count1=Double.parseDouble(jtf.getText());
str=jtf.getText()+com;
jtf.setText(str);
}
if("/".equals(com)){
fuhao=com;
count1=Double.parseDouble(jtf.getText());
str=jtf.getText()+com;
jtf.setText(str);
}

if("0".equals(com)||"1".equals(com)||"2".equals(com)||"3".equals(com)||"4".equals(com)||"5".equals(com)||"6".equals(com)||"7".equals(com)||"8".equals(com)||"9".equals(com))
{ jtf.setText(jtf.getText()+com);}
if("=".equals(com)){
str=jtf.getText()+com;
count2=Double.parseDouble(str.substring(str.indexOf(fuhao)+1,str.length()-1));
if("+".equals(fuhao)) result=count1+count2;
if("-".equals(fuhao)) result=count1-count2;
if("*".equals(fuhao)) result=count1*count2;
if("/".equals(fuhao)) result=count1/count2;

jtf.setText(result+"");}}

// TODO Auto-generated method stub
public static void main(String[] args) {

new Computer();

}

}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
大时代暑
2009-09-26 · TA获得超过2.5万个赞
知道大有可为答主
回答量:4111
采纳率:0%
帮助的人:4828万
展开全部
import java.awt.*;

public class TestGridLayout {

public static void main(String[] args) {
Frame f = new Frame("计算器");
Panel p = new Panel(new GridLayout(3,5,4,4));
String[] key = new String[]{"0","1","2","3","4","5","6","7","8","9","*","-","+","/","."};
for(int i=0;i<15;i++){
p.add(new Button(key[i]));
}
f.add(p,BorderLayout.CENTER);
f.add(new TextField(30), BorderLayout.NORTH);
f.setLocation(300,300);
f.pack();
f.setVisible(true);
}

}
没写全,写全太麻烦了,程序也发不上去
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式