前端之家收集整理的这篇文章主要介绍了
Preferences写XML文件,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package corejavaI.layout; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.prefs.Preferences; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; public class PreferencesTest { public static void main(String[] args) { // TODO Auto-generated method stub EventQueue.invokeLater(new Runnable() { public void run() { // TODO Auto-generated method stub PreferencesFrame frame = new PreferencesFrame(); frame.setLocation(300,200); frame.setDefaultClo
SEOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300,300); frame.setVisible(true); } }); } } class PreferencesFrame extends JFrame{ private static final int DEFAULT_WIDTH = 300; private static final int DEFULT_HEIGHT = 200; public PreferencesFrame(){ Preferences root = Preferences.userRoot(); final Preferences node = root.node("/com/horstman/corejava"); int left = node.getInt("left",0); int top = node.getInt("top",0); int width = node.getInt("width",DEFAULT_WIDTH); int height = node.getInt("height",DEFULT_HEIGHT); setBounds(left,top,width,height); String title = node.get("title",""); if(title.equals("")) title = JOptionPane.showInputDialog("请输入frame title:"); if(title == null) title = ""; System.out.println(title); setTitle(title); final JFileChooser chooser = new JFileChooser(); chooser.setCurrentDirectory(new File(".")); chooser.setFileFilter(new javax.swing.filechooser.FileFilter(){ @Override public boolean accept(File f) { return f.getName().toLowerCase().endsWith(".xml") || f.isDirectory(); } @Override public String getDescription() { return "XML files"; } }); 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(new ActionListener() { public void actionPerformed(ActionEvent e) { if(chooser.showSaveDialog(PreferencesFrame.this) == JFileChooser.APPROVE_OPTION){ try { OutputStream out = new FileOutputStream(chooser.getSelectedFile()); node.exportSubtree(out); out.close(); } catch (Exception e2) { e2.printStackTrace(); } } } }); JMenuItem importItem = new JMenuItem("Import preferencs"); menu.add(importItem); importItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(chooser.showOpenDialog(PreferencesFrame.this) == JFileChooser.APPROVE_OPTION){ try { InputStream in = new FileInputStream(chooser.getSelectedFile()); Preferences.importPreferences(in); in.close(); } catch (Exception e2) { e2.printStackTrace(); } } } }); JMenuItem exitItem = new JMenuItem("Exit"); menu.add(exitItem); exitItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub node.putInt("left",getX()); node.putInt("top",getY()); node.putInt("width",getWidth()); node.putInt("height",getHeight()); node.put("title",getTitle()); System.exit(0); } }); } }
原文链接:https://www.f2er.com/xml/298403.html