我试图遵循一些基于Java Preferences API的演示代码,但是我发现一种奇怪的方法,未找到与setDefaultCloseOperation(int)和setJMenuBar(JMenuBar)有关的错误。此示例程序摘自Cay S. Horstman撰写的 Core Java,第一卷-基础知识,第11版的第796-799页。
对于可能出现的小错误,我深表歉意,但是我一直在为应该是一个简单的解决方法而head之以鼻。重现此问题需要整个文件,因为如果忽略了文件的某些部分,则可能会引起除上述之外的其他编译错误。
我尝试了以下方法。
.*
。JDK 1.8_202
与旧版/从未发布过的JDK版本)。PreferencesFrame.java
import java.awt.*;
import java.io.*;
import java.util.prefs.*;
import javax.swing.*;
import javax.swing.filechooser.*;
/** A program to test preference settings. The program remembers the frame position, size, and title. **/
public class PreferencesTest {
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
PreferencesFrame frame = new PreferencesFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
}
/** A frame that restores position and size from user preferences and updates the preferences upon exit. **/
class PreferencesFrame extends Frame {
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 200;
private Preferences root = Preferences.userRoot();
private Preferences node = root.node("/com/horstmann/corejava");
public PreferencesFrame() {
// Get position, size, and title from preferences.
int left = node.getInt("left", 0);
int top = node.getInt("top", 0);
int width = node.getInt("width", DEFAULT_WIDTH);
int height = node.getInt("height", DEFAULT_HEIGHT);
setBounds(left, top, width, height);
// If no title was given, ask the user.
String title = node.get("title", "");
if (title.equals(""))
title = JOptionPane.showInputDialog("Title:");
if (title == null) title = "";
setTitle(title);
// Set up the file chooser to show XML files, to select the preferences.xml file.
final JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setFileFilter(new FileNameExtensionFilter("XML files", "xml"));
// Set up the menus.
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menu = new JMenu("File");
menuBar.add(menu);
JMenuItem exportItem = new JMenuItem("Export Preferences");
menu.add(exportItem);
exportItem.addActionListener(event -> {
if (chooser.showSaveDialog(PreferencesFrame.this) == JFileChooser.APPROVE_OPTION) {
try {
savePreferences();
OutputStream out = new FileOutputStream(chooser.getSelectedFile());
node.exportSubtree(out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
JMenuItem importItem = new JMenuItem("Import Preferences");
menu.add(importItem);
importItem.addActionListener(event -> {
if (chooser.showSaveDialog(PreferencesFrame.this) == JFileChooser.APPROVE_OPTION) {
try {
InputStream in = new FileInputStream(chooser.getSelectedFile());
Preferences.importPreferences(in);
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
JMenuItem exitItem = new JMenuItem("Exit");
menu.add(exitItem);
exitItem.addActionListener(event -> {
savePreferences();
System.exit(0);
});
}
public void savePreferences() {
node.putInt("left", getX());
node.putInt("top", getY());
node.putInt("width", getWidth());
node.putInt("height", getHeight());
node.put("title", getTitle());
}
}
preferences.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd">
<preferences EXTERNAL_XML_VERSION="1.0">
<root type="user">
<map/>
<node name="com">
<map/>
<node name="horstmann">
<map/>
<node name="corejava">
<map>
<entry key="left" value="11"/>
<entry key="top" value="9"/>
<entry key="width" value="453"/>
<entry key="height" value="365"/>
<entry key="title" value="Hello, World!"/>
</map>
</node>
</node>
</node>
</root>
</preferences>
我希望程序可以编译并显示JFrame,但是会看到2个编译错误。
1. PreferencesTest.java.13: error: cannot find symbol
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
symbol: method setDefaultCloseOperation(int)
class PreferencesFrame
2. PreferencesTest.java:49: error: cannot find symbol setJMenuBar(menuBar);
symbol: method setJMenuBar(JMenuBar)
class PreferencesFrame
答案 0 :(得分:2)
问题可能在这里:hdfs:///
。它应该是class PreferencesFrame extends Frame
。 class PreferencesFrame extends JFrame
类来自Frame
包,java.awt
类(具有JFrame
方法的类)来自setDefaultCloseOperation
包。
您的总进口应为:
javax.swing