Chinaunix首页 | 论坛 | 博客
  • 博客访问: 369561
  • 博文数量: 100
  • 博客积分: 2586
  • 博客等级: 少校
  • 技术积分: 829
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-09 15:20
个人简介

我是一个Java爱好者

文章分类

全部博文(100)

文章存档

2014年(2)

2013年(7)

2012年(2)

2010年(44)

2009年(28)

2008年(17)

我的朋友

分类: Java

2009-07-20 15:51:21

       //数字键/

       else if(e.getSource()==bt15)

       {

           if(flag==1||flag==2||flag==3)

           {}

           else

           {

              flag=4;

              if(tf.getText().trim().indexOf('/')==-1)

              {

                  x=Double.valueOf(tf.getText().trim());

                  tf.setText(tf.getText().trim()+"/");

                  y=0d;

              }

              else

              {

                  tf.setText(tf.getText().trim());

              }

           }

       }

       //清空

       else if(e.getSource()==bt17)

       {

           tf.setText("0");

           flag=0;

       }

       //Backspace

       else if(e.getSource()==bt18)

       {

           int len=tf.getText().trim().length();

           if(len>1)

           {

              tf.setText(tf.getText().trim().substring(0, len-1));

              flag=0;

           }

           else if(len==1)

           {

              tf.setText("0");

           }

          

       }

       //数字键=

       else if (e.getSource()==bt16)

       {

           System.out.println(flag);

           switch(flag)

           {

              case 1:

                  if(tf.getText().trim().indexOf('=')==-1)

                  {

                     str=tf.getText().trim();

                     int gy=str.indexOf('+');

                     if(str.length()-gy>1)

                     {

                         y=Double.valueOf(str.substring(gy+1));

                         result=x+y;

                         tf.setText(str+"="+String.valueOf(result));

                         break;

                     }

                    

                  }

              case 2:

                  if(tf.getText().trim().indexOf('=')==-1)

                  {

                     str=tf.getText().trim();

                     int gy=str.indexOf('-');

                     if(str.length()-gy>1)

                     {

                         y=Double.valueOf(str.substring(gy+1));

                         result=x-y;

                         tf.setText(str+"="+String.valueOf(result));

                         break;

                     }

                    

                  }

              case 3:

                  if(tf.getText().trim().indexOf('=')==-1)

                  {

                     str=tf.getText().trim();

                     int gy=str.indexOf('*');

                     if(str.length()-gy>1)

                     {

                         y=Double.valueOf(str.substring(gy+1));

                         result=x*y;

                         tf.setText(str+"="+String.valueOf(result));

                         break;

                     }

                    

                  }

              case 4:

                  if(tf.getText().trim().indexOf('=')==-1)

                  {

                     str=tf.getText().trim();

                     int gy=str.indexOf('/');

                     if(str.length()-gy>1)

                     {

                         y=Double.valueOf(str.substring(gy+1));

                         if(y==0)

                         {

                            tf.setText("除数不能为零");

                            break;

                         }

                         else

                         {

                            result=x/y;

                            tf.setText(str+"="+String.valueOf(result));

                            break;

                         }

                     }

                    

                  }

           }

       }

    }

    @Override

    public void keyPressed(KeyEvent e)

    {

       int keyNum=e.getKeyCode();

       //数字键0

       if(keyNum==96)

       {

           if(tf.getText().trim().indexOf('=')==-1)

           {

              if(tf.getText().equals("0"))

              {}

              else

              {

                  tf.setText(tf.getText().trim()+String.valueOf(e.getKeyCode()-96).trim());

              }

           }

       }

       //数字键1

       else if(keyNum==97)

       {

           if(tf.getText().trim().indexOf('=')==-1)

           {

              if(tf.getText().equals("0"))

              {

                  tf.setText(String.valueOf(e.getKeyCode()-96).trim());

                  //tf.setFocusable(true);

              }

              else

              {

                  tf.setText(tf.getText().trim()+String.valueOf(e.getKeyCode()-96).trim());

                  //tf.setFocusable(true);

              }

           }

       }

       //数字键2

       else if(keyNum==98)

       {

           if(tf.getText().trim().indexOf('=')==-1)

           {

              if(tf.getText().equals("0"))

              {

                  tf.setText(String.valueOf(e.getKeyCode()-96).trim());

              }

              else

              {

                  tf.setText(tf.getText().trim()+String.valueOf(e.getKeyCode()-96).trim());

              }

           }

       }

       //数字键3

       else if(keyNum==99)

       {

           if(tf.getText().trim().indexOf('=')==-1)

           {

              if(tf.getText().equals("0"))

              {

                  tf.setText(String.valueOf(e.getKeyCode()-96).trim());

              }

              else

              {

                  tf.setText(tf.getText().trim()+String.valueOf(e.getKeyCode()-96).trim());

              }

           }

       }

       //数字键4

        else if(keyNum==100)

       {

           if(tf.getText().trim().indexOf('=')==-1)

           {

              if(tf.getText().equals("0"))

              {

                  tf.setText(String.valueOf(e.getKeyCode()-96).trim());

              }

              else

              {

                  tf.setText(tf.getText().trim()+String.valueOf(e.getKeyCode()-96).trim());

              }

           }

       }

       //数字键5

       else if(keyNum==101)

       {

           if(tf.getText().trim().indexOf('=')==-1)

           {

              if(tf.getText().equals("0"))

              {

                  tf.setText(String.valueOf(e.getKeyCode()-96).trim());

              }

              else

              {

                  tf.setText(tf.getText().trim()+String.valueOf(e.getKeyCode()-96).trim());

              }

           }

       }

       //数字键6

       else if(keyNum==102)

       {

           if(tf.getText().trim().indexOf('=')==-1)

           {

              if(tf.getText().equals("0"))

              {

                  tf.setText(String.valueOf(e.getKeyCode()-96).trim());

              }

              else

              {

                  tf.setText(tf.getText().trim()+String.valueOf(e.getKeyCode()-96).trim());

              }

           }

       }

       //数字键7

       else if(keyNum==103)

       {

           if(tf.getText().trim().indexOf('=')==-1)

           {

              if(tf.getText().equals("0"))

              {

                  tf.setText(String.valueOf(e.getKeyCode()-96).trim());

              }

              else

              {

                  tf.setText(tf.getText().trim()+String.valueOf(e.getKeyCode()-96).trim());

              }

           }

       }

       //数字键8

       else if(keyNum==104)

       {

           if(tf.getText().trim().indexOf('=')==-1)

           {

              if(tf.getText().equals("0"))

              {

                  tf.setText(String.valueOf(e.getKeyCode()-96).trim());

              }

              else

              {

                  tf.setText(tf.getText().trim()+String.valueOf(e.getKeyCode()-96).trim());

              }

           }

       }

       //数字键9

       else if(keyNum==105)

       {

           if(tf.getText().trim().indexOf('=')==-1)

           {

              if(tf.getText().equals("0"))

              {

                  tf.setText(String.valueOf(e.getKeyCode()-96).trim());

              }

              else

              {

                  tf.setText(tf.getText().trim()+String.valueOf(e.getKeyCode()-96).trim());

              }

           }

       }

       //数字键+

       else if(keyNum==107)

       {

           if(flagk==2||flagk==3||flagk==4)

           {

              flagk=1;

           }

           else

           {

              flagk=1;

              if(tf.getText().trim().indexOf('+')==-1)

              {

                  x=Double.valueOf(tf.getText().trim());

                  tf.setText(tf.getText().trim()+"+");

                  y=0d;

               }

              else

              {

                  tf.setText(tf.getText().trim());

              }

           }

       }

       //数字键-

       else if(keyNum==109)

       {

           if(flagk==1||flagk==3||flagk==4)

           {

              flagk=2;

           }

           else

           {

              flagk=2;

              if(tf.getText().trim().indexOf('-')==-1)

              {

                  x=Double.valueOf(tf.getText().trim());

                  tf.setText(tf.getText().trim()+"-");

                  y=0d;

              }

              else

              {

                  tf.setText(tf.getText().trim());

              }

           }

       }

       //数字键*

       else if(keyNum==106)

       {

           if(flagk==1||flagk==2||flagk==4)

           {

              flagk=3;

           }

           else

           {

              flagk=3;

              if(tf.getText().trim().indexOf('*')==-1)

              {

                  x=Double.valueOf(tf.getText().trim());

                  tf.setText(tf.getText().trim()+"*");

                  y=0d;

              }

              else

              {

                  tf.setText(tf.getText().trim());

              }

           }

       }

       //数字键/

       else if(keyNum==111)

       {

           if(flagk==1||flagk==2||flagk==3)

           {

              flagk=4;

           }

           else

           {

              flagk=4;

              if(tf.getText().trim().indexOf('/')==-1)

              {

                  x=Double.valueOf(tf.getText().trim());

                  tf.setText(tf.getText().trim()+"/");

                  y=0d;

              }

              else

              {

                  tf.setText(tf.getText().trim());

              }

           }

       }

       //Backspace

       else if(keyNum==8)

       {

           int len=tf.getText().trim().length();

           if(len>1)

           {

              tf.setText(tf.getText().trim().substring(0, len-1));

              flagk=0;

           }

           else if(len==1)

           {

              tf.setText("0");

           }

           tf.setFocusable(true);

       }

       //回车键

       else if(keyNum==10)

       {

           switch(flagk)

           {

              case 1:

                  if(tf.getText().trim().indexOf('=')==-1)

                  {

                     str=tf.getText().trim();

                     int gy=str.indexOf('+');

                     if(str.trim().length()-gy>1)

                     {

                         y=Double.valueOf(str.substring(gy+1));

                         result=x+y;

                         tf.setText(str+"="+String.valueOf(result));

                         break;

                     }

                  }

              case 2:

                  if(tf.getText().trim().indexOf('=')==-1)

                  {

                     str=tf.getText().trim();

                     int gy=str.indexOf('-');

                     if(str.trim().length()-gy>1)

                     {

                         y=Double.valueOf(str.substring(gy+1));

                         result=x-y;

                         tf.setText(str+"="+String.valueOf(result));

                         break;

                     }

                  }

               case 3:

                  if(tf.getText().trim().indexOf('=')==-1)

                  {

                     str=tf.getText().trim();

                     int gy=str.indexOf('*');

                     if(str.trim().length()-gy>1)

                     {

                         y=Double.valueOf(str.substring(gy+1));

                         result=x*y;

                         tf.setText(str+"="+String.valueOf(result));

                         break;

                     }

                  }

              case 4:

                  if(tf.getText().trim().indexOf('=')==-1)

                  {

                     str=tf.getText().trim();

                     int gy=str.indexOf('/');

                     if(str.trim().length()-gy>1)

                     {

                         y=Double.valueOf(str.substring(gy+1));

                         if(y==0)

                         {

                            tf.setText("除数不能为零");

                            break;

                         }

                         else

                         {

                            result=x/y;

                            tf.setText(str+"="+String.valueOf(result));

                            break;

                         }  

                     }

                  }

           }

       }

    }

    @Override

    public void keyReleased(KeyEvent arg0)

    {

       // TODO Auto-generated method stub

    }

    @Override

    public void keyTyped(KeyEvent arg0)

    {

       // TODO Auto-generated method stub

    }

   

}

