您好,我正在尝试使用上一个按钮和下一个按钮来使用幻灯片图像,我无法做到这一点,我执行了以下代码来实现我的输出,但是当我运行我的程序并尝试幻灯片图像时,它显示图像不可用我。我不明白为什么会这样。我从 一个网站
public class ImageSlider extends JPanel implements ActionListener {
private static final int MAX = 20;
private static final Font sans = new Font("SansSerif", Font.PLAIN, 16);
private static final Border border =
BorderFactory.createMatteBorder(4, 16, 4, 16, Color.lightGray);
private List<String> list = new ArrayList<String>(MAX);
private List<ImageIcon> cache = new ArrayList<ImageIcon>(MAX);
private JLabel imageLabel = new JLabel();
private JButton prevButton = new JButton();
private JButton nextButton = new JButton();
private JComboBox favorites;
public ImageSlider() {
this.setLayout(new BorderLayout());
list.add("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\yellow.png");
list.add("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\a0.png");
list.add("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\yellow.png");
list.add("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\a0.png");
for (int i = 0; i < list.size(); i++) cache.add(i, null);
JLabel titleLabel = new JLabel();
titleLabel.setText("ImageSlider");
titleLabel.setHorizontalAlignment(JLabel.CENTER);
titleLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 24));
titleLabel.setBorder(border);
this.add(titleLabel, BorderLayout.NORTH);
imageLabel.setIcon(getImage(0));
imageLabel.setHorizontalAlignment(JLabel.CENTER);
imageLabel.setBorder(border);
this.add(imageLabel, BorderLayout.CENTER);
favorites = new JComboBox(
list.toArray(new String[list.size()]));
favorites.setActionCommand("favs");
favorites.addActionListener(this);
prevButton.setText("\u22b2Prev");
prevButton.setFont(sans);
prevButton.setActionCommand("prev");
prevButton.addActionListener(this);
nextButton.setText("Next\u22b3");
nextButton.setFont(sans);
nextButton.setActionCommand("next");
nextButton.addActionListener(this);
JPanel controlPanel = new JPanel();
controlPanel.add(prevButton);
controlPanel.add(favorites);
controlPanel.add(nextButton);
controlPanel.setBorder(border);
this.add(controlPanel, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent ae) {
String cmd = ae.getActionCommand();
if ("favs".equals(cmd)) {
int index = favorites.getSelectedIndex();
ImageIcon image = getImage(index);
imageLabel.setIcon(image);
if (image != null) imageLabel.setText("");
else imageLabel.setText("Image not available.");
}
if ("prev".equals(cmd)) {
int index = favorites.getSelectedIndex() - 1;
if (index < 0) index = list.size() - 1;
favorites.setSelectedIndex(index);
}
if ("next".equals(cmd)) {
int index = favorites.getSelectedIndex() + 1;
if (index > list.size() - 1) index = 0;
favorites.setSelectedIndex(index);
}
}
public JButton getDefault() { return nextButton; }
// Return the (possibly cached) image having the given index.
private ImageIcon getImage(int index) {
ImageIcon image = cache.get(index);
if (image != null) return image;
String name = "images/" + list.get(index);
URL url = ImageSlider.class.getResource(name);
if (url != null) {
image = new ImageIcon(url);
}
cache.set(index, image);
return image;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageSlider go = new ImageSlider();
frame.add(go);
frame.setTitle("ImageSlider");
frame.setSize(400, 300);
frame.setVisible(true);
go.getDefault().requestFocusInWindow();
}
});
}
}
请告诉我哪里错了 提前致谢
最佳答案
" i found this code from a website"
复制和粘贴代码永远不是好办法。了解它的工作原理/原因,并创建您自己的代码,使用找到的代码作为引用。
" try to slide image it shows image not available i."
没有运行你的程序,因为我没有图像资源,我可以看到这个问题。您的代码使用 getResource()
,它将在类路径中查找图像。
URL url = ImageSlider.class.getResource(name);
传递给它的路径 name
需要类路径引用。但是你所做的是通过绝对路径
list.add("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\yellow.png");
...
String name = "images/" + list.get(index);
这段代码的设置方式,看起来代码的制作者在 src
中有他们的 images
文件夹,并且只是传递/添加了图像文件名,例如"image.png"
list.add("image.png");
你的文件结构应该是这样的
ProjectRoot
src
images
image.png
注意:这只是一个有根据的猜测。尝试一下(更改图像位置和路径。如果它不起作用,请尝试在 "images/"
→ "/之前添加一个
/
图片/"
关于java - 如何在 Swing 中使用图像 slider ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22293602/