无法使用 for 循环更改当前受影响的 JLabel

Unable to use for loop to change which JLabel is currently being affected

我正在尝试使用 forloop 来更改哪个 jlabel 受到影响,因为它们都执行相同的功能,并且除了每个名称末尾的数值之外具有相同的名称。

基本上,我使用拖放传输处理程序来创建一个食物网页游戏。我想将游戏随机分配给可能出现的三个不同的食物网。随机化很好,但我不知道如何使用循环来确定正在设置哪个 JLabel 的图标。由于我通常为每个 JLabel 使用相同的名称,每个 JLabel 都有一个图标,该图标会根据随机选择的主题食物网而改变,我想知道如何改变 JLabel 正在使用的循环选择,至于我尝试执行 "selection(i+1)".

时出现语法错误
private static int randomNumber(){
    return(int) (Math.random() * (3 - 1 + 1) + 1);
}

private void generateQuiz(){
    switch(randomNumber()){
        case 1: //the for loop changes each available selection based on each element in the foor loop.
            for(int i = 0; i < consumers1.size(); i++){
                selection(i+1).setIcon(new ImageIcon(getClass().getResource(
                        "/resources/quiz/"+consumers1.get(i)+".png")));
            }
            break;

        case 2:
            break;

        case 3:
            break;

想要:使用 for 循环自动神奇地更改每个选择框,而不是为每个案例手动输入每个单独的内容

编辑:这是围绕这个问题的其余一般代码,

private String[] producers = {"grass", "plankton", "berries"};
private ArrayList<String> consumers1 = new ArrayList();
private ArrayList<String> consumers2 = new ArrayList();
private ArrayList<String> consumers3 = new ArrayList();

//mouselistener to handle all image move-ability
MouseListener mouseListener = new MouseListener() { 

    @Override public void mouseClicked(MouseEvent e) {} 

    //a mouselistener for that each img has the ability to be moved into spots
    @Override
    public void mousePressed(MouseEvent e) { 
        JComponent jc = (JComponent)e.getSource();
        TransferHandler th = jc.getTransferHandler();
        th.exportAsDrag(jc, e, TransferHandler.COPY);

       // System.out.println(producers1.getIcon());
    }

    @Override public void mouseReleased(MouseEvent e) {}
    @Override public void mouseEntered(MouseEvent e) {}
    @Override public void mouseExited(MouseEvent e) {}
};

/**
 * Creates new form Quiz
 */
public Quiz() {
    initComponents();
    //sets the jframe to the center of the screen
    setLocationRelativeTo(null);
    //changes how the jframe closes
    addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent windowEvent) {
                //confirm dialog to ensure user wants to close, if not return
                    int confirm = JOptionPane.showConfirmDialog(null, 
                        "A quiz is currently inprogress. "
                        + "\nAll unsubmitted quizs will not be saved!"
                        + "\nDo you wish to exit?",
                        "Exit", 
                        JOptionPane.YES_NO_OPTION);
                    if (confirm == JOptionPane.YES_OPTION){
                        System.exit(0);
                    }
            }
    });

    //random algorithm; t1 = x, tn = (tn-1) + xn

    //<--- RANDOM QUIZ GENERATION --->
    //add all the image names to array lists
    Collections.addAll(consumers1, "goat", "rabbit", "jackal", "wildcat",
        "lion");
    Collections.addAll(consumers2, "fish", "mussel", "bird", "octopus",
            "human");
    Collections.addAll(consumers3, "butterfly", "grasshopper", "frog",
            "spider", "snake");
    generateQuiz();
    // end of quiz generaiton

    //<--- DRAG AND DROP ADDITIONS LISTENERS AND TRANSFER HANDLERS --->
    //adds mouse listener to be able to drag and drop the imgs in the seleciton boxes
    selection1.addMouseListener(mouseListener);
    selection2.addMouseListener(mouseListener);
    selection3.addMouseListener(mouseListener);
    selection4.addMouseListener(mouseListener);
    selection5.addMouseListener(mouseListener);
    selection6.addMouseListener(mouseListener);

   //creates transfer handlers to bea ble to drag and drop the images in the frame
    TransferHandler th = new TransferHandler("icon");
    selection1.setTransferHandler(th);
    selection2.setTransferHandler(th);
    selection3.setTransferHandler(th);
    selection4.setTransferHandler(th);
    selection5.setTransferHandler(th);
    selection6.setTransferHandler(th);

    producer.setTransferHandler(th);
    consumer1.setTransferHandler(th);
    consumer2.setTransferHandler(th);
    consumer3.setTransferHandler(th);
    consumer4.setTransferHandler(th);
    consumer5.setTransferHandler(th);
   //<--- END OF DRAG AND DROP ADDITIONS LISTENERS AND TRANSFER HANDLERS --->
}

private static int randomNumber(){
    return(int) (Math.random() * (3 - 1 + 1) + 1);
}

private void generateQuiz(){
    switch(randomNumber()){
        case 1:
            for(int i = 0; i < consumers1.size(); i++){
                selection(i+1).setIcon(new ImageIcon(getClass().getResource(
                        "/resources/quiz/"+consumers1.get(i)+".png")));
            }
            break;

        case 2:
            break;

        case 3:
            break;
    }
}

P.S.: 它是在 netbeans gui builder 中为学校作业制作的,所以它在 neatbeans

自动生成的代码中声明

啊,"selection..."简直就是一个变量名

编译Java程序时,大部分变量名都被指向内存位置的指针所取代,所以你不能在运行时assemble一个字符串并调用相应的多变的。 (你可以让它们 public 并使用反射,但在这里绝对是错误的)。

最简单的方法如下:

JLabel[] selections={
    selection1,
    selection2,
    selection3,
    selection4,
    selection5,
    selection6
};

然后使用 selections[i] 而不是 selection(i+1)