javax.swing包中,定义了两种类型的组件:顶层容器(JFrameJAppletJDialogJWindow)和轻量级组件。Swing组件都是AWTContainer类的直接子类和间接子类

  组件从功能上分可分为:

  1) 顶层容器:JFrame,JApplet,JDialog,JWindow共4个

  2) 中间容器:JPanel,JScrollPane,JSplitPane,JToolBar 

  3) 特殊容器:在GUI上起特殊作用的中间层,如JInternalFrame,JLayeredPane,JRootPane.

  4) 基本控件:实现人际交互的组件,如Jbutton, JComboBox, JList, JMenu, JSlider, JtextField。

  5) 不可编辑信息的显示:向用户显示不可编辑信息的组件,例如JLabel, JProgressBar, ToolTip。

  6) 可编辑信息的显示:向用户显示能被编辑的格式化信息的组件,如JColorChooser, JFileChoose, JFileChooser, Jtable, JtextArea。

  JComponent类的特殊功能又分为:

  1) 边框设置:使用setBorder()方法可以设置组件外围的边框,使用一个EmptyBorder对象能在组件周围留出空白。

  2) 双缓冲区:使用双缓冲技术能改进频繁变化的组件的显示效果。与AWT组件不同,JComponent组件默认双缓冲区,不必自己重写代码。如果想关闭双缓冲区,可以在组件上施加setDoubleBuffered(false)方法。

  3) 提示信息:使用setTooltipText()方法,为组件设置对用户有帮助的提示信息。

  4) 键盘导航:使用registerKeyboardAction( ) 方法,能使用户用键盘代替鼠标来驱动组件。JComponent类的子类AbstractButton还提供了便利的方法--用setMnemonic( )方法指明一个字符,通过这个字符和一个当前L&F的特殊修饰共同激活按钮动作。

  5) 可插入L&F:每个Jcomponent对象有一个相应的ComponentUI对象,为它完成所有的绘画、事件处理、决定尺寸大小等工作。 ComponentUI对象依赖当前使用的L&F,用UIManager.setLookAndFeel( )方法可以设置需要的L&F.

  6) 支持布局:通过设置组件最大、最小、推荐尺寸的方法和设置X、Y对齐参数值的方法能指定布局管理器的约束条件,为布局提供支持。

阅读(615) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~