I am building a PhotoViewer. I want to be able to get the path of an ImageIcon which is displayed on a JLabel. Therefore I have created an MouseListener and a FileChooser. I have spent many hours trying to solve this issue.
我正在构建一个PhotoViewer。我希望能够获得在JLabel上显示的ImageIcon的路径。因此我创建了一个MouseListener和一个FileChooser。我花了很多时间试图解决这个问题。
for (int i=0; i< scaled.size(); i++){
labels.add(i, new JLabel(new ImageIcon(scaled.get(i))));
}
Here I want the give each Label which is beeing created a name i, so I can getName() later and ask the specific Label which ImageIcon it contains and its path.
在这里,我希望给每个标签创建一个名称i,所以我可以稍后getName()并询问特定的标签,它包含哪个ImageIcon及其路径。
I have three ArrayLists:
我有三个ArrayLists:
ArrayList<JLabel> labels = new ArrayList<JLabel>();
ArrayList<ImageIcon> AL = new ArrayList<ImageIcon>();
ArrayList<Image> scaled = new ArrayList<Image>();
Now the mouseClicked event should give me a Path to the ImageIcon on the Label which has been clicked.
现在,mouseClicked事件应该给我一个指向已被点击的Label上的ImageIcon的路径。
public void mouseClicked(MouseEvent arg0) {
System.out.println(arg0.getSource().getName());
int a = arg0.getSource().getName();
getImageIcon.labels.get(a);
CreateFrame(arg0.getSource().getDescription());
}
After that a Frame is created with the Constructot parameter of the Path of the choosen picture. And I will display it in a seperate Frame.
之后,使用所选图片的路径的Constructot参数创建Frame。我将在一个单独的框架中显示它。
Full Code:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Label;
import java.awt.List;
import java.awt.event.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.*;
public class ImageViewer {
public static void main(String[] args) {
JFrame frame = new ImageViewerFrame();
frame.setTitle("Photoviewer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
@SuppressWarnings("serial")
class ImageViewerFrame extends JFrame implements MouseListener{
JLabel label;
JFileChooser chooser;
JMenuBar menubar;
JMenu menu;
JMenuItem menuitem;
JPanel panel = new JPanel();
public ArrayList<File> images = new ArrayList <File>();
public void method1(){
JLabel test = labels.get(0);
System.out.println(test.getName());
}
ArrayList<JLabel> labels = new ArrayList<JLabel>();
ArrayList<ImageIcon> AL = new ArrayList<ImageIcon>();
ArrayList<Image> scaled = new ArrayList<Image>();
public ImageViewerFrame() {
setSize(500,500);
panel.setLayout(new GridLayout(0,5));
label = new JLabel();
add(label);
add(panel);
JButton test = new JButton ("TEST");
test.addMouseListener(this);
panel.add(test);
panel.setVisible(true);
chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setMultiSelectionEnabled(true);
menubar = new JMenuBar();
setJMenuBar(menubar);
menu = new JMenu("File");
menubar.add(menu);
menuitem = new JMenuItem("Open");
menu.add(menuitem);
menuitem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event){
int result = chooser.showOpenDialog(null);
if(result == JFileChooser.APPROVE_OPTION) {
File[] f = chooser.getSelectedFiles();
for(int i=0; i< f.length; i++)
{
images.add(f[i]);
ImageIcon imageicon = new ImageIcon(f[i].toString());
AL.add(imageicon);
}
for (ImageIcon x : AL){
Image image = x.getImage();
Image newimg = image.getScaledInstance(120,120, java.awt.Image.SCALE_SMOOTH);
scaled.add(newimg);
}
for (int i=0; i< scaled.size(); i++){
labels.add(i, new JLabel(new ImageIcon(scaled.get(i))));
}
for (JLabel x : labels){
x.addMouseListener(ImageViewerFrame.this);
}
for (int i=0; i< scaled.size(); i++){
//panel.add(new JLabel(i), (new ImageIcon (scaled.get(i))));
panel.add(labels.get(i));
}
}
}
});
}
@Override
public void mouseClicked(MouseEvent arg0) {
System.out.println(arg0.getSource().getName());
int a = arg0.getSource().getName();
getImageIcon.labels.get(a);
CreateFrame(arg0.getSource().getDescription());
}
private void CreateFrame() {
JFrame frame2 = new JFrame("Test");
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel3 = new JPanel();
JButton next = new JButton("Next");
JButton previous = new JButton ("Previous");
JButton diashow = new JButton ("Diashow");
panel3.add(next);
panel3.add(previous);
panel3.add(diashow);
panel3.setVisible(true);
frame2.setVisible(true);
frame2.add(panel3);
}
private void CreateFrame(String s) {
JFrame frame2 = new JFrame("Test");
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel3 = new JPanel();
JButton next = new JButton("Next");
JButton previous = new JButton ("Previous");
JButton diashow = new JButton ("Diashow");
panel3.add(next);
panel3.add(previous);
panel3.add(diashow);
panel3.setVisible(true);
frame2.setVisible(true);
frame2.add(panel3);
panel3.add(new JLabel(s));
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
1 个解决方案
#1
3
There are multiple possible approaches to this, none of which involve:
有多种可能的方法,其中没有一个涉及:
Getting a Path from an ImageIcon..
从ImageIcon获取路径..
请参阅什么是XY问题?
One way:
File[] f = chooser.getSelectedFiles();
Declare File[] f
as an attribute of the class, so it is accessible later. On click, find the source component, get the image icon, and get the image from it.
将File [] f声明为类的属性,以便以后可以访问。单击,找到源组件,获取图像图标,然后从中获取图像。
JLabel label = (JLabel)mouseEvent.getSource();
ImageIcon imageIcon = (ImageIcon)label.getIcon();
Image img = imageIcon.getImage();
Then:
int index = scaled.get(img);
& the file is:
&文件是:
File file = f[index];
#1
3
There are multiple possible approaches to this, none of which involve:
有多种可能的方法,其中没有一个涉及:
Getting a Path from an ImageIcon..
从ImageIcon获取路径..
请参阅什么是XY问题?
One way:
File[] f = chooser.getSelectedFiles();
Declare File[] f
as an attribute of the class, so it is accessible later. On click, find the source component, get the image icon, and get the image from it.
将File [] f声明为类的属性,以便以后可以访问。单击,找到源组件,获取图像图标,然后从中获取图像。
JLabel label = (JLabel)mouseEvent.getSource();
ImageIcon imageIcon = (ImageIcon)label.getIcon();
Image img = imageIcon.getImage();
Then:
int index = scaled.get(img);
& the file is:
&文件是:
File file = f[index];