我正在屏幕上显示一个图像,现在我想按下 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();
}
});
}
}