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

您没有登录

» Java开发网 » Java GUI 设计  

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





发贴: 41
积分: 0
于 2006-11-26 22:24 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
是applet中的计算器~
显示面板不能固定~来回跳~请高手帮看看代码~谢谢~

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class CalculatorApplet2 extends Applet implements ActionListener
{
  private Color background;
  private Label result;
  private Button c,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 c)
  {
    Panel p = new Panel();
    p.setLayout(lm);
    p.setBackground(c);
    return p;
  }
  private void makeButton()
  {
    Font f = 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=makeButton("C",lightRed,f);
    eq=makeButton("=",lightBlue,f);
    div=makeButton("/",lightBlue,f);
    times=makeButton("*",lightBlue,f);
    d7=makeButton("7",yellow,f);
    d8=makeButton("8",yellow,f);
    d9=makeButton("9",yellow,f);
    minus=makeButton("-",lightBlue,f);
    d4=makeButton("4",yellow,f);
    d5=makeButton("5",yellow,f);
    d6=makeButton("6",yellow,f);
    plus=makeButton("+",lightBlue,f);
    d1=makeButton("1",yellow,f);
    d2=makeButton("2",yellow,f);
    d3=makeButton("3",yellow,f);
    d0=makeButton("0",yellow,f);
    dp=makeButton(".",yellow,f);
    eq2=makeButton("=",lightRed,f);
  }
  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);
    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.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);
    clearCalc();
  }
  public void paint(Graphics g)
  {
    resize(row1.getWidth(),8*d1.getHeight());
    validate();
  }
  public void actionPerformed(ActionEvent e)
  {
    String s =(String)e.getActionCommand();
    if(digits.indexOf(s)!=-1)
    {
      handleDigit(s);
    }
    else if(s.equals("."))
    {
      handleDecimal();
    }
    else if(operators.indexOf(s)!=-1)
    {
      handleOperator(s);
    }
    else if(s.equals("C"))
    {
      handleClear();
    }
  }
  private void handleDigit(String s)
  {
    if(newOpd)
    {
      result.setText(s);
      newOpd =false;
    }
    else
    {
      result.setText(result.getText()+s);
      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());
  }
  
}



挖掘潜力
作者 Re:做了个简单计算器,布局好像有问题~ [Re:8261669]
8261669





发贴: 41
积分: 0
于 2006-11-26 22:25 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
马上就变成这样了~



挖掘潜力
作者 Re:做了个简单计算器,布局好像有问题~ [Re:8261669]
sc33



版主


发贴: 128
积分: 30
于 2006-12-06 12:54 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
注意paint()方法中的resize(row1.getWidth(),8*d1.getHeight());这一句,你去掉试一下。明白怎么回事了嘛?


作者 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

作者 Re:做了个简单计算器,布局好像有问题~ [Re:8261669]
liangx





发贴: 37
积分: 0
于 2007-02-15 17:31 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
这里有我常用的方法.
程序中把所有的按钮都用匿名对象来实现.避免了D0~D9的没个都要addActionListener这种累赘的方式.
程序只实现了按钮的功能.计算逻辑都没做.

package two;

import java.awt.BorderLayout;
import java.awt.Color;
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;

class CalcButton extends JButton {
  String num;
  public CalcButton(String c,Color color,ActionListener action){
    super(c);
    num=c;
    setBackground(color);
    addActionListener(action);
  }
}
public class Calc extends JFrame implements ActionListener{

  private final JTextField display=new JTextField();
  public Calc(){
    super("计算器");
    initComponent();
  }
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    new Calc().setVisible(true);
  }

  private void initComponent() {
    // TODO Auto-generated method stub
    JPanel c=(JPanel) getContentPane();
    
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    
    c.setLayout(new BorderLayout());
    c.add(display,BorderLayout.NORTH);
    JPanel centerPanel=new JPanel(new GridLayout(3,3));
    for(int i=1;i<10;++i){
      centerPanel.add(new CalcButton(String.valueOf(i),Color.cyan,this));
    }
    c.add(centerPanel);
    JPanel southPanel=new JPanel(new GridLayout(1,2));
    southPanel.add(new CalcButton("0",Color.cyan,this));
    southPanel.add(new CalcButton("c",Color.BLUE,this));
    c.add(southPanel,BorderLayout.SOUTH);
    setSize(150,220);
  }

  public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    CalcButton b= (CalcButton) e.getSource();
    if(b.num.equals("c"))
      display.setText("");
    else
      display.setText(display.getText()+b.num);
  }
}





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