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

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 自己的作业,运行不了,能力有限查不出错误,希望各大虾帮帮忙,急!!!!
king431





发贴: 13
积分: 0
于 2005-11-08 20:30 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
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Count {
  public static void main(String args[])
  {
    JFrame Myframe=new JFrame();
    Myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Myframe.show();
  }
}
class Myframe extends JFrame
{
  public static final int HEIGHT=200;
  public static final int WIDHT=300;
  public Myframe()
  {
    setTitle("计算器");
    setSize(WIDHT,HEIGHT);
    Container con=getContentPane();
    CountPanel countpanel=new CountPanel();
    con.add(countpanel,BorderLayout.CENTER);
  }
}
class CountPanel extends JPanel
{
  private JPanel panel=new JPanel();
  private String lastcommand="=";
  private double result=0;
  private boolean start=true;
      
  setLayout(new GridBagLayout());
  private JTextField Numshow=new JTextField("0",15);
  private JButton CE=new JButton("CE");
  private JButton Backspace=new JButton("Backspace");
  private JButton Num0=new JButton("0");
  private JButton Num1=new JButton("1");
  private JButton Num2=new JButton("2");
  private JButton Num3=new JButton("3");
  private JButton Num4=new JButton("4");
  private JButton Num5=new JButton("5");
  private JButton Num6=new JButton("6");
  private JButton Num7=new JButton("7");
  private JButton Num8=new JButton("8");
  private JButton Num9=new JButton("9");
  private JButton Add=new JButton("+");
  private JButton Sub=new JButton("-");
  private JButton Mul=new JButton("*");
  private JButton Div=new JButton("/");
  private JButton Equ=new JButton("=");
  
  ActionListener numAction=new NumAction();
  ActionListener Command=new CommandAction();
  
  GridBagConstraints constraints=new GridBagConstraints();
  constraints.fill=GridBagConstraints.HORIZONTAL;
  constraints.weightx = 6;
  constraints.weighty = 4;
  
  addText(Numshow,constraints,0,0,1,4);
  
  addButton(CE,constraints,1,0,1,2,Command);
  addButton(Backspace,constraints,1,2,1,2,Command);
  
  addButton(Num7,constraints,2,0,1,1,numAction);
  addButton(Num8,constraints,2,1,1,1,numAction);
  addButton(Num9,constraints,2,2,1,1,numAction);
  addButton(Div,constraints,2,3,1,1,Command);
  
  addButton(Num4,constraints,3,0,1,1,numAction);
  addButton(Num5,constraints,3,1,1,1,numAction);
  addButton(Num5,constraints,3,2,1,1,numAction);
  addButton(Mul,constraints,3,3,1,1,Command);
  
  addButton(Num1,constraints,4,0,1,1,numAction);
  addButton(Num2,constraints,4,1,1,1,numAction);
  addButton(Num3,constraints,4,2,1,1,numAction);
  addButton(Sub,constraints,4,3,1,1,Command);
  
  addButton(Num0,constraints,5,0,1,1,numAction);
  addButton(Equ,constraints,5,1,1,2,Command);
  addButton(Add,constraints,5,3,1,1,Command);
  }
  
  public void addText(JTextField c,GridBagConstraints constraints,int x,int y,int w,int h)
  {
    constraints.gridx=x;
    constraints.gridy=y;
    constraints.gridwidth=w;
    constraints.gridheight=h;
    add(c,constraints);
  }
  public void addButton(Button button,GridBagConstraints constraints,int x,int y,int w,int h,ActionListener listener)
  {
    constraints.gridx=x;
    constraints.gridy=y;
    constraints.gridwidth=w;
    constraints.gridheight=h;
    button.addActionListener(listener);
    add(button,constraints);
  }
  private class NumAction implements ActionListener
  {
    public void actionPerformed(ActionEvent event)
    {
      String input=event.getActionCommand();
      if(start)
      {
        Numshow.setText("");
        start=false;
      }
      Numshow.setText(Numshow.getText() +input);
    }
  }
  private class CommandAction implements ActionListener
  {
    public void actionPerformed(ActionEvent evt)
    {
      String command=evt.getActionCommand();
      if(start)
      {
        if(command.equals("-"))
        {
          Numshow.setText(command);
          start=false;
        }
        else
          lastcommand=command;
      }
      else
      {
        calculate(Integer.parseInt(Numshow.getText()));
        lastcommand=command;
        start=true;
      }
    }
  }
  public void calculate (int x)
  {
    if(lastcommand.equals("+")) result+=x;
    else if(lastcommand.equals("-")) result-=x;
    else if(lastcommand.equals("*")) result*=x;
    else if(lastcommand.equals("/")) result/=x;
    else if(lastcommand.equals("=")) result=x;
    else if(lastcommand.equals("CE")) result=0;
    else if(lastcommand.equals("Backspace")) result=result/10;
    Numshow.setText(""+result);
  }
}




话题树型展开
人气 标题 作者 字数 发贴时间
5344 自己的作业,运行不了,能力有限查不出错误,希望各大虾帮帮忙,急!!!! king431 4314 2005-11-08 20:30
4289 Re:自己的作业,运行不了,能力有限查不出错误,希望各大虾帮帮忙,急!!!! francis36 14 2005-11-09 00:45
4402 Re:自己的作业,运行不了,能力有限查不出错误,希望各大虾帮帮忙,急!!!! annieKim 5208 2005-11-09 08:58

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