首页 > 代码库 > Properties的练习
Properties的练习
import java.awt.event.*; import java.io.*; import java.util.Properties; import javax.swing.*; /** Properties的练习 */ public class Sample extends JApplet { public static void main(String[] args) { PropertiesFrame frame = new PropertiesFrame(); frame.setVisible(true); } } class PropertiesFrame extends JFrame { public PropertiesFrame() { String userDir = System.getProperty("user.home"); File propertiesDir = new File(userDir,".corejava"); if(!propertiesDir.exists()) { propertiesDir.mkdir(); } propertiesFile = new File(propertiesDir,"program.properties"); Properties defaultSettings = new Properties(); defaultSettings.put("left","0"); defaultSettings.put("top","0"); defaultSettings.put("width",""+DEFAULT_WIDTH); defaultSettings.put("height",""+DEFAULT_HEIGHT); defaultSettings.put("title",""); settings = new Properties(defaultSettings); if(propertiesFile.exists()) { try { FileInputStream in = new FileInputStream(propertiesFile); settings.load(in); } catch (IOException ex) { ex.printStackTrace(); } } int left = Integer.parseInt(settings.getProperty("left")); int top = Integer.parseInt(settings.getProperty("top")); int width = Integer.parseInt(settings.getProperty("width")); int height = Integer.parseInt(settings.getProperty("height")); setBounds(left,top,width,height); String title = settings.getProperty("title"); if(title.equals("")) { title = JOptionPane.showInputDialog("Please supply a frame title:"); } if(title == null) { title = ""; } setTitle(title); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent event) { settings.put("left",""+getX()); settings.put("top",""+getY()); settings.put("width",""+getWidth()); settings.put("height",""+getHeight()); settings.put("title",getTitle()); try { FileOutputStream out = new FileOutputStream(propertiesFile); settings.store(out,"Program Properties"); } catch (IOException ex) { ex.printStackTrace(); } System.exit(0); } }); } private File propertiesFile; private Properties settings; public static final int DEFAULT_WIDTH = 300; public static final int DEFAULT_HEIGHT = 200; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。