如何将来自用户输入的图像多次提示到多个不同的 JLabel JAVA

How do I prompt an image from a user input more than once onto several different JLabels JAVA

大家好,我会尽力解释这个,因为它很难解释。我正在做的项目要求用户 select 他们想要在不同时间出现的动物图像。但我只能在同一个 JLabel (ImageBlock) 上做一次。这是我的代码,所以你明白了。

主要class动物

public static void main(String[] args) {
    JFrame application = new JFrame("Animal Project");

    GUI graphicalInterface = new GUI();
    application.add(graphicalInterface);

    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    application.setLocation(200, 200);
    application.pack();
    application.setVisible(true);
    application.setResizable(false);
}

子classGUI

public class GUI extends JPanel implements ActionListener {

    private JButton animalOption = new JButton();
    private JPanel buttonPanel;
    private JPanel imagePanel;
    private ImageIcon bear;
    private ImageIcon tiger;
    private ImageIcon lion;
    private JLabel imageBlock1;
    private JLabel imageBlock2;
    private JLabel imageBlock3;

    GUI() {
        Border blackline = BorderFactory.createLineBorder(Color.black);

        //create button panel
        buttonPanel = new JPanel();
        buttonPanel.setPreferredSize(new Dimension(100, 70));
        buttonPanel.setOpaque(true);
        buttonPanel.setBackground(Color.white);
        buttonPanel.setBorder(blackline);
        imagePanel = new JPanel(new GridLayout(3, 3));
        imagePanel.setPreferredSize(new Dimension(500, 500));
        imagePanel.setOpaque(true);
        imagePanel.setBackground(Color.white);
        imagePanel.setBorder(blackline);
        imageBlock1 = new JLabel();
        imageBlock1.setPreferredSize(new Dimension(100, 100));
        imageBlock1.setOpaque(true);
        imageBlock1.setBackground(Color.white);
        imageBlock1.setBorder(blackline);
        imageBlock2 = new JLabel();
        imageBlock2.setPreferredSize(new Dimension(100, 100));
        imageBlock2.setOpaque(true);
        imageBlock2.setBackground(Color.white);
        imageBlock2.setBorder(blackline);
        imageBlock3 = new JLabel();
        imageBlock3.setPreferredSize(new Dimension(100, 100));
        imageBlock3.setOpaque(true);
        imageBlock3.setBackground(Color.white);
        imageBlock3.setBorder(blackline);

        bear = new ImageIcon("Bear.png");
        tiger = new ImageIcon("Tiger.png");
        lion = new ImageIcon("Lion.png");

        animalOption = new JButton();
        //add action listener to each button
        animalOption.addActionListener(this);
        //set button size
        animalOption.setPreferredSize(new Dimension(100, 50));
        //set text for each button
        animalOption.setText("Animal");
        animalOption.setToolTipText("press to select your animal");
        //add buttons to gui
        buttonPanel.add(animalOption);

        this.add(buttonPanel);
        this.add(imagePanel);
        imagePanel.add(imageBlock1);
        imagePanel.add(imageBlock2);
        imagePanel.add(imageBlock3);

    }

    public void actionPerformed(ActionEvent e) {
        int choice;

        if (e.getSource().equals(animalOption)) { //add DVD Button
            choice = selectAnimal();

            if (choice == 1) {
                imageBlock1.setIcon(bear);
            } else if (choice == 2) {
                imageBlock1.setIcon(tiger);
            } else if (choice == 3) {
                imageBlock1.setIcon(lion);
            }
        }

    }

    static int selectAnimal() {

        int animal = 0;
        String DVDYears = JOptionPane.showInputDialog("Please enter animal, type 1 for bear, type 2 for tiger, type 3 for lion");
        animal = Integer.parseInt(DVDYears);

        return animal;

    }
}

输入 运行 代码后按下按钮,系统会提示我输入对话框,如果我输入“1”代表熊,我会得到这个,这正是我想要的!

]

但之后就是问题了,一旦我再次点击按钮,它不会转到下一个图像块,它只会覆盖熊已经存在的相同图像。这显然是因为我没有把代码放进去,但我不知道该怎么做,我正在纠结如何移动到下一个图像块。

是否需要多线程?我想用不同的动物和更多的 imageBlock JLabel 来扩展这个项目,但我开始的时候比较小,这样我就可以在知道如何取得进展后进行扩展。谁能帮我解决这个问题好几天了,我不知道如何扩展它,我将不胜感激任何帮助和提示。

忘记我的旧post--我误解了你的意思。我想我现在明白了。我想你想要的只是一个变量来存储要更改的图像块。试试这个:

将其与其他实例变量一起放在 class 的顶部:

private JLabel currImageBlock = null;

然后将您的 actionPerformed() 方法更改为此

public void actionPerformed(ActionEvent e) {
    int choice;

    if (currImageBlock == null) {
        currImageBlock = imageBlock1;
    } else if (currImageBlock == imageBlock1) {
        currImageBlock = imageBlock2;
    } else if (currImageBlock == imageBlock2) {
        currImageBlock = imageBlock3;
    } else if (currImageBlock == imageBlock3) {
        currImageBlock = imageBlock1;
    } 

    if (e.getSource().equals(animalOption)) { //add DVD Button
        choice = selectAnimal();

        if (choice == 1) {
            currImageBlock.setIcon(bear);
        } else if (choice == 2) {
            currImageBlock.setIcon(tiger);
        } else if (choice == 3) {
            currImageBlock.setIcon(lion);
        }
    }
}

这有点乱,可能会变得更优雅一些,可能是通过使用 JLabel 图像块数组来代替,但它可以满足您的要求(我认为)。循环将图像放入每个图像块。