我要做一个IP输入框,类似于微软的那个界面,输入完一个IP自动跳到下一个的,于是我在网上找了些资料
下面是代码,一共两个文件,分别能编译通过,执行时出现Exception in thread "main" java.lang.NoSuchMethodError: main
我不知道如何修改,各位帮我看看吧------------------------------第一个文件代码-------------------------------------------
  package   textField;   
    
  import   java.awt.*;   
  import   javax.swing.*;   
  import   java.util.*;   
  import   java.awt.event.*;   
  import   javax.swing.border.*;   
    
  public   class   IPAddressEditor   extends   JPanel   {   
  private   static   final   Dimension   fixedDimension   =   new   Dimension(148,26);   
  private   static   final   int   fixedWidth   =   148;   
  private   static   final   int   fixedHeight   =   26;   
    
  private   String   ipAddress;   
    
  DigitalText   digitalText1   =   new   DigitalText();   
  DigitalText   digitalText2   =   new   DigitalText();   
  DigitalText   digitalText3   =   new   DigitalText();   
  DigitalText   digitalText4   =   new   DigitalText();   
    
  JLabel   jLabel1   =   new   JLabel();   
  JLabel   jLabel2   =   new   JLabel();   
  JLabel   jLabel3   =   new   JLabel();   
    
  Border   border1;   
    
  public   IPAddressEditor()   {   
  try   {   
  jbInit();   
  }   
  catch(Exception   ex)   {   
  ex.printStackTrace();   
  }   
  }   
    
  private   void   jbInit()   throws   Exception   {   
  border1   =   BorderFactory.createEmptyBorder();   
    
  this.setLayout(null);   
  this.setBorder(BorderFactory.createEtchedBorder());   
  this.addComponentListener(new   java.awt.event.ComponentAdapter()   {   
    
  public   void   componentResized(ComponentEvent   e)   {   
  this_componentResized(e);   
  }   
  });   
    
  this.setSize(fixedWidth,fixedHeight);   
  this.setMaximumSize(fixedDimension);   
  this.setMinimumSize(fixedDimension);   
  this.setPreferredSize(fixedDimension);   
  //this.setRequestFocusEnabled(false);   
    
    
  digitalText1.setMaxLength(3);   
  digitalText1.setMaxValue(255);   
  digitalText1.setValue(255);   
  digitalText1.setBorder(border1);   
  digitalText1.setHorizontalAlignment(SwingConstants.CENTER);   
  digitalText1.setBounds(new   Rectangle(2,   2,   30,   22));   
  digitalText1.addKeyListener(new   java.awt.event.KeyAdapter()   {   
    
  public   void   keyReleased(KeyEvent   e)   {   
  digitalText1_keyReleased(e);   
  }   
  });   
    
  digitalText2.setBounds(new   Rectangle(40,   2,   30,   22));   
  digitalText2.addKeyListener(new   java.awt.event.KeyAdapter()   {   
    
  public   void   keyReleased(KeyEvent   e)   {   
  digitalText2_keyReleased(e);   
  }   
  });   
  digitalText2.setBorder(border1);   
  digitalText2.setHorizontalAlignment(SwingConstants.CENTER);   
  digitalText2.setMaxLength(3);   
  digitalText2.setMaxValue(255);   
  digitalText2.setValue(255);   
    
  digitalText3.setBounds(new   Rectangle(78,   2,   30,   22));   
  digitalText3.addKeyListener(new   java.awt.event.KeyAdapter()   {   
    
  public   void   keyReleased(KeyEvent   e)   {   
  digitalText3_keyReleased(e);   
  }   
  });   
  digitalText3.setBorder(border1);   
  digitalText3.setHorizontalAlignment(SwingConstants.CENTER);   
  digitalText3.setMaxLength(3);   
  digitalText3.setMaxValue(255);   
  digitalText3.setValue(255);   
    
  digitalText4.setBounds(new   Rectangle(116,   2,   30,   22));   
  digitalText4.addKeyListener(new   java.awt.event.KeyAdapter()   {   
    
  public   void   keyReleased(KeyEvent   e)   {   
  digitalText4_keyReleased(e);   
  }   
  });   
  digitalText4.setBorder(border1);   
  digitalText4.setHorizontalAlignment(SwingConstants.CENTER);   
  digitalText4.setMaxLength(3);   
  digitalText4.setMaxValue(255);   
  digitalText4.setValue(255);   
    
  jLabel1.setBackground(Color.white);   
  jLabel1.setFont(new   java.awt.Font("Dialog",   1,   14));   
  jLabel1.setForeground(Color.blue);   
  jLabel1.setOpaque(true);   
  jLabel1.setHorizontalAlignment(SwingConstants.CENTER);   
  jLabel1.setText(".");   
  jLabel1.setBounds(new   Rectangle(32,   2,   8,   22));   
    
  jLabel2.setBounds(new   Rectangle(70,   2,   8,   22));   
  jLabel2.setText(".");   
  jLabel2.setForeground(Color.blue);   
  jLabel2.setOpaque(true);   
  jLabel2.setHorizontalAlignment(SwingConstants.CENTER);   
  jLabel2.setHorizontalTextPosition(SwingConstants.CENTER);   
  jLabel2.setFont(new   java.awt.Font("Dialog",   1,   14));   
  jLabel2.setBackground(Color.white);   
    
  jLabel3.setBounds(new   Rectangle(108,   2,   8,   22));   
  jLabel3.setText(".");   
  jLabel3.setForeground(Color.blue);   
  jLabel3.setOpaque(true);   
  jLabel3.setHorizontalAlignment(SwingConstants.CENTER);   
  jLabel3.setFont(new   java.awt.Font("Dialog",   1,   14));   
  jLabel3.setBackground(Color.white);   
    
    
  this.add(digitalText1,   null);   
  this.add(jLabel1,   null);   
  this.add(digitalText2,   null);   
  this.add(jLabel2,   null);   
  this.add(digitalText3,   null);   
  this.add(jLabel3,   null);   
  this.add(digitalText4,   null);   
    
  this.setIpAddress("127.0.0.0");   
  }   
    
  public   void   setIpAddress(String   newIpAddress)   {   
    
  //以"/"为分割符   
  StringTokenizer   strToken=new   StringTokenizer(newIpAddress,".");   
  String[]   strIPAddress=new   String[strToken.countTokens()];   
    
  if(strToken.countTokens()!=4)   
  return;   
    
  //divide   the   newIpAddress   up   three   strings   
  int   k=0;   
  while(strToken.hasMoreTokens())   
  {   
  strIPAddress[k]=strToken.nextToken();   
  k++;   
  }   
    
  //set   value   for   each   TextField   
  try{   
  digitalText1.setValue(Integer.parseInt(strIPAddress[0]));   
  digitalText2.setValue(Integer.parseInt(strIPAddress[1]));   
  digitalText3.setValue(Integer.parseInt(strIPAddress[2]));   
  digitalText4.setValue(Integer.parseInt(strIPAddress[3]));   
    
  ipAddress   =   getIpAddress();   
  }   
  catch(Exception   e){   
  return;   
  }   
  }   
    
  public   String   getIpAddress()   {   
  ipAddress   =   Integer.toString(digitalText1.getValue())   +   "."   +   
  Integer.toString(digitalText2.getValue())   +   "."   +   
  Integer.toString(digitalText3.getValue())   +   "."   +   
  Integer.toString(digitalText4.getValue());   
  return   ipAddress;   
  }   
    
  void   this_componentResized(ComponentEvent   e)   {   
  this.setSize(fixedWidth,fixedHeight);   
  validate();   
  }   
    
  public   void   setBounds(int   l,int   t,int   w,int   h)   
  {   
  System.out.println("my   setBounds   fuction3!");   
  super.setBounds(l,t,fixedWidth,fixedHeight);   
  return;   
  }   
    
  void   digitalText1_keyReleased(KeyEvent   e)   {   
  if(digitalText1.getText().length()   >=   digitalText1.getMaxLength()){   
  digitalText2.requestFocus();   
  digitalText2.selectAll();   
  }   
  }   
    
  void   digitalText2_keyReleased(KeyEvent   e)   {   
  if(digitalText2.getText().length()   >=   digitalText2.getMaxLength()){   
  digitalText3.requestFocus();   
  digitalText3.selectAll();   
  }   
  }   
    
  void   digitalText3_keyReleased(KeyEvent   e)   {   
  if(digitalText3.getText().length()   >=   digitalText3.getMaxLength()){   
  digitalText4.requestFocus();   
  digitalText4.selectAll();   
  }   
  }   
    
  void   digitalText4_keyReleased(KeyEvent   e)   {   
  if(digitalText4.getText().length()   >=   digitalText4.getMaxLength()){   
  digitalText4.nextFocus();   
  }   
  }   
  }   
------------------------------------------------第一个文件代码结束------------------------------------------------

解决方案 »

  1.   

     ---------------------第二个文件代码---------------------------
      package   textField;   
        
      import   java.awt.*;   
      import   javax.swing.*;   
      import   javax.swing.text.*;   
      import   java.awt.event.*;   
      import   java.util.*;   
      import   java.io.*;   
        
      /**   
      *   用来输入正整数的单行文本框   
      *   可以控制输入的最大长度,最大值和最小值并在显示时可以选择是否在数字前面补零以达到最大长度   
      */   
      public   class   DigitalText   extends   JTextField   implements   Serializable   {   
        
      /**数据成员变量,分别代表当前值,最大值,最小值和最大长度,分别赋默认值*/   
      private   int   value   =   0;   
      private   int   maxValue   =   99;   
      private   int   minValue   =   0;   
      private   int   maxLength   =   2;   
      /**成员变量,代表输入框是否需要在最前面补零,true表示需要*/   
      private   boolean   enableAddZero   =   false;   
        
      //构造函数   
      public   DigitalText()   {   
      try   {   
      jbInit();   
      }   
      catch(Exception   ex)   {   
      ex.printStackTrace();   
      }   
      }   
      /**   
      *   <pre>   
      *   Function:   jbInit   
      *   Des   cription:   initialize,called   by   constructor   
      *   </pre>   
      *   @param   void   
      *   @return   void   
      *   @exception   void   
      */   
      private   void   jbInit()   throws   Exception   {   
      //set   default   property   such   as   background   color,   
      //doucument,value,alignment   etc.   
      setDocument(new   DigitalTextDocument());   
      setBackground(Color.white);   
      setHorizontalAlignment(JTextField.RIGHT);   
        
      setMaxLength(maxLength   =   2);   
      setMaxValue(maxValue   =   99);   
      setMinValue(minValue   =   0);   
      setValue(value   =   0);   
        
      //add   keyListener   and   focuslistener   
      this.addKeyListener(new   java.awt.event.KeyAdapter()   {   
        
      public   void   keyReleased(KeyEvent   e)   {   
      this_keyReleased(e);   
      }   
      });   
      addFocusListener(new   java.awt.event.FocusAdapter()   {   
        public   void   focusLost(FocusEvent   e)   {   
          this_focusLost(e);   
        }   
        
      public   void   focusGained(FocusEvent   e)   {   
      this_focusGained(e);   
      }   
        });   
      }   
        
      /**   
      *   <pre>   
      *   Function:   setValue(int   newValue)   
      *   Des   cription:   set   a   new   value   and   refresh   the   view   
      *   </pre>   
      *   @param   newValue   
      *   @return   void   
      *   @exception   void   
      */   
      public   void   setValue(int   newValue)   {   
      if((newValue   <=   maxValue)   &&   (newValue   >=   minValue))   
      value   =   newValue;   
      setText(Integer.toString(value));   
      adjustForZero();   
      revalidate();   
      }   
      //获取value   
      public   int   getValue()   {   
      return   value;   
      }   
        
      //设置最大值   
      public   void   setMaxValue(int   newMaxValue)   {   
      setMinMaxValue(minValue,newMaxValue);   
      }   
        
      //获取最大值   
      public   int   getMaxValue()   {   
      return   maxValue;   
      }   
        
      //设置最小值   
      public   void   setMinValue(int   newMinValue)   {   
      setMinMaxValue(newMinValue,maxValue);   
      }   
      //获取最小值   
      public   int   getMinValue()   {   
      return   minValue;   
      }   
        
      /**   
      *   <pre>   
      *   Function:   setMinMaxValue   
      *   Des   cription:   this   method   sets   the   minValue   and   maxValue   one   time,   
      *   called   by   setMinValue   and   setMaxValue,may   be   used   by   programmer.   
      *   </pre>   
      *   @param   newMainValue   
      *   @param   newMinValue   
      *   @return   void   
      *   @exception   void   
      */   
      public   void   setMinMaxValue(int   newMinValue,int   newMaxValue){   
      //at   first,save   two   parameters   
      int   tempMinValue   =   newMinValue;   
      int   tempMaxValue   =   newMaxValue;   
        
      //if   ralation   of   two   parameters   is   NOT   right,do   nothing   
      if(tempMinValue   >   tempMaxValue)   
      return;   
      if(tempMaxValue   <   0   ||   tempMinValue   >(int)Math.pow(10,maxLength)-1   )   
      return;   
        
      //if   one   of   the   two   parameters   is   out   of   right   boudary,set   news   value   for   them   
      if(tempMinValue   <   0)   
      tempMinValue   =   0;   
      if(tempMaxValue   >(int)Math.pow(10,maxLength)-1)   
      tempMaxValue   =   (int)Math.pow(10,maxLength)-1;   
        
      //give   data   member   new   values   
      this.maxValue   =   tempMaxValue;   
      this.minValue   =   tempMinValue;   
        
      //adjust   current   value   because   of   fresh   boudaries:minValue   and   maxValue   
      //being   set   again   
      if   (value   <   newMinValue)   
      setValue(newMinValue);   
      if   (value   >   newMaxValue)   
      setValue(newMaxValue);   
        
      //update   document's   members   to   hold   coherence   
      ((DigitalTextDocument)getDocument()).setMinValue(minValue);   
      ((DigitalTextDocument)getDocument()).setMaxValue(maxValue);   
      }   
        
      /**   
      *   <pre>   
      *   Function:   setMaxLength   
      *   Des   cription:   this   method   sets   the   maxLength   and   maybe   need   to   set   maxValue   
      *   ,minValue   enven   value   to   hold   logic   right   
      *   </pre>   
      *   @param   newMaxLength   
      *   @return   void   
      *   @exception   void   
      */   
      public   void   setMaxLength(int   newMaxLength)   {   
      //if   input   parameter   is   illegal,do   nothing   
      if(newMaxLength   <   1)   
      return;   
        
      //if   new   length   is   longer   than   the   old,need   NOT   to   change   boudaries,because   
      //new   value   scope   is   greater   than   the   old.All   value   is   still   legal   in   new   length   
      //if   new   length   is   shorter,the   case   is   complicated.There   need   to   adjust   some   
      //values   accordingly   
        
      //new   length   is   longer   than   the   old,   evaluate   only   accordingly   
      if(newMaxLength   >=   maxLength)   {   
      this.maxLength   =   newMaxLength;   
      ((DigitalTextDocument)getDocument()).setMaxLength(maxLength);   
      }   
      else{//new   length   is   shorter   
      //if   the   old   minValue   is   greater   than   the   new   MAXVALUE(old   maxValue   is   greater   ofcause)   
      //setMinMaxValue:0~the   new   MAXVALUE   
      if   (minValue   >   (int)Math.pow(10,newMaxLength)-1)   
      this.setMinMaxValue(0,(int)Math.pow(10,newMaxLength)-1);   
        
      //else   only   old   maxValue   is   greater   than   new   MAXVALUE   
      else   if   (maxValue   >(int)Math.pow(10,newMaxLength)-1)   
      this.setMinMaxValue(minValue,(int)Math.pow(10,newMaxLength)-1);   
        
      //evaluate   accrodingly   
      this.maxLength   =   newMaxLength;   
      ((DigitalTextDocument)getDocument()).setMaxLength(maxLength);   
      }   
      }   public   int   getMaxLength()   {   
      return   maxLength;   
      }   
        
      private   void   this_focusLost(FocusEvent   e){   
      commit();   
      }   
        
      void   writeObject(ObjectOutputStream   oos)   throws   IOException   {   
      oos.defaultWriteObject();   
      }   
        
      void   readObject(ObjectInputStream   ois)   throws   ClassNotFoundException,   IOException   {   
      ois.defaultReadObject();   
      }   
        
      -------------------------代码过长,转到3楼继续---------------------------
      

  2.   

    ------------------------------接着二楼,还是第二个文件的代码----------------------------------/**   
      *   <pre>   
      *   Function:   adjustForZero   
      *   Des   cription:   this   method   adjust   the   view   of   textfield.   if   enableAddZero   is   TRUE,   
      *   it   add   zeros   before   the   string   of   value   to   make   the   string   length   is   
      *   equal   to   maxLength.if   enableAddZero   is   FALSE,it   delete   zeros   which   are   
      *   the   head   of   string   and   are   not   contributing   for   value;   
      *   </pre>   
      *   @param   void   
      *   @return   void   
      *   @exception   void   
      */   
      public   void   adjustForZero(){   
      if(enableAddZero){   
      String   tempStr   =   Integer.toString(getValue());   
      int   addZeroNumber   =   maxLength   -   tempStr.length();   
            for(int   i   =   0;   i<   addZeroNumber;   i++)   
      tempStr=   "0"+tempStr;   
      setText(tempStr);   
      revalidate();   
      }   
      else{   
      int   tempValue   =   Integer.parseInt(getText().trim());   
      setText(Integer.toString(tempValue));   
      revalidate();   
      }   
      }   
        
      public   void   setEnableAddZero(boolean   newEnableAddZero)   {   
      enableAddZero   =   newEnableAddZero;   
      adjustForZero();   
      }   
        
      public   boolean   isEnableAddZero()   {   
      return   enableAddZero;   
      }   
        
      void   this_keyReleased(KeyEvent   e)   {   
      //if   is   ENTER,commit   current   value   
      if(e.getKeyCode()==e.VK_ENTER)   
      commit();   
      //if   is   ESCAPE,cancle   current   changes   
      if(e.getKeyCode()   ==   e.VK_ESCAPE){   
      setValue(value);   
      }   
      }   
      /**   
      *   <pre>   
      *   Function:   commit   
      *   Des   cription:   this   method   is   called   when   user   ends   the   current   edit   and   needs   
      *   save   current   value.It   is   a   private,called   in   KeyReleased   and   focusLost   
      *   methods.   
      *   </pre>   
      *   @param   void   
      *   @return   void   
      *   @exception   void   
      */   
      private   void   commit(){   
        try{   
          setValue(Integer.parseInt(getText()));   
        }   
        catch(Exception   ex){   
      setValue(value);   
      return;   
        }   
      }   
        
      void   this_focusGained(FocusEvent   e)   {   
      this.selectAll();   
      }   
      }   
        
        
      /******************************************************************************   
      *   Class:   DigitalTextDocument   
      *   Extends:   PlainDocument   
      ******************************************************************************/   
      class   DigitalTextDocument   extends   PlainDocument{   
        
        private   int   maxLength;   
        private   int   maxValue;   
        private   int   minValue;   
        
      //不帯参数的构造函数   
        public   DigitalTextDocument(){   
          super();   
          maxLength   =   2;   
          minValue   =   0;   
          maxValue   =   99;   
        }   
        
      //构造函数,   
        public   DigitalTextDocument(int   maxLength){   
          super();   
          maxLength   =   maxLength;   
          minValue   =   0;   
          maxValue   =   (int)Math.pow(10,maxLength)-1;   
        }   
        
      //构造函数,   
        public   DigitalTextDocument(int   maxLength,int   min,int   max){   
          super();   
          maxLength   =   maxLength;   
          minValue   =   Math.max(min,0);   
      maxValue   =   Math.min(max,(int)Math.pow(10,maxLength)-1);   
        }   
        
        
      //设定最大长度   
        public   void   setMaxLength(int   length){   
          maxLength   =   length;   
        }   
        
      //获取最大长度   
        public   int   getMaxLength(){   
          return   maxLength;   
        }   
        
      //设定最大值   
        public   void   setMaxValue(int   max){   
      maxValue   =   max;   
        }   
        
      //获取最大值   
        public   int   getMaxValue(){   
          return   maxLength;   
        }   
        
      //设定最小值   
        public   void   setMinValue(int   min){   
      minValue   =   min;   
        }   
        
      //获取最小值   
        public   int   getMinValue(){   
          return   minValue;   
        }   
        
      //重载父类的insertString函数   
        public   void   insertString(int   offset,String   str,AttributeSet   a)throws   BadLocationException{   
          int   valueAfterInsert   =   0;   
      String   strBeforeInsert   =   getText(0,getLength());   
      String   strAfterInsert   =   strBeforeInsert.substring(0,offset)+   
                    str+   
                    strBeforeInsert.substring(offset);   
        
        
          //首先保证插入该字符串后,是整数;如果不是,则不进行插入操作   
      try{   
      valueAfterInsert   =   Integer.parseInt(strAfterInsert);   
      }   
      catch   (   NumberFormatException   e){   
        
      return;   
      }   
        
          //如果插入字符创str后,文档超长,则插入失败   
          if(strAfterInsert.length()   >   maxLength)   
            return;   
      //如果插入后数据超出范围,插入失败   
      else   if(valueAfterInsert   >   maxValue)   
      return;   
      else   if((strAfterInsert.length()   ==   maxLength)   &&   (valueAfterInsert   <   minValue))   
      return;   
          else{   
            super.insertString(offset,str,a);   
            return;   
      }   
      }   
        
      private   boolean   isValid(String   str){   
      //首先保证插入该字符串后,是整数;如果不是,则不进行插入操作   
      int   valueAfterInsert;   
      try{   
      valueAfterInsert   =   Integer.parseInt(str);   
      }   
      catch   (   NumberFormatException   e){   
      return   false;   
      }   
        
          //如果插入字符创str后,文档超长,则插入失败   
          if(str.length()   >   maxLength)   
            return   false;   
      //如果插入后数据超出范围,插入失败   
      else   if((valueAfterInsert   >   maxValue)||(valueAfterInsert   <   minValue))   
      return   false;   
          else   
            return   true;   
      }   
      }   
    --------------第二个文件代码结束--------------------------------
      

  3.   

    那已经是全部异常了
    就是没有Main函数,可我不知道怎么去加,我是菜鸟啊- -
      

  4.   

    public static  void main(String[] a){
      JFrame f = new JFrame("test");
      Container c=f.getContentPane();
      c.setLayout(null);
      
      
      IpAddressEditor ie = new IpAddressEditor();
      ie.setSize(fixedWidth,fixedHeight);  
      ie.setLocation(0, 0);
      c.add(ie);
      
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.setSize(400, 300);
      f.setVisible(true);
      }