Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java GUI 设计  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:问一个菜鸟问题 [Re:dickran]
lisliefor





发贴: 287
积分: 7
于 2006-05-28 19:15 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
写得不错~
修改的地方我都加了注释,自己看吧!

package help;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

/**
* <p>
* Title:
* </p>
* <p>
* Description:
* </p>
* <p>
* Copyright: Copyright 2006
* </p>
* <p>
* Company:
* </p>
*
* @author unascribed
* @version 1.0
*/

public class Frame1 extends Frame implements ActionListener {
  TextField t = new TextField("", 15);
  Panel p1 = new Panel();
  Button[] b = new Button[10];
  Button bAdd = new Button("+");
  Button bSub = new Button("-");
  Button bMul = new Button("*");
  Button bDiv = new Button("/");
  Button bPoint = new Button(".");
  Button bEqual = new Button("=");
  Button bNull = new Button("清空");
  
  //菜单条、菜单、菜单项
  MenuBar bar = new MenuBar();
  Menu menu = new Menu("Mmenu");
  MenuItem abourt = new MenuItem("Abourt");
  MenuItem help = new MenuItem("Help");
  
  String str1 = "";
  String str2 = "";
  String operator = null;
  
  boolean first = true;
  int countOper = 0;
  double result = 0.0;
  double num1 = 0.0, num2 = 0.0;
  boolean error = false;

  public Frame1() {
    t.setEditable(false);
    for (int i = 0; i < b.length; i++) {
      b[i] = new Button((i+1)+""); //这里我小改动了一下
      p1.add(b[i]);
      b[i].setActionCommand("number");
      b[i].addActionListener(this);
    }
    p1.add(bPoint);
    bPoint.setActionCommand("number");
    p1.add(bAdd);
    p1.add(bSub);
    p1.add(bMul);
    p1.add(bDiv);
    p1.add(bEqual);
    p1.add(bNull);
    bAdd.setActionCommand("oper");
    bSub.setActionCommand("oper");
    bMul.setActionCommand("oper");
    bDiv.setActionCommand("oper");
    bAdd.addActionListener(this);
    bSub.addActionListener(this);
    bMul.addActionListener(this);
    bDiv.addActionListener(this);
    bPoint.addActionListener(this);
    bEqual.addActionListener(this);
    bNull.addActionListener(this);
    p1.setLayout(new GridLayout(4, 4, 5, 5));
    add(t, "North");
    add(p1, "Center");
    setLocation(400, 200);
    setSize(200, 200);
    setBackground(Color.lightGray);
    setVisible(true);
    
    //自己给abourt、help添加监听器,实现你想的功能
    setMenuBar(bar);
    bar.add(menu);
    menu.add(abourt);
    menu.add(help);

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
  }

  public void actionPerformed(ActionEvent e) {
    Button temp = (Button) e.getSource();
    if (e.getActionCommand().equals("number")) {
      if (first) {
        str1 = str1 + temp.getLabel();
        t.setText(str1);
      } else {
        str2 = str2 + temp.getLabel();
        t.setText(str2);
      }
    } else if (e.getActionCommand().equals("oper")) {
      if (str1 == "") {
        countOper = 0;
        first = true;
      } else {
        countOper++;
        if (countOper > 1) {
          getResult();
        }
        operator = temp.getLabel();
        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 == "")) {
        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) {
        error = true;
      } else {
        result = num1 / num2;
      }
    }
    if (error) {
      t.setText("error");
    } else {
      t.setText(String.valueOf(result));
      str1 = String.valueOf(result);
      str2 = "";
    }
  }

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

}




话题树型展开
人气 标题 作者 字数 发贴时间
6263 问一个菜鸟问题 dickran 3559 2006-05-28 11:52
5256 Re:问一个菜鸟问题 lisliefor 3807 2006-05-28 19:15
5316 Re:问一个菜鸟问题 dickran 5 2006-05-30 14:59

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923