Java 单击JButton-Swing,获取目录中的下一个映像

Java 单击JButton-Swing,获取目录中的下一个映像,java,swing,gallery,jbutton,Java,Swing,Gallery,Jbutton,我正在屏幕上显示一个图像,现在我想按Jbutton并显示该目录中的下一个图像。和前面的其他按钮应显示前面的图像。所有这些都在一个循环中。因此,第一个图像显示在目录的最后一个图像之后。这是我目前的代码: import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.image.Buf

我正在屏幕上显示一个图像,现在我想按Jbutton并显示该目录中的下一个图像。和前面的其他按钮应显示前面的图像。所有这些都在一个循环中。因此,第一个图像显示在目录的最后一个图像之后。这是我目前的代码:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import net.miginfocom.swing.MigLayout;

class ImageFilter extends FileFilter {

@Override
public boolean accept(File f) {
    if (f.isDirectory()) {
        return true;
    }
    String name = f.getAbsolutePath();
    if (name.matches(".*((.jpg)|(.gif)|(.png))"))
        return true;
    else
        return false;
}

@Override
public String getDescription() {
    return "Image Files(*.jpg, *.gif, *.png)";
}

}

@SuppressWarnings("serial")
class bottompanel extends JPanel {
JButton prev, next;

bottompanel() {
    this.setLayout(new MigLayout("debug", "45%[center][center]", ""));

    prev = new JButton("Previous");
    next = new JButton("Next");
    next.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent ae){

        }
    });
    this.add(prev, " w 100!");
    this.add(next, "w 100!");
}
}

@SuppressWarnings("serial")
class imgpanel extends JPanel {
imgpanel(JLabel image) {
    this.setLayout(new MigLayout("debug", "", ""));
    this.add(image, "push,align center");
}
}

@SuppressWarnings("serial")
class DispImg extends JFrame {
JMenuBar jmenubar;
JMenu jmenu;
JMenuItem jopen, jexit;
JLabel image;
BufferedImage img, dimg;

DispImg() {

    // initializing the Frame
    this.setTitle("Display Test");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setExtendedState(JFrame.MAXIMIZED_BOTH);
    this.setLayout(new MigLayout("debug", "[fill,grow]", "[]push[]"));
    this.setLocationByPlatform(true);
    this.setMinimumSize(new Dimension(800, 600));
    this.setVisible(true);
    this.setResizable(false);

    // create label
    image = new JLabel(" ");
    //add label to panel
    this.add(new imgpanel(image), "wrap");

    //add buttons to bottompanel
    this.add(new bottompanel(), "gaptop 10");

    // Making Menubar
    jmenubar = new JMenuBar();
    jmenu = new JMenu("File");
    jopen = new JMenuItem("Open");
    jopen.setMnemonic(KeyEvent.VK_O);
    KeyStroke key1 = KeyStroke.getKeyStroke(KeyEvent.VK_O, Event.CTRL_MASK);
    jopen.setAccelerator(key1);
    jopen.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            JFileChooser fc = new JFileChooser();
            fc.setFileFilter(new ImageFilter());
            int result = fc.showOpenDialog(null);
            if (result == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                try {
                    img = ImageIO.read(file);
                    float width = img.getWidth();
                    float height = img.getHeight();
                    if (img.getHeight() > 500 && (width / height) > 1) {
                        Image thumb = img.getScaledInstance(-1, 620,
                                Image.SCALE_SMOOTH);
                        image.setIcon(new ImageIcon(thumb));
                    } else if (img.getHeight() > 500
                            && (width / height) <= 1) {
                        Image thumb = img.getScaledInstance(460, -1,
                                Image.SCALE_SMOOTH);
                        image.setIcon(new ImageIcon(thumb));
                    } else {
                        image.setIcon(new ImageIcon(img));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    });
    jexit = new JMenuItem("Exit");
    jexit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            System.exit(0);
        }
    });
    jmenu.add(jopen);
    jmenu.addSeparator();
    jmenu.add(jexit);
    jmenubar.add(jmenu);
    this.setJMenuBar(jmenubar);
}

public static void main(String s[]) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new DispImg();
        }
    });
}
}
当我在“下一步”按钮上添加actionListener时,没有发生任何事情,也没有显示新图像

actionListener中没有任何代码,因此我不确定您希望发生什么


不知道如何从目录中获取下一个映像

您可能会使用JFileChooser选择目录,也可能选择要显示的初始图像


一旦你有了目录,你就可以使用文件.listFiles。。。方法获取目录中所有图像文件的列表。然后,“下一个/上一个”按钮将加/减一以访问数组中的下一个/上一个文件。

这是我迄今为止的代码:…-好的,那么你的具体问题是什么?就编写而言,您的问题只不过是一个代码转储,a这是我的代码,请修复它。为了得到一个更好的答案,请列出所有你一直坚持的细节。我们更擅长回答问题,而不是修复代码。首先,告诉我们您当前的代码有什么问题,缺少了什么,您可能看到了什么错误,…不知道如何从目录中获取下一个图像。当我在“下一步”按钮上添加actionListener时,没有发生任何事情,也没有显示新图像。没有生成异常,按next/previous应该会来回显示图像,但不会发生这种情况。