我正在构建一个PhotoViewer。我希望能够获得在JLabel上显示的ImageIcon的路径。因此我创建了一个MouseListener和一个FileChooser。我花了很多时间试图解决这个问题。
for (int i=0; i< scaled.size(); i++){
labels.add(i, new JLabel(new ImageIcon(scaled.get(i))));
}
在这里,我希望给每个标签创建一个名称i,所以我可以稍后getName()并询问特定的标签,它包含哪个ImageIcon及其路径。
我有三个ArrayLists:
ArrayList<JLabel> labels = new ArrayList<JLabel>();
ArrayList<ImageIcon> AL = new ArrayList<ImageIcon>();
ArrayList<Image> scaled = new ArrayList<Image>();
现在,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());
}
之后,使用所选图片的Path的Constructot参数创建Frame。我将在一个单独的框架中显示它。
完整代码:
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
}
}
答案 0 :(得分:3)
有多种可能的方法,其中没有一种涉及:
从ImageIcon获取路径..
一种方式:
File[] f = chooser.getSelectedFiles();
将File[] f
声明为该类的属性,以便以后可以访问。单击,找到源组件,获取图像图标,并从中获取图像。
JLabel label = (JLabel)mouseEvent.getSource();
ImageIcon imageIcon = (ImageIcon)label.getIcon();
Image img = imageIcon.getImage();
然后:
int index = scaled.get(img);
&安培;该文件是:
File file = f[index];