我试图在java中创建一个图像查看器。我的程序所做的是它需要来自用户的两个输入,在输入1中,用户输入他想要查看图像的文件夹的路径,并且在输入2中,用户还输入他想要的另一个文件夹的路径。查看图像然后按下运行按钮。在按下运行按钮时,来自用户输入的文件夹的第一图像在图像查看器中彼此平行可见,然后用户可以在按下不同按钮时查看下一图像和先前图像。我上述程序的代码如下所示: -
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class Main extends JFrame {
private JPanel contentPane;
public JTextField textField;
public JTextField textField_1;
public JLabel label;
public JLabel label_1;
int pos = 0;
int pos1 = 0;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 693, 421);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblInput = new JLabel("Input 1");
lblInput.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblInput.setBounds(43, 26, 71, 29);
contentPane.add(lblInput);
textField = new JTextField();
textField.setBounds(124, 26, 312, 29);
contentPane.add(textField);
textField.setColumns(10);
JLabel lblExePath = new JLabel("Input 2");
lblExePath.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblExePath.setBounds(43, 90, 71, 29);
contentPane.add(lblExePath);
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_1.setBounds(124, 90, 312, 29);
contentPane.add(textField_1);
JButton btnRun = new JButton("Run");
btnRun.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
showImage(0);
showImages(0);
}
});
btnRun.setBounds(505, 95, 89, 23);
contentPane.add(btnRun);
label = new JLabel("");
label.setBounds(43, 151, 262, 176);
contentPane.add(label);
label_1 = new JLabel("");
label_1.setBounds(332, 151, 262, 176);
contentPane.add(label_1);
JButton btnPrevious = new JButton("Previous");
btnPrevious.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
pos = pos-1;
if (pos < 0) {
pos = 0;
}
showImage(pos);
pos1 = pos1-1;
if (pos1 < 0) {
pos1 = 0;
}
showImages(pos1);
}
});
btnPrevious.setBounds(124, 349, 89, 23);
contentPane.add(btnPrevious);
JButton btnNext = new JButton("Next");
btnNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
pos = pos+1;
if (pos >= getImage().length) {
pos = getImage().length - 1;
}
showImage(pos);
pos1 = pos1+1;
if (pos1 >= getImages().length) {
pos1 = getImages().length - 1;
}
showImage(pos1);
}
});
btnNext.setBounds(331, 349, 89, 23);
contentPane.add(btnNext);
}
public String[] getImage() {
File file = new File(textField.getText());
String[] imagesList = file.list();
return imagesList;
}
public void showImage(int index) {
String[] imagesList = getImage();
String imageName = imagesList[index];
ImageIcon icon = new ImageIcon(imageName);
Image image = icon.getImage().getScaledInstance(label.getWidth(),
label.getHeight(), Image.SCALE_SMOOTH);
label.setIcon(new ImageIcon(image));
}
public String[] getImages() {
File file = new File(textField_1.getText());
String[] imagesList = file.list();
return imagesList;
}
public void showImages(int index) {
String[] imagesList = getImages();
String imageName = imagesList[index];
ImageIcon icon = new ImageIcon(imageName);
Image image = icon.getImage().getScaledInstance(label_1.getWidth(),
label_1.getHeight(), Image.SCALE_SMOOTH);
label_1.setIcon(new ImageIcon(image));
}
}
现在,我面临的问题是,一切都运转正常,我能够将用户输入的文件夹文件转换为数组在getimage()方法中列出并能够将它们转换为不同的数组,但是我无法在showimage()方法中正确遍历该数组,并且无法使图像显示在屏幕上。如果此代码中还有其他任何问题,请指导,因为我是编程的新手。
答案 0 :(得分:0)
如果您打印正在使用的文件名,您将看到它们是基本文件名,没有目录。这意味着它们是相对文件名,它们是否存在完全取决于Java进程本身的当前目录。
java.io.File是一个过时的类。你应该避免使用它。相反,使用java.nio.file package,它不仅会返回完整的文件路径,还会在您尝试列出文件失败时为您提供有用的信息。替换以下两种情况:
String[] imagesList = file.list();
用这个:
String[] imagesList;
try (DirectoryStream<Path> dir = Files.newDirectoryStream(file.toPath())) {
Collection<String> paths = new ArrayList<>();
for (Path path : dir) {
paths.add(path.toString());
}
imagesList = paths.toArray(new String[0]);
} catch (IOException e) {
throw new RuntimeException("Cannot list files in " + file, e);
}