本题程序的功能是:单击窗口左上角的File菜单中的Open命令,打开一个GIF图像文件,并将图像文件显示在窗口中;单击File菜单中的Exit命令,能够退出程序。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
public class simple extends JFrame implements ActionListener
public simple()
setTitle ("ImageViewer" );
setSize(300,400);
JMenuBar mbar = new JMenuBar();
JMenu m = new JMenu("File");
openItem = new JMenuItem("Open");
openItem,addActionListener (this);
m.add (openItem);
exitItem = new JMenuItem("Exit");
exitItem.addActionListener(this);
m.add(exitItem);
mbar.add (m);
setJMenuBar (mbar);
label = new JLabel();
Container contentPane = getContentPane();
contentPane.add(label,"Center");
public void actionPerformed(ActionEvent evt)
Object source = evt.getSource();
if (source == openItem)
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setFileFilter(new javax.swing.filechooser.FileFilter()
public boolean accept(File f)
return f.getName().toLowerCase().endsWith(".gif");
public String getDescription()
return "GIF Images";
);
int r = chooser.showOpenDialog(this);
if(r == JFileChooser.APPROVE_OPTION)
String name = chooser.getSelectedFile().getName();
label.setIcon(______);
else
if(source == exitItem)
System.exit(0);
public static void main(String[] args)
JFrame frame = new simple();
______;
private JLabel label;
private JMenuItem openItem;
private JMenuItem exitItem;
参考答案:new ImageIcon(name)。
frame.show()。