import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PhotoAlbum implements ActionListener {
JFrame frame;
JPanel contentPane;
JButton next;
JLabel pictures;
JLabel sticker;
public PhotoAlbum(){
/* Create and set up the frame */
frame = new JFrame("Photo Album");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/* Create a content pane with a BoxLayout and empty borders */
contentPane = new JPanel();
contentPane.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));
contentPane.setBackground(Color.white);
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
/* Create a label that shows the start of the game */
pictures = new JLabel(new ImageIcon("icecream.gif"));
pictures.setAlignmentX(JLabel.CENTER_ALIGNMENT);
pictures.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
contentPane.add(pictures);
/* Create a next button */
next = new JButton("Next");
next.setActionCommand("Next");
next.setAlignmentX(JButton.CENTER_ALIGNMENT);
next.addActionListener(this);
contentPane.add(next);
/* Add content pane to frame */
frame.setContentPane(contentPane);
/* Size and then disnext the frame. */
frame.pack();
frame.setVisible(true);
}
/**
* Handle the button click
* pre: none
* post: The appropriate image and message are disnexted.
*/
public void actionPerformed(ActionEvent event) {
if (event.getSource() == "Next"){
for ( int count = 0; count < 6; count ++){
switch (count) {
case 1: pictures.setIcon(new ImageIcon("icecream.gif"));
break;
case 2: pictures.setIcon(new ImageIcon("coffee.gif"));
break;
case 3: pictures.setIcon(new ImageIcon("giraffe.gif"));
break;
case 4: pictures.setIcon(new ImageIcon("jeep.gif"));
break;
case 5: pictures.setIcon(new ImageIcon("sunset.gif"));
break;
default:
}
}
}
}
/**
* Create and show the GUI.
*/
private static void runGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
PhotoAlbum Game = new PhotoAlbum();
}
public static void main(String[] args) {
/* Methods that create and show a GUI should be
run from an event-dispatching thread */
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
runGUI();
}
});
}
}
此代码不显示任何图像。我该如何解决?我试图重写它或使用不同的声明,但它仍然没有显示图片。只有按钮