如何在秋千中使用图像滑块?

1 java swing jlist jslider imageicon

嗨我想使用prev和下一个按钮按钮使用幻灯片图像我不能这样我按照代码实现我的输出但是当我运行我的程序并尝试幻灯片图像它显示图像不可用我.我不知道为什么会发生这种情况.我从网站上找到了这个代码

   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();
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

请告诉我我错在哪里先谢谢

Pau*_*tha 5

"我从网站上找到了这个代码"

复制和粘贴代码绝不是可行的方法.了解它的工作原理和原因,并创建自己的代码,使用找到的代码作为参考.

"尝试滑动图像,显示图像不可用i."

没有运行你的程序,因为我没有图像资源,我可以看到这个问题.您的代码使用a getResource(),它将在类路径中查找图像.

URL url = ImageSlider.class.getResource(name);
Run Code Online (Sandbox Code Playgroud)

路径name传递给它需要的类路径参考.但你正在做的是通过绝对的道路

list.add("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\yellow.png");
...
String name = "images/" + list.get(index);
Run Code Online (Sandbox Code Playgroud)

设置此代码的方式,看起来代码的生产者将其images文件夹放在其中src并且只是传递/添加了图像文件名,例如"image.png"

list.add("image.png");
Run Code Online (Sandbox Code Playgroud)

你的文件结构应该是这样的

ProjectRoot
         src
             images
                  image.png
Run Code Online (Sandbox Code Playgroud)

注意:这只是一个有根据的猜测.尝试一下(更改图像位置和路径.如果不起作用,请尝试添加/之前的"images/""/images/"