您好我正在尝试使用普通和下一个按钮按钮使用幻灯片图像我无法做到这一点我做了以下代码来实现我的输出但是当我运行我的程序并尝试滑动图像它显示图像不可用我。我不知道为什么会发生这种情况。我发现了这个代码 一个网站
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();
}
});
}
}
请告诉我我错在哪里
提前致谢
答案 0 :(得分:5)
“我在网站上找到了这段代码”
复制和粘贴代码永远不是可行的方法。了解它的工作原理和原因,并创建自己的代码,使用找到的代码作为参考。
“尝试滑动图像,显示图像不可用i。”
如果没有运行程序,因为我没有图像资源,我可以看到这个问题。您的代码使用getResource()
,它将在类路径中查找图像。
URL url = ImageSlider.class.getResource(name);
传递给它的路径name
需要类路径引用。但你正在做的是传递绝对路径
list.add("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\yellow.png");
...
String name = "images/" + list.get(index);
设置此代码的方式,看起来代码的生产者在images
中有src
个文件夹,只是传递/添加了图像文件名,例如"image.png"
list.add("image.png");
您的文件结构应该是
ProjectRoot
src
images
image.png
注意:这只是一个有根据的猜测。尝试一下(更改图片位置和路径。如果不起作用,请尝试在/
→"images/"
之前添加"/images/"
答案 1 :(得分:1)
尝试此代码:这是滑动图像的简单代码。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class myImage extends JFrame implements ActionListener
{
ImageIcon m[];
JLabel l;
JButton b,b1;
int i,l1;
JPanel p;
public myImage()
{
setLayout(new BorderLayout( ));
setSize(1000, 1000);
setVisible(true);
JPanel p=new JPanel(new FlowLayout());
b=new JButton("Pre");
b1=new JButton("Next");
p.add(b);
p.add(b1);
add(p,BorderLayout.SOUTH);
b.addActionListener(this);
b1.addActionListener(this);
m = new ImageIcon[2];
m[0] = new ImageIcon("m.jpg");
m[1] = new ImageIcon("m1.jpg");
l = new JLabel();
l.setBounds(400, 0, getWidth(), getHeight());
add(l,BorderLayout.CENTER);
l.setIcon(m[0]);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b)
{
if(i==0)
{
JOptionPane.showMessageDialog(null,"This is First Image");
}
else
{
i=i-1;
l.setIcon(m[i]);
}
}
if(e.getSource()==b1)
{
if(i==m.length-1)
{
JOptionPane.showMessageDialog(null,"This is LastImage");
}
else
{
i=i+1;
l.setIcon(m[i]);
}
}
}
public static void main(String args[])
{
myImage i1 = new myImage();
}
}