有关动态变量的问题
在JFrame里根据一个值count,new出count个JTextField和JTextArea,用jtextf和jtexta两个数组存放count个组件,在for循环里new出组件,为什么运行后JTextField和JTextArea画不出来?求解~~
public class MassageMa extends JFrame { public MassageMa(int count) { this.setBounds(300, 100, 800, 600); this.setLayout(null); this.setVisible(true); JTextField[] jtextf = new JTextField[count]; JTextArea[] jtexta = new JTextArea[count]; for(int i=1;i<=count;i++){ if(i%2!=0){ jtextf[i-1] = new JTextField("嘿嘿"); jtextf[i-1].setBounds(50, 60+70*(i/2), 70, 30); jtextf[i-1].setEditable(false); this.add(jtextf[i-1]); jtexta[i-1] = new JTextArea(); jtexta[i-1].setBounds(145, 60+70*(i/2), 170, 30); this.add(jtexta[i-1]); } if(i%2==0){ jtextf[i-1] = new JTextField("嘿嘿"); jtextf[i-1].setBounds(385, 60+70*(i/2-1), 70, 30); jtextf[i-1].setEditable(false); this.add(jtextf[i-1]); jtexta[i-1] = new JTextArea(); jtexta[i-1].setBounds(480, 60+70*(i/2-1), 170, 30); this.add(jtexta[i-1]); } } }}
public class MessageMa extends JFrame { public MessageMa(int count) { this.setBounds(300, 100, 800, 600); this.setLayout(null); this.setTitle("HaHa"); JTextField[] jtextf = new JTextField[count]; JTextArea[] jtexta = new JTextArea[count]; for (int i = 0; i < count; i++) { if (i % 2 == 0) { jtextf[i] = new JTextField("嘿嘿"); jtextf[i].setBounds(50, 60 + 70 * ((i + 1) / 2), 70, 30); jtextf[i].setEditable(false); this.add(jtextf[i]); jtexta[i] = new JTextArea(); jtexta[i].setBounds(145, 60 + 70 * ((i + 1) / 2), 170, 30); this.add(jtexta[i]); } else { jtextf[i] = new JTextField("嘿嘿"); jtextf[i].setBounds(385, 60 + 70 * ((i + 1) / 2 - 1), 70, 30); jtextf[i].setEditable(false); this.add(jtextf[i]); jtexta[i - 1] = new JTextArea(); jtexta[i - 1].setBounds(480, 60 + 70 * ((i + 1) / 2 - 1), 170, 30); this.add(jtexta[i - 1]); } } this.setVisible(true); } public static void main(String[] args) { new MessageMa(10); }}