2017-05-23 74 views
1

我的任務是捕捉鼠標點擊後切換兩個圖像,意味着當任何用戶點擊兩個圖像都應該切換。 但不知何故,在我的代碼無法檢測到鼠標點擊,擺動圖像切換

imageAnimal = createImageIcon("Lion", "Lion"); 
    Image scale = imageAnimal.getImage().getScaledInstance(200,200,Image.SCALE_SMOOTH); 
    imageAnimal = new ImageIcon(scale); 
    image1Label = new JLabel("", imageAnimal, JLabel.CENTER); 

    imageMot = createImageIcon("car", "car"); 
    Image scale = imageMot.getImage().getScaledInstance(200,200,Image.SCALE_SMOOTH); 
    imageMot = new ImageIcon(scale); 
    image1Label = new JLabel("", imageMot, JLabel.CENTER); 

---代碼來捕獲鼠標事件

public void switch() { 
     abstract class MouseListener implements ActionListener { 
     public void actionPerformed(MouseEvent event){ 
      boolean clicked = false; 
      JPanel imageClicked1; 
      JPanel imageClicked2 = (JPanel) event.getSource(); 
      int numClicks = 0; 
       for(int i = 0; i < temp.size(); i++) 
       { 
       if(clicked) 
       { 
        numClicks++; 
        imageClicked1 = (JPanel) event.getSource(); 
         if(numClicks == 2) 
         { 
          switchImages(imageClicked1, imageClicked2); 
         }      
       } 
       MAINpanel.repaint(); 
       MAINpanel.revalidate();     
      } 
     } 

      public void switchImages(JPanel img1, JPanel img2) 
      { 
       //ArrayList<JPanel>sorted = new ArrayList<JPanel>(); 
       JPanel t; 
       JPanel posValue, nextValue; 
       for(int i = 0; i < temp.size(); i++) 
       { 
        for(int k = 1; k < temp.size(); k++) 
        { 
        if(temp.get(i) == img1 && temp.get(k) == img2) 
        { 
         posValue = temp.get(k); 
         nextValue = temp.get(i); 
         t = temp.get(k); 
         posValue = temp.get(i); 
         nextValue = t; 
        }      
        } 
       } 
       for(int i = 0; i < 5; i++) 
      { 
       mainPanel.add(temp.get(i), BorderLayout.CENTER); 
      } 

      }  
     } 
    } 

回答

2
  1. 爲了更好地幫助越早,張貼Minimal, Complete, and Verifiable exampleShort, Self Contained, Correct Example。見下面的例子。
  2. 一個例子得到圖像的方法是熱鏈接到this Q&A中看到的圖像。見下面的例子。
  3. switch方法中聲明並添加鼠標偵聽器似乎沒有意義。但是也許一個MCVE/SSCCE會說清楚。
  4. MAINpanel.repaint();哎呀.. 使用標籤顯示圖像!在交換機上,交換其ImageIcon實例。請看下面的例子。

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.border.EmptyBorder; 
import java.net.*; 

public class ImageSwitch { 

    private JComponent ui = null; 
    JLabel label1 = new JLabel(); 
    JLabel label2 = new JLabel(); 
    ImageIcon imageIconA; 
    ImageIcon imageIconB; 

    ImageSwitch() { 
     try { 
      initUI(); 
     } catch (MalformedURLException ex) { 
      ex.printStackTrace(); 
     } 
    } 

    private void switchImages() { 
     if (label1.getIcon().equals(imageIconA)) { 
      label1.setIcon(imageIconB); 
      label2.setIcon(imageIconA); 
     } else { 
      label1.setIcon(imageIconA); 
      label2.setIcon(imageIconB); 
     } 
    } 

    public void initUI() throws MalformedURLException { 
     if (ui!=null) return; 

     ui = new JPanel(new GridLayout(0,1,2,2)); 
     ui.setBorder(new EmptyBorder(4,4,4,4)); 

     imageIconA = new ImageIcon(new URL("https://i.stack.imgur.com/OVOg3.jpg")); 
     imageIconB = new ImageIcon(new URL("https://i.stack.imgur.com/lxthA.jpg")); 
     label1.setIcon(imageIconA); 
     label2.setIcon(imageIconB); 
     ui.add(label1); 
     ui.add(label2); 

     MouseListener mouseListener = new MouseAdapter() { 

      @Override 
      public void mouseClicked(MouseEvent e) { 
       switchImages(); 
      } 
     }; 
     label1.addMouseListener(mouseListener); 
     label2.addMouseListener(mouseListener); 
    } 

    public JComponent getUI() { 
     return ui; 
    } 

    public static void main(String[] args) { 
     Runnable r = new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (Exception useDefault) { 
       } 
       ImageSwitch o = new ImageSwitch(); 

       JFrame f = new JFrame(o.getClass().getSimpleName()); 
       f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
       f.setLocationByPlatform(true); 

       f.setContentPane(o.getUI()); 
       f.pack(); 
       f.setMinimumSize(f.getSize()); 

       f.setVisible(true); 
      } 
     }; 
     SwingUtilities.invokeLater(r); 
    } 
}