Topic: 求教计算器问题(急~~~)

  Print this page

1.求教计算器问题(急~~~) Copy to clipboard
Posted by: rahoo
Posted on: 2004-12-26 13:44

一个计算器程序,有个问题一直不知道怎么解决,就是在上面那个TextField中如何让输入的数字从右边开始显示起?
我也查看了API,但没发现TextField有什么方法可以实现,只看到TextArea有一个方法可以在指定位置插入字符串 public void insert(String str, int pos),但好象那个pos只要输入大于0的数就说字符串下标越界,输入0就可以,但这样还是从左边开始,所以我估计那个pos 是指定哪一行的,因为在上面说的情况中,我只定义了TextArea为一行一列,所以输入pos为1就越界了,不知对否?
那怎样在TextField中让输入的数字从右边开始显示起?望高手指点~~~

2.Re:求教计算器问题(急~~~) [Re: rahoo] Copy to clipboard
Posted by: doway
Posted on: 2004-12-26 22:50

现成的怕是没有的吧,有的话应该是个属性,.Net中这个属性叫"RightToLeft"。

3.Re:求教计算器问题(急~~~) [Re: rahoo] Copy to clipboard
Posted by: j2ee8000
Posted on: 2004-12-31 22:04

// 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);
  }
}
仅供参考!!


   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