2014-02-17 36 views
0

我在佈局中添加此JTextFields時遇到問題。唯一會出現的JTextField只是1而不是18左右。我的計劃是在我的佈局上有20個JTextFields。有了這20個文本字段,它們將具有隨機的x + y值,並且還具有佈局上的隨機位置。這是我的代碼:在for循環中添加JTextField的數組,它自身添加到佈局

import java.util.Random; 
import javax.swing.JFrame; 
import javax.swing.JTextField; 

public class JtextArray extends JFrame 
{  
    JTextField[] allField = new JTextField [20]; 

    Random rand = new Random(); 
    int x = rand.nextInt(100);int y = rand.nextInt(100); 
    int xpost = rand.nextInt(300); int ypost = rand.nextInt(150); 

    JtextArray(){ 
    super("dsd"); 
    setLayout(null); 
    for(int x = 0;x<=18;x++){ 
     System.out.println(x); 
     allField[x] = new JTextField(String.format("  %s + %s", x , y)); 
     allField[x].setBounds(xpost, ypost, 100, 30); 
     add(allField[x]); 
    } 
    }} 

我主要

import javax.swing.JFrame; 

public class ArraySample extends JFrame{ 
    public static void main(String[] args){ 
     JtextArray object = new JtextArray(); 
     object.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     object.setSize(400,400); 
     object.setVisible(true); 

    } 
} 

回答

2

你的文本框具有相同的座標!

在for循環中使用rand.nextInt

+0

謝謝!!!陷入這個問題已經有3個小時了。 :)) –