How to find JLabel that was clicked and show ImageIcon from it?

风格不统一 提交于 2019-12-12 07:28:49

问题


Here is my code. I want to know which l was clicked and then in a new frame, display that ImageIcon.

The e.getSource() is not working...

final JFrame shirts = new JFrame("T-shirts");
JPanel panel = new JPanel(new GridLayout(4, 4, 3, 3));

for (int i = 1; i < 13; i++) {
    l = new JLabel(new ImageIcon("T-shirts/"+i+".jpg"), JLabel.CENTER);
    l.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
    l.setFont(l.getFont().deriveFont(20f));
    panel.add(l);
}//end of for loop

panel.addMouseListener(new MouseAdapter(){  
    public void mouseClicked(MouseEvent e)  
    {
        sizes = new JFrame("Shopping");
        sizes.setVisible(true);
        sizes.setSize(500, 500);
        sizes.setLocation(100,200);
        shirts.dispose();
        if(e.getSource()==l){//FIX
            sizes.add(l);
        }//end of if
    }  
});

shirts.setContentPane(panel);
shirts.setSize(1000, 1000);
shirts.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
shirts.setVisible(true);

回答1:


If you add your MouseListener directly to your JLabels, then you can display the pressed label's icon easily in a JOptionPane:

@Override
public void mousePressed(MouseEvent mEvt) {
   JLabel label = (JLabel) mEvt.getSource();
   Icon icon = label.getIcon();
   JOptionPane.showMessageDialog(label, icon);
}

For example:

import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.*;

public class FooMouseListener extends JPanel {
   private GetImages getImages;

   public FooMouseListener() throws IOException {
      getImages = new GetImages();
      setLayout(new GridLayout(GetImages.SPRITE_ROWS, GetImages.SPRITE_COLS));
      MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
      for (int i = 0; i < GetImages.SPRITE_CELLS; i++) {
         JLabel label = new JLabel(getImages.getIcon(i));
         add(label);
         label.addMouseListener(myMouseAdapter);
      }
   }

   private class MyMouseAdapter extends MouseAdapter {
      @Override
      public void mousePressed(MouseEvent e) {
         JLabel label = (JLabel) e.getSource();
         Icon icon = label.getIcon();
         JOptionPane.showMessageDialog(label, icon, "Selected Icon", JOptionPane.PLAIN_MESSAGE);
      }
   }

   private static void createAndShowGui() {
      FooMouseListener mainPanel = null;
      try {
         mainPanel = new FooMouseListener();
      } catch (IOException e) {
         e.printStackTrace();
         System.exit(-1);
      }

      JFrame frame = new JFrame("FooMouseListener");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class GetImages {
   private static final String SPRITE_PATH = "http://th02.deviantart.net/"
         + "fs70/PRE/i/2011/169/0/8/blue_player_sprite_sheet_by_resetado-d3j7zba.png";
   public static final int SPRITE_ROWS = 6;
   public static final int SPRITE_COLS = 6;
   public static final int SPRITE_CELLS = SPRITE_COLS * SPRITE_ROWS;

   private List<Icon> iconList = new ArrayList<>();

   public GetImages() throws IOException {
      URL imgUrl = new URL(SPRITE_PATH);
      BufferedImage mainImage = ImageIO.read(imgUrl);

      for (int i = 0; i < SPRITE_CELLS; i++) {
         int row = i / SPRITE_COLS;
         int col = i % SPRITE_COLS;
         int x = (int) (((double) mainImage.getWidth() * col) / SPRITE_COLS);
         int y = (int) ((double) (mainImage.getHeight() * row) / SPRITE_ROWS);
         int w = (int) ((double) mainImage.getWidth() / SPRITE_COLS);
         int h = (int) ((double) mainImage.getHeight() / SPRITE_ROWS);
         BufferedImage img = mainImage.getSubimage(x, y, w, h);
         ImageIcon icon = new ImageIcon(img);
         iconList.add(icon);
      }
   }

   // get the Icon from the List at index position
   public Icon getIcon(int index) {
      if (index < 0 || index >= iconList.size()) {
         throw new ArrayIndexOutOfBoundsException(index);
      }

      return iconList.get(index);
   }

   public int getIconListSize() {
      return iconList.size();
   }

}



回答2:


Have you tried this?

public void mouseClicked(MouseEvent e)  
{

       sizes = new JFrame("Shopping");
       sizes.add(l);
       sizes.setVisible(true);
       sizes.setSize(500, 500);
       sizes.setLocation(100,200);
       shirts.dispose();

//Remove the "e.getSource()" part.


}  

It will automatically display the image, because you are assigning the Image Name to it, in the same segment as the Addition to the new JFrame.

Let me know of the outcome



来源:https://stackoverflow.com/questions/29735016/how-to-find-jlabel-that-was-clicked-and-show-imageicon-from-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!