我希望建立一个向朋友和家人展示图片的程序。使用常规Windows Image Viewer时,会有大量未使用的屏幕空间,并且它不会对我用于PPT演示的USB演示器做出反应。
我的想法是让用户选择一个文件夹(简单),将选择该文件夹中的所有图像,JFrame将以全屏模式显示第一个图像(简单)。在右箭头(简单)上,演示文稿将跳转到选择的下一张图片(困难)。听起来很简单,但我无法解决。
这是我安德鲁斯建议后的代码:
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.KeyEvent;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.ImageIcon;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
class ArrowAction extends AbstractAction {
private String cmd;
public ArrowAction(String cmd) {
this.cmd = cmd;
}
@Override
public void actionPerformed(ActionEvent e) {
if (cmd.equalsIgnoreCase("Backward")) {
System.out.println("Previous picture!");
} else if (cmd.equalsIgnoreCase("Forward")) {
System.out.println("Next Picture!");
}
}
}
class TestKeyBinding {
public TestKeyBinding() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
InputMap im = panel.getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW);
ActionMap am = panel.getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "Forward");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "Backward");
am.put("Forward", new ArrowAction("Forward"));
am.put("Backward", new ArrowAction("Backward"));
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsDevice gs = ge.getDefaultScreenDevice();
gs.setFullScreenWindow(frame);
BufferedImage myPicture;
try {
myPicture = ImageIO.read(new URL("http://upload.wikimedia.org/wikipedia/commons/1/13/Vancouver_ib.jpg"));
JLabel picLabel = new JLabel(new ImageIcon(myPicture));
panel.add(picLabel);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
frame.add(panel);
frame.validate();
}
}
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
TestKeyBinding kb = new TestKeyBinding();
}
}
知道怎么做onKeyEvent并显示下一张图片吗?还有我应该习惯的其他最佳实践吗?
谢谢, 弗洛里安