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

您没有登录

» Java开发网 » Java GUI 设计  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:做了个简单计算器,布局好像有问题~ [Re:8261669]
sc33



版主


发贴: 128
积分: 30
于 2006-12-06 13:02 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 cjsdn;

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

public class CalculatorApplet extends Applet implements ActionListener {

  private Color background;

  private Label result;

  private Button c_key, eq, div, times, d7, d8, d9, minus, d4, d5, d6, plus, d1,
      d2, d3, eq2, d0, dp;

  private Panel row1, row2, row3, p12, p120, p3p, p3peq;

  private String lastOptr;

  private String Filler = " ";

  private boolean sawDecimal, newOpd, sawAnOpd;

  private double opd1;

  private int appletWidth, appletHeight;

  static final String initialString = "0.00000";

  static final String digits = "0123456789";

  static final String operators = "+-*/=";

  private Button makeButton(String label, Color color, Font font) {
    Button b = new Button(label);
    b.setBackground(color);
    b.setFont(font);
    return b;
  }

  private Panel makePanel(LayoutManager lm, Color color) {
    Panel p = new Panel();
    p.setLayout(lm);
    p.setBackground(color);
    return p;
  }

  private void makeButton() {
    Font font = new Font("Courier", Font.BOLD, 10);
    Color lightRed = new Color(255, 100, 100);
    Color lightBlue = new Color(100, 100, 255);
    Color yellow = new Color(255, 255, 0);
    c_key = makeButton("C", lightRed, font);
    eq = makeButton("=", lightBlue, font);
    div = makeButton("/", lightBlue, font);
    times = makeButton("*", lightBlue, font);
    d7 = makeButton("7", yellow, font);
    d8 = makeButton("8", yellow, font);
    d9 = makeButton("9", yellow, font);
    minus = makeButton("-", lightBlue, font);
    d4 = makeButton("4", yellow, font);
    d5 = makeButton("5", yellow, font);
    d6 = makeButton("6", yellow, font);
    plus = makeButton("+", lightBlue, font);
    d1 = makeButton("1", yellow, font);
    d2 = makeButton("2", yellow, font);
    d3 = makeButton("3", yellow, font);
    d0 = makeButton("0", yellow, font);
    dp = makeButton(".", yellow, font);
    eq2 = makeButton("=", lightRed, font);
  }

  public void init() {
    background = new Color(200, 255, 255);
    this.setLayout(new FlowLayout(FlowLayout.CENTER, 4, 1));
    result = new Label(initialString, Label.RIGHT);
    result.setBackground(new Color(255, 255, 255));
    add(result);
    makeButton();
    row1 = makePanel(new FlowLayout(FlowLayout.LEFT, 4, 2), background);
    row1.add(c_key);
    row1.add(eq);
    row1.add(div);
    row1.add(times);
    row2 = makePanel(new FlowLayout(FlowLayout.LEFT, 4, 2), background);
    row2.add(d7);
    row2.add(d8);
    row2.add(d9);
    row2.add(minus);
    row3 = makePanel(new FlowLayout(FlowLayout.LEFT, 4, 2), background);
    row3.add(d4);
    row3.add(d5);
    row3.add(d6);
    row3.add(plus);
    add(row1);
    add(row2);
    add(row3);
    p12 = makePanel(new BorderLayout(2, 2), background);
    p12.add("West", d1);
    p12.add("East", d2);
    p120 = makePanel(new BorderLayout(2, 2), background);
    p120.add("North", p12);
    p120.add("South", d0);
    p3p = makePanel(new BorderLayout(2, 2), background);
    p3p.add("North", d3);
    p3p.add("South", dp);
    p3peq = makePanel(new BorderLayout(2, 2), background);
    p3peq.add("West", p3p);
    p3peq.add("East", eq2);
    add(p120);
    add(p3peq);
    setBackground(background);
    c_key.addActionListener(this);
    eq.addActionListener(this);
    eq2.addActionListener(this);
    dp.addActionListener(this);
    plus.addActionListener(this);
    minus.addActionListener(this);
    times.addActionListener(this);
    div.addActionListener(this);
    d1.addActionListener(this);
    d2.addActionListener(this);
    d3.addActionListener(this);
    d4.addActionListener(this);
    d5.addActionListener(this);
    d6.addActionListener(this);
    d7.addActionListener(this);
    d8.addActionListener(this);
    d9.addActionListener(this);
    d0.addActionListener(this);
    resize(100,168);
    clearCalc();
  }

  public void paint(Graphics g) {
    validate();
  }

  public void actionPerformed(ActionEvent e) {
    String str = (String) e.getActionCommand();
    if (digits.indexOf(str) != -1) {
      handleDigit(str);
    } else if (str.equals(".")) {
      handleDecimal();
    } else if (operators.indexOf(str) != -1) {
      handleOperator(str);
    } else if (str.equals("C")) {
      handleClear();
    }
  }

  private void handleDigit(String str) {
    if (newOpd) {
      result.setText(str);
      newOpd = false;
    } else {
      result.setText(result.getText() + str);
      sawAnOpd = true;
    }
  }

  private void handleClear() {
    clearCalc();
  }

  private void clearCalc() {
    sawDecimal = false;
    newOpd = true;
    sawAnOpd = false;
    lastOptr = "";
    //result.setText(initialString);
  }

  private void handleOperator(String s) {
    if (!sawAnOpd) {
      return;
    }
    if (lastOptr.equals("+")) {
      opd1 += getDisplay();
    } else if (lastOptr.equals("-")) {
      opd1 -= getDisplay();
    } else if (lastOptr.equals("*")) {
      opd1 *= getDisplay();
    } else if (lastOptr.equals("/")) {
      opd1 /= getDisplay();
    }
    result.setText(opd1 + "");
    lastOptr = s;
    sawDecimal = false;
    newOpd = true;
  }

  private void handleDecimal() {
    if (newOpd) {
      result.setText("0.");
      newOpd = false;
    } else {
      result.setText(result.getText() + ".");
      sawDecimal = true;
    }
    //result.setText(result.getText()+".");
  }

  private double getDisplay() {
    return Double.parseDouble(result.getText());
  }

}


sc33 edited on 2006-12-06 13:14


话题树型展开
人气 标题 作者 字数 发贴时间
8986 做了个简单计算器,布局好像有问题~ 8261669 5133 2006-11-26 22:24
7648 Re:做了个简单计算器,布局好像有问题~ 8261669 9 2006-11-26 22:25
7601 Re:做了个简单计算器,布局好像有问题~ sc33 74 2006-12-06 12:54
7509 Re:做了个简单计算器,布局好像有问题~ sc33 5405 2006-12-06 13:02
7670 Re:做了个简单计算器,布局好像有问题~ liangx 2639 2007-02-15 17:31

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