1 package 文本框;
2 import java.awt.*;
3 import java.awt.event.*;
4
5 import javax.swing.*;
6
7
8 public class wenbenkuangshuzu extends JFrame implements ActionListener
9 {
10 JPanel p1=new JPanel();
11 JPanel p2=new JPanel();
12 JButton button=new JButton("添加");
13 TextField t1=new TextField("",7);//作用是获得文本框的数量
14 int i=0;
15 public wenbenkuangshuzu()
16 {
17 super("添加n个文本框并标记");
18 this.setSize(300, 300);
19 this.setBackground(Color.lightGray);
20 JLabel b1=new JLabel("文本框数量");
21
22 this.add(p1,BorderLayout.NORTH);
23 p1.add(b1);
24 p1.add(t1);
25 p1.add(button);
26 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
27 this.add(p2,BorderLayout.CENTER);
28 button.addActionListener(this);
29 this.setVisible(true);
30 }
31
32 public void actionPerformed(ActionEvent e)
33 {
34 int m,n;
35 String str;
36 str=t1.getText();
37 n=Integer.parseInt(str);
38 JTextField t[]=new JTextField[n];
39 if(e.getSource()==button)
40 {
41 for(m=0;m<t.length;m++)
42 {
43 p2.add(t[m]);
44
45 }
46
47
48 }
49 }
50
51 public static void main(String arg[])
52 {
53 new wenbenkuangshuzu();
54 }
55
56
57 }
这个程序可以运行,但是在文本框t1中输入数值后;点击添加,文本框添加不上去,提示有错误,请大家帮忙看看啊!!!!!!!!!!后天要用啊,先谢谢啦!!!
添加回答
举报
0/150
提交
取消