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);//作用是獲得文本框的數(shù)量
14 int i=0;
15 public wenbenkuangshuzu()
16 {
17 super("添加n個(gè)文本框并標(biāo)記");
18 this.setSize(300, 300);
19 this.setBackground(Color.lightGray);
20 JLabel b1=new JLabel("文本框數(shù)量");
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 }
這個(gè)程序可以運(yùn)行,但是在文本框t1中輸入數(shù)值后;點(diǎn)擊添加,文本框添加不上去,提示有錯(cuò)誤,請大家?guī)兔纯窗。。。。。。。。。?!后天要用啊,先謝謝啦!?。?
1 回答

明月笑刀無情
TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超4個(gè)贊
你在43行的時(shí)候沒有對JTextField進(jìn)行實(shí)例化,比如t[m]=new JTextField();報(bào)錯(cuò)的原因應(yīng)該是這個(gè)
添加回答
舉報(bào)
0/150
提交
取消