Java-在for循环中随机将数组中的字符串分配给JButton。每个元素一次

时间:2017-09-11 20:50:34

标签: arrays string random jbutton unique

public static void main(String[] args){
     String[] word = {" The ", " itsy " , " bitsy ", " spider ", " went " , " up " , 
                      " the ", " water ", " spout. ", " Down ", " came ", " the " , 
                      " rain ", " and ", " flushed ", " the ", " spider ", " out. "};
     Random random = new Random();

     JFrame frame = new JFrame();
     frame.setLayout(new BorderLayout());
     frame.setTitle("JavaGame");
     frame.setSize(600,600);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     JPanel northPanel= new JPanel(new GridLayout(3,6));
     for (int i=0; i<18; i++) {
         JButton button = new JButton();
         northPanel.add(button);
         button.setText(word[random.nextInt(word.length)]);
     }
}

我正在寻找插件以确保每个元素只填充一次。

0 个答案:

没有答案