我让程序使用标签运行,但我不能让它使用图像。我是一个初学者,这就是我到目前为止所能想到的。它可以运行,但我不明白在此代码中实现图像。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutExample extends JFrame implements KeyListener
{
private Container pane = getContentPane();
private CardLayout layout = new CardLayout();
public CardLayoutExample()
{
pane.setLayout(layout);
pane.add(new JLabel("hey", SwingConstants.CENTER), "hey");
pane.add(new JLabel("what", SwingConstants.CENTER), "what");
pane.add(new JLabel("is", SwingConstants.CENTER), "is");
pane.add(new JLabel("your", SwingConstants.CENTER), "your");
pane.add(new JLabel("first", SwingConstants.CENTER), "first");
pane.add(new JLabel("name", SwingConstants.CENTER), "name");
addKeyListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("CardLayout Example");
setSize(410,310);
setVisible(true);
}
public void keyTyped(KeyEvent event)
{
layout.next(pane);
}
public void keyPressed(KeyEvent event){}
public void keyReleased(KeyEvent event){}
public static void main(String args[])
{
CardLayoutExample top = new CardLayoutExample();
}
}