简体   繁体   English

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

[英]How to use image slider in swing?

Hi i am trying to use slide image using prev and next button button i am not able to this i did following code to achieve my output but when i run my program and try to slide image it shows image not available i. 嗨,我正在尝试通过上一个和下一个按钮使用幻灯片图像,但我无法执行以下代码来实现我的输出,但是当我运行程序并尝试滑动图像时,它显示图像不可用。 i am not getting why it is happening. 我不明白为什么会这样。 i found this code from a website 我从网站上找到了此代码

   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();
            }
        });
    }
}

please tell me where i am wrong Thanks in advance 请告诉我我哪里错了

" i found this code from a website" “我从一个网站上找到了此代码”

Copying ans pasting code is never the way to go. 复制ans粘贴代码绝不是路要走。 Learn how/why it works, and create your own code that uses the found code as a reference. 了解其工作方式/原因,并创建自己的代码,将找到的代码用作参考。

" try to slide image it shows image not available i." “尝试滑动图像,显示图像不可用。”

Without running your program, because I don't have the image resources, I could see this problem. 如果没有运行您的程序,因为我没有图像资源,则可能会看到此问题。 Your code, uses a getResource() , which will look for images in the class path. 您的代码使用getResource() ,它将在类路径中查找图像。

URL url = ImageSlider.class.getResource(name);

The path name That is passed to it requires a class path reference. 传递给它的路径name需要类路径引用。 But what you are doing is passing an absolute path 但是,您正在做的事情是走绝对的道路

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

The way this code is set up, it looks like the producer of the code had their images folder inside the src and just passed/added the image file name eg "image.png" 设置此代码的方式看起来像是代码的生产者将其images文件夹放在src并且只是传递/添加了图像文件名,例如"image.png"

list.add("image.png");

You your file structure should look like 您的文件结构应如下所示

ProjectRoot
         src
             images
                  image.png

NOTE: This is all just an educated guess. 注意:这只是一个有根据的猜测。 Try it out (change the image location and the paths. Also if it doesn't work, try to add a / before "images/""/images/" 试试吧(更改图像位置和路径。此外,如果它不工作,尝试添加/"images/""/images/"

Try This Code: This is simple code to slide images. 尝试以下代码:这是滑动图像的简单代码。

 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();
}


 }

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM