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

您没有登录

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

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:求教计算器问题(急~~~) [Re:rahoo]
j2ee8000





发贴: 15
积分: 0
于 2004-12-31 22:04 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
// writen by jason zhang
// 布局管理的学习
// 04.11.11
//------------------------------------------------------------------

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class layout_exp extends JFrame implements ActionListener{
  JButton num1=new JButton("1");
  JButton num2=new JButton("2");
  JButton num3=new JButton("3");
  JButton num4=new JButton("4");
  JButton num5=new JButton("5");
  JButton num6=new JButton("6");
  JButton num7=new JButton("7");
  JButton num8=new JButton("8");
  JButton num9=new JButton("9");
  JButton num0=new JButton("0");
  JButton op1=new JButton("+");
  JButton op2=new JButton("-");
  JButton op3=new JButton("*");
  JButton op4=new JButton("/");
  JButton op5=new JButton("=");
  JTextField result=new JTextField(15);
  int lastnum;
  int nownum;
  int count;
  int operator_type;
  layout_exp()
  {

    super("计算器");
    setSize(300,300);
    lastnum=0;
    nownum=0;
    count=0;
    operator_type=0;
    JPanel pane=new JPanel();
    GridLayout computer=new GridLayout(5,3,10,10);//网格布局10行3列,间距10
    pane.setLayout(computer);//面板选取布局
    pane.add(num1);
    pane.add(num2);
    pane.add(num3);
    pane.add(num4);
    pane.add(num5);
    pane.add(num6);
    pane.add(num7);
    pane.add(num8);
    pane.add(num9);
    pane.add(num0);
    pane.add(op1);
    pane.add(op2);
    pane.add(op3);
    pane.add(op4);
    pane.add(op5);
    JPanel pane1=new JPanel();//另一个面板,显示结果
    pane1.add(result);//添加控件到面板
    JPanel panetotal=new JPanel();//总的面板,包含上面的2个,注意此时上面的2个必须已设计完成
    GridLayout com=new GridLayout(2,1,10,10);
    panetotal.setLayout(com);
    panetotal.add(pane1);
    panetotal.add(pane);
    //下面添加响应事件
    num1.addActionListener(this);
    num2.addActionListener(this);
    num3.addActionListener(this);
    num4.addActionListener(this);
    num5.addActionListener(this);
    num6.addActionListener(this);
    num7.addActionListener(this);
    num8.addActionListener(this);
    num9.addActionListener(this);
    num0.addActionListener(this);
    op1.addActionListener(this);
    op2.addActionListener(this);
    op3.addActionListener(this);
    op4.addActionListener(this);
    op5.addActionListener(this);
    setContentPane(panetotal);//将面板加入框架
  }
  
  public void actionPerformed(ActionEvent evt)
  {//具体处理按键响应
    Object source=evt.getSource();
    if(source==num1)
    {
      
      if(count>0)
      {
        String t=result.getText();
       if(Integer.parseIntPhone==0)
        result.setText("");
     String temp=result.getText();
        result.setText(temp+1);
        count++;
        return;
      }
      
      else
      {
        result.setText("1");
        count++;
        return;
      }
    }  
    if(source==num2)
    {
      
      if(count>0)
      {
        String t=result.getText();
       if(Integer.parseIntPhone==0)
        result.setText("");
        String temp=result.getText();
        result.setText(temp+2);
        count++;
        return;
      }
      
      else
      {
        result.setText("2");
        count++;
        return;
      }
    }
    if(source==num3)
    {
      
      if(count>0)
      {
        String t=result.getText();
        if(Integer.parseIntPhone==0)
        result.setText("");
        String temp=result.getText();
        result.setText(temp+3);
        count++;
        return;
      }
      
      else
      {
        result.setText("3");
        count++;
        return;
      }
    }
    if(source==num4)
    {
      
      if(count>0)
      {
        String t=result.getText();
        if(Integer.parseIntPhone==0)
        result.setText("");
        String temp=result.getText();
        result.setText(temp+4);
        count++;
        return;
      }
      
      else
      {
        result.setText("4");
        count++;
        return;
      }
    }
    if(source==num5)
    {
      
      if(count>0)
      {
        String t=result.getText();
        if(Integer.parseIntPhone==0)
        result.setText("");
        String temp=result.getText();
        result.setText(temp+5);
        count++;
        return;
      }
      
      else
      {
        result.setText("5");
        count++;
        return;
      }
    }
    if(source==num6)
    {
      
      if(count>0)
      {
        String t=result.getText();
        if(Integer.parseIntPhone==0)
        result.setText("");
        String temp=result.getText();
        result.setText(temp+6);
        count++;
        return;
      }
      
      else
      {
        result.setText("6");
        count++;
        return;
      }
    }
    if(source==num7)
    {
      
      if(count>0)
      {
        String t=result.getText();
        if(Integer.parseIntPhone==0)
        result.setText("");
        String temp=result.getText();
        result.setText(temp+7);
        count++;
        return;
      }
      
      else
      {
        result.setText("7");
        count++;
        return;
      }
    }
    if(source==num8)
    {
      
      if(count>0)
      {
        String t=result.getText();
        if(Integer.parseIntPhone==0)
        result.setText("");
        String temp=result.getText();
        result.setText(temp+8);
        count++;
        return;
      }
      
      else
      {
        result.setText("8");
        count++;
        return;
      }
    }
    if(source==num9)
    {
      
      if(count>0)
      {
        String t=result.getText();
        if(Integer.parseIntPhone==0)
        result.setText("");
        String temp=result.getText();
        result.setText(temp+9);
        count++;
        return;
      }
      
      else
      {
        result.setText("9");
        count++;
        return;
      }
    }
    if(source==num0)
    {
      if(count>0)
      {
        String temp=result.getText();
        if(Integer.parseInt(temp)!=0)
         result.setText(temp+0);
        count++;
        return;
      }
      
      else
      {
        result.setText("0");
        count++;
        return;
      }
    }
    if(source==op1)
    {
      count=0;
      operator_type=1;
      String temp=result.getText();
      lastnum=Integer.parseInt(temp);
      return;
    }
    if(source==op2)
    {
      operator_type=2;
      count=0;
      String temp=result.getText();
      lastnum=Integer.parseInt(temp);
      return;
    }
    if(source==op3)
    {
      count=0;
      operator_type=3;
      String temp=result.getText();
      lastnum=Integer.parseInt(temp);
      return;
    }
    if(source==op4)
    {
      count=0;
      operator_type=4;
      String temp=result.getText();
      lastnum=Integer.parseInt(temp);
      return;
    }
    if(source==op5)
    {
      count=0;
      String temp=result.getText();
      nownum=Integer.parseInt(temp);//通过函数将JText中的字符串转换为整型
      switch(operator_type)
      {
        case 1:
          result.setText(String.valueOf(lastnum+nownum));
       break;
       case 2:
          result.setText(String.valueOf(lastnum-nownum));
       break;
       case 3:
          result.setText(String.valueOf(lastnum*nownum));
       break;
       case 4:
          result.setText(String.valueOf((float)((float)lastnum/(float)nownum)));
       break;
      }
      lastnum=0;
      nownum=0;
      return;
    }
  }
  public static void main(String[] arg){
    JFrame frame=new layout_exp();
    //添加窗口退出响应
    ExitWindow exit=new ExitWindow();
    frame.addWindowListener(exit);
    frame.show();
  }
}
    
  
class ExitWindow extends WindowAdapter{
  public void windowClosing(WindowEvent e)
  {
    System.exit(0);
  }
}
仅供参考!!



wertqwetwt

有了Swing为什么还要SWT?

话题树型展开
人气 标题 作者 字数 发贴时间
11800 求教计算器问题(急~~~) rahoo 314 2004-12-26 13:44
8791 Re:求教计算器问题(急~~~) doway 44 2004-12-26 22:50
8834 Re:求教计算器问题(急~~~) j2ee8000 6948 2004-12-31 22:04

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