请大家看看,到底出了什么问题?是《java核心技术卷一》上的例子P26=-

qiujianben 2002-07-07 11:47:52
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.io.*;
import java.awt.swing.*;
public class ImageViewer
{
public static void main(String[] args)
{
JFrame frame = new ImageViewerFrame();
frame.setTitle("ImageViewer");
frame.setSize("300,400");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}

class ImageViewerFrame extends JFrame
{
public ImageViewerFrame()
{
//set menu bar
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);

JMenu menu = new JMenu("File");
menuBar.add(menu);

JMenuItem openItem = new JMenuItem("Open");
menu.add(openItem);
openItem.addActionListener(new FileOpenListener());

JMenuItem exitItem = new JMenuItem("Exit");
menu.add(exitItem);
exitItem.AddActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
label = new JLabel();
Container contentPane = getContentPane();
contentPane.add(label,"Center");
}

private class FileOpenListener implements ActionListener
{
//set up file choose
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
//accept all files ending by .gif
chooser.setFileFilter(new javax.swing.filechooser.FileFilter
{
public boolean accept(File f)
{
return f.getName().toLowerCase().endsWith(".gif")||f.isDirectory();
}
public String getDescription()
{
return "GIF Images";
}
});
int r = chooser.showOpenDialog(ImageViewerFrame.this);
if (r == JFileChooser.APPROVE_OPTION)
{
String name = chooser.getSelectedFile().getPath();
label.setIcon(new ImageIcon(name);
}
}
}
private JLabel label;
}

编译结果:
---------- javac ----------
ImageViewer.java:51: <identifier> expected
chooser.setCurrentDirectory(new File("."));
^
ImageViewer.java:53: <identifier> expected
chooser.setFileFilter(new javax.swing.filechooser.FileFilter
^
ImageViewer.java:65: illegal start of type
if (r == JFileChooser.APPROVE_OPTION)
^
ImageViewer.java:69: <identifier> expected
}
^
ImageViewer.java:72: 'class' or 'interface' expected
private JLabel label;
^
ImageViewer.java:73: 'class' or 'interface' expected
}
^
ImageViewer.java:73: 'class' or 'interface' expected
}
^
ImageViewer.java:4: package java.awt.io does not exist
import java.awt.io.*;
^
ImageViewer.java:5: package java.awt.swing does not exist
import java.awt.swing.*;
^
ImageViewer.java:18: cannot resolve symbol
symbol : class JFrame
location: class ImageViewerFrame
class ImageViewerFrame extends JFrame
^
ImageViewer.java:50: cannot resolve symbol
symbol : class JFileChooser
location: class ImageViewerFrame.FileOpenListener
JFileChooser chooser = new JFileChooser();
^
ImageViewer.java:51: package chooser does not exist
chooser.setCurrentDirectory(new File("."));
^
ImageViewer.java:53: package chooser does not exist
chooser.setFileFilter(new javax.swing.filechooser.FileFilter
^
ImageViewer.java:10: cannot resolve symbol
symbol : class JFrame
location: class ImageViewer
JFrame frame = new ImageViewerFrame();
^
ImageViewer.java:13: cannot resolve symbol
symbol : variable JFrame
location: class ImageViewer
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
^
ImageViewer.java:23: cannot resolve symbol
symbol : class JMenuBar
location: class ImageViewerFrame
JMenuBar menuBar = new JMenuBar();
^
ImageViewer.java:23: cannot resolve symbol
symbol : class JMenuBar
location: class ImageViewerFrame
JMenuBar menuBar = new JMenuBar();
^
ImageViewer.java:26: cannot resolve symbol
symbol : class JMenu
location: class ImageViewerFrame
JMenu menu = new JMenu("File");
^
ImageViewer.java:26: cannot resolve symbol
symbol : class JMenu
location: class ImageViewerFrame
JMenu menu = new JMenu("File");
^
ImageViewer.java:29: cannot resolve symbol
symbol : class JMenuItem
location: class ImageViewerFrame
JMenuItem openItem = new JMenuItem("Open");
^
ImageViewer.java:29: cannot resolve symbol
symbol : class JMenuItem
location: class ImageViewerFrame
JMenuItem openItem = new JMenuItem("Open");
^
ImageViewer.java:33: cannot resolve symbol
symbol : class JMenuItem
location: class ImageViewerFrame
JMenuItem exitItem = new JMenuItem("Exit");
^
ImageViewer.java:33: cannot resolve symbol
symbol : class JMenuItem
location: class ImageViewerFrame
JMenuItem exitItem = new JMenuItem("Exit");
^
ImageViewer.java:42: cannot resolve symbol
symbol : variable label
location: class ImageViewerFrame
label = new JLabel();
^
ImageViewer.java:42: cannot resolve symbol
symbol : class JLabel
location: class ImageViewerFrame
label = new JLabel();
^
ImageViewer.java:43: cannot resolve symbol
symbol : method getContentPane ()
location: class ImageViewerFrame
Container contentPane = getContentPane();
^
ImageViewer.java:44: cannot resolve symbol
symbol : variable label
location: class ImageViewerFrame
contentPane.add(label,"Center");
^
ImageViewer.java:47: ImageViewerFrame.FileOpenListener should be declared abstract; it does not define actionPerformed(java.awt.event.ActionEvent) in ImageViewerFrame.FileOpenListener
private class FileOpenListener implements ActionListener
^
ImageViewer.java:50: cannot resolve symbol
symbol : class JFileChooser
location: class ImageViewerFrame.FileOpenListener
JFileChooser chooser = new JFileChooser();
^
29 errors
Normal Termination
输出完成(耗费 2 秒)。
...全文
67 点赞 收藏 2
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
苏格拉顶777 2002-07-08
FileOpenListener的定义里面,你把实现的代码放在了,类成员定义里了。
应该放在实现的actionPerformed函数里。语法错误。
回复
KooKvv 2002-07-08
private class FileOpenListener implements ActionListener
{
下少一行code.
public void actionPerformed(ActionEvent evt)
{
回复
发帖
Java SE
创建于2007-09-28

6.1w+

社区成员

Java 2 Standard Edition
申请成为版主
帖子事件
创建了帖子
2002-07-07 11:47
社区公告
暂无公告