简体   繁体   中英

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

Hello guys I'll do my best to explain this because it's very hard to explain. I'm working on project that requires the user to select which animal image they want to appear several different times. But I can only do it the once on the same JLabel (ImageBlock). Here is my code so you understand.

Main class Animals

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);
}

Sub class GUI

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;

    }
}

After I run the code then press the button I am prompted the input dialogue, if I enter "1" for bear, I get this, which is exactly what I want!

[动物项目 ]

But after that is the problem, once I click the button again it won't go to the next imageBlock, it will only overwrite the same image where the Bear already is. This is obviously because I haven't put the code in, but I don't know how to do it and I'm struggling on how to move to the next image block.

Would it require Multithreading? I want to expand this project with different animals and more imageBlock JLabels but I'm starting smaller so I can then expand once I know how to progress. Could anyone please help me I've been stuck on this problem for days and I can't work out how to expand on it, I would greatly appreciate any help and tips.

Forget my old post--I misunderstood what you were asking. I think I get it now. I think what you want is just a variable to store what image block to change. Try this:

Put this up at the top of your class with your other instance variables:

private JLabel currImageBlock = null;

And then change your actionPerformed() method to this

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);
        }
    }
}

This is a little messy and could probably be made a little more elegant, possibly by using an array of JLabel image blocks instead, but it does what you're asking (I think.) It will cycle through putting the image in every image block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM