あなたはKeyBindingsを見てきましたが、これはシンプルで可能です。たとえば、(3.HFOEのコードに基づいて、ありがとう)、残りは最初と最後の値setEnabled
をブロックすることですJButtons
Cards
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class OnTheFlyImageTest {
private static final long serialVersionUID = 1L;
private JPanel cardPanel;
private CardLayout cardLayout;
private JFrame frame;
public OnTheFlyImageTest() {
JPanel cp = new JPanel(new BorderLayout());
cp.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
cardLayout = new CardLayout(5, 5);
cardPanel = new JPanel(cardLayout);
cp.add(cardPanel);
for (int i = 0; i < 5; i++) {// Create random panels for testing.
String name = "ImagePanel" + (i + 1);
String image = (i & 1) == 0 ? "foo.gif" : "bar.gif";
Color clr = (i & 1) == 0 ? Color.red : Color.blue;
ImagePanel imgPanel = new ImagePanel(name, image, clr);
cardPanel.add(imgPanel, name);
cardLayout.addLayoutComponent(imgPanel, name);
}
JPanel buttonPanel = new JPanel(new GridLayout(1, 2, 5, 5));
JButton prevButton = new JButton("< Previous");
prevButton.setActionCommand("Previous");
prevButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cardLayout.previous(cardPanel);
}
});
buttonPanel.add(prevButton);
JButton nextButton = new JButton("Next >");
nextButton.setActionCommand("Next");
nextButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cardLayout.next(cardPanel);
}
});
buttonPanel.add(nextButton);
JPanel temp = new JPanel(new BorderLayout());
temp.add(buttonPanel, BorderLayout.LINE_END);
cp.add(temp, BorderLayout.SOUTH);
frame = new JFrame("CardLayout and KeyBindings");
frame.add(cp);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Test");
frame.pack();
frame.setVisible(true);
setKeyBindings();
}
private void setKeyBindings() {
frame.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
KeyStroke.getKeyStroke("TAB"), "clickTAB");
frame.getRootPane().getActionMap().put("clickTAB", new AbstractAction() {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
cardLayout.show(cardPanel, "ImagePanel" + 1);
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new OnTheFlyImageTest();
}
});
}
}
class ImagePanel extends JPanel {
private static final long serialVersionUID = 1L;
private String imgString;
private JLabel imgLabel;
public ImagePanel(String name, String imgString, Color backGround) {
setName(name);
this.imgString = imgString;
setLayout(new BorderLayout());
setBackground((backGround));
// Ensure size is correct even before any image is loaded.
setPreferredSize(new Dimension(400, 300));
}
@Override
public void setVisible(boolean visible) {
if (visible) {
System.err.println(getName() + ": Loading and adding image");
ImageIcon icon = new ImageIcon(imgString);
imgLabel = new JLabel(icon);
add(imgLabel);
}
super.setVisible(visible);
if (!visible) { // Do after super.setVisible() so image doesn't "disappear".
System.err.println(getName() + ": Removing image");
if (imgLabel != null) { // Before display, this will be null
remove(imgLabel);
imgLabel = null; // Hint to GC that component/image can be collected.
}
}
}
}