2017-08-05 3 views
0

Comment puis-je changer entre les images en cliquant sur le bouton. J'ai fait une variable x parce que je pouvais faire autrement si-déclarations, mais cela ne fonctionne pas pour moi, probablement parce que je l'ai fait quelque chose de mal ... Voici mon code à ce jour:Comment changer les images avec un JButton

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(); 
    } 
} 
+1

« mais cela ne fonctionne pas pour moi » pourriez-vous préciser « ne fonctionnait pas » ? Qu'est-ce que vous attendez exactement de votre code, qu'est-ce qui vous fait penser cela, et que se passe-t-il à la place? – Pshemo

+0

quand j'ai appuyé sur le bouton il n'a pas changé d'image. La seule image que je vois est le premier (pic1) \t changePic.addActionListener (new ActionListener() { \t \t public void actionPerformed \t (ActionEvent e) { \t \t \t \t if (e.getSource() == changePic) { \t \t \t \t \t if (x == 0) { \t \t \t \t \t \t panel.add (pic1); \t \t \t \t \t} else if (x == 1) { \t \t \t \t \t \t panel.add (pic2); \t \t \t \t \t} else if (x == 2) { \t \t \t \t \t \t panel.add (pic3); \t \t \t \t \t} \t \t \t \t \t x ++; \t \t \t \t \t si (x> 2) \t \t \t \t \t \t x = 0; \t \t \t \t} \t \t \t} \t \t}); –

+2

Utilisez l'option [Modifier] pour mettre à jour votre question avec la description du problème et votre code (puisqu'il est illisible dans la section des commentaires). – Pshemo

Répondre

0
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(); 
    } 
} 
  • vous n'avez pas besoin de plusieurs JLabel s, utilisez plutôt 3 ImageIcon s.
  • vous devez appeler l » pack()JFrame chaque fois que vous aviez changements UI (dans ce cas , changement d'image)