D:\Java\JDK\jdk\bin\corejava\v1ch02\ImageViewer>javac Imageviewer.java
Imageviewer.java:15: 错误: 找不到符号
var frame = new ImageViewerFrame();
^
符号: 类 var
位置: 类 ImageViewer
Imageviewer.java:36: 错误: 找不到符号
var label = new JLabel();
源代码在下面
import java.awt.*;
import java.io.*;
import javax.swing.*;
/**
* A program for viewing images.
* @version 1.31 2018-04-10
* @author Cay Horstmann
*/
public class ImageViewer
{
public static void main(String[] args)
{
EventQueue.invokeLater(() -> {
var frame = new ImageViewerFrame();
frame.setTitle("ImageViewer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
}
/**
* A frame with a label to show an image.
*/
class ImageViewerFrame extends JFrame
{
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 400;
public ImageViewerFrame()
{
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
// use a label to display the images
var label = new JLabel();
add(label);
// set up the file chooser
var chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
// set up the menu bar
var menuBar = new JMenuBar();
setJMenuBar(menuBar);
var menu = new JMenu("File");
menuBar.add(menu);
var openItem = new JMenuItem("Open");
menu.add(openItem);
openItem.addActionListener(event -> {
// show file chooser dialog
int result = chooser.showOpenDialog(null);
// if file selected, set it as icon of the label
if (result == JFileChooser.APPROVE_OPTION)
{
String name = chooser.getSelectedFile().getPath();
label.setIcon(new ImageIcon(name));
}
});
var exitItem = new JMenuItem("Exit");
menu.add(exitItem);
exitItem.addActionListener(event -> System.exit(0));
}
}
刚学java,这个问题是环境配置没有配好还是var本来写的有问题。