0

How can I change between the pictures with help of clicking the button. I made a x variable because then I could make different if-statements but that didn't work for me, probably because I did something wrong... Here is my code so far:

public class Main extends JFrame{

    private JButton changePic;
    private JPanel panel;
    private JLabel pic1;
    private JLabel pic2;
    private JLabel pic3;
    private JLabel picture;
    private int x = 0;

    public Main(){

        panel = new JPanel();
        add(panel);

        changePic = new JButton("Change Button");
        panel.add(changePic);


        pic1 = new JLabel(new ImageIcon("pic1.png"));
        pic2 = new JLabel(new ImageIcon("pic.png"));
        pic3 = new JLabel(new ImageIcon("pic3.png"));

        panel.add(pic1);
        panel.add(pic2);
        panel.add(pic3);

        changePic.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                if(e.getSource() == changePic){

                }
            }
        });
        getContentPane().setBackground(Color.white);
        setSize(300, 300);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

    }

    public static void main(String[] args){
        Main fr = new Main();
    }
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
Magnus. B
  • 101
  • 1
  • 3
  • 1
    "but that didn't work for me" could you clarify "didn't work"? What exactly you expect your code to do, what makes you think that, and what happens instead? – Pshemo Aug 05 '17 at 16:41
  • when i pressed the button it didn't swaped picture. The only Image i see is the first(pic1) changePic.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ if(e.getSource() == changePic){ if(x == 0){ panel.add(pic1); }else if(x == 1){ panel.add(pic2); }else if(x == 2){ panel.add(pic3); } x++; if(x > 2) x = 0; } } }); – Magnus. B Aug 05 '17 at 16:47
  • 2
    Use [edit] option to update your question with problem description and your code (since it is unreadable in comment section). – Pshemo Aug 05 '17 at 16:49
  • Anyway maybe this will interest you [How to change icon of a JLabel?](https://stackoverflow.com/questions/1567445/how-to-change-icon-of-a-jlabel) – Pshemo Aug 05 '17 at 16:51
  • 1) See [this answer](https://stackoverflow.com/a/16040330/418556) for an example of using a `JButton` to start & stop a Swing `Timer` that displays an array of images (in a `JLabel`) in a loop. It will give you many parts of the answer that is needed. E.G. change the `x` to a `counter` then use it as an index to an array. 2) Always [edit] the question with new code. It is almost unreadable in a comment. – Andrew Thompson Aug 05 '17 at 17:06
  • Depending on what you're trying to do, a `CardLayout` can help you to swap between components with set images. – user1803551 Aug 05 '17 at 17:46

1 Answers1

1
public class Main extends JFrame{

    private JButton changePic;
    private JPanel panel;

    private JLabel pic1;
    private int x = 0;

    public Main(){

        panel = new JPanel();
        add(panel);

        changePic = new JButton("Change Button");
        panel.add(changePic);


        pic1 = new JLabel();
        panel.add(pic1);
        ImageIcon icon1 = new ImageIcon("pic1.gif");
        ImageIcon icon2 = new ImageIcon("pic2.gif");
        ImageIcon icon3 = new ImageIcon("pic3.gif");

        changePic.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e){
                if(e.getSource() == changePic){
                    if(x == 0){
                        pic1.setIcon(icon1);
                    }else if(x == 1){
                        pic1.setIcon(icon2);
                    }else if(x == 2){
                        pic1.setIcon(icon3);
                    }
                    Main.this.pack();
                    x++;
                    if(x > 2) x = 0;

                }
            }
        });
        getContentPane().setBackground(Color.white);
        setSize(300, 300);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

    }

    public static void main(String[] args){
        Main fr = new Main();
    }
}
  • you don't need multiple JLabels, use 3 ImageIcons instead.
  • you need to call JFrame's pack() every time you had UI Changes (in this case, changing image)
didxga
  • 5,935
  • 4
  • 43
  • 58