得到textfield的textdocument,控制里面的几个接口方法。
不过你的正整数是个位数吗?

解决方案 »

  1.   

    public class FrameTest extends JFrame
    {
       public FrameTest()
       {}
       
       public static void main(String args[])
       {
    }
    }
      

  2.   

    public class Frame1 extends JFrame implements ActionListener
    {
       JButton button;
       JTextField field;   public Frame1()
       {
         super();
         button = new JButton("Close");
         button.addActionListener(this);
         field = new JTextField();     this.getContentPane().setLayout(new FlowLayout());
         this.getContentPane().add(button);
         this.getContentPane().add(field);
         this.setSize(400, 300);
         this.setVisible(true);
       }   public void actionPerformed(ActionEvent event)
       {
         if(event.getSource() == button)
         {
           if(field.getText() != null)
           {
             try
             {
                int num = Integer.parseInt(field.getText());
              
                dispose();
                new Frame2(num);
             }catch(NumberFormatException nfe) { }
           }
         }
       }
    }
    public class Frame2 extends JFrame
    {
       int num;
        
       public Frame2(int num)
       {
          this.num = num;      this.getContentPane().setLayout(new FlowLayout());
          JLabel labels[] = new JLabel[num];
          for(int i=0; i<labels.length; i++)
          {
            this.getContentPane().add(labels[i]);
          }
          this.setSize(400, 300);
          this.setVisible(true);
        }
    }
      

  3.   

    hehe gz
     mercury1231原代码写的有水平
      

  4.   

    前面老兄说得挺好,但好像有点小问题
    public class Frame1 extends JFrame implements ActionListener
    {
       JButton button;
       JTextField field;   public Frame1()
       {
         super();
         button = new JButton("Close");
         button.addActionListener(this);
         field = new JTextField();     this.getContentPane().setLayout(new FlowLayout());
         this.getContentPane().add(button);
         this.getContentPane().add(field);
         this.setSize(400, 300);
         this.setVisible(true);
       }   public void actionPerformed(ActionEvent event)
       {
         if(event.getSource() == button)
         {
           if(field.getText() != null)
           {
             try
             {
                int num = Integer.parseInt(field.getText());
              
                dispose();
                new Frame2(num);
             }catch(NumberFormatException nfe) { }
           }
         }
       }
    }
    public class Frame2 extends JFrame
    {
       int num;
        
       public Frame2(int num)
       {
          this.num = num;      this.getContentPane().setLayout(new FlowLayout());
          JLabel labels[] = new JLabel[num];
          for(int i=0; i<labels.length; i++)
          {
             labels[i]=new JLabel("fd");//要加一句
            this.getContentPane().add(labels[i]);
          }
          this.setSize(400, 300);
          this.setVisible(true);
        }
    }