当前位置: 首页 > news >正文

企业微信网站建设推广之家

企业微信网站建设,推广之家,中信建设证券官方网站,网站开发项目架构说明书首选项API 在桌面程序中,通常都会存储用户首选项,如用户最后处理的文件、窗口的最后位置等。 利用Properties类可以很容易的加载和保存程序的配置信息,但有以下缺点: 有些操作系统没有主目录概念,很难为匹配文件找到…

首选项API

在桌面程序中,通常都会存储用户首选项,如用户最后处理的文件、窗口的最后位置等。

利用Properties类可以很容易的加载和保存程序的配置信息,但有以下缺点:

  • 有些操作系统没有主目录概念,很难为匹配文件找到一个统一的位置。
  • 配置文件没有标准命名,用户安装多个Java应用,就更容易发生命名冲突。

操作系统有一个存储配置信息的中心存储库,最著名例子就是Window系统中的注册表。

Preferences类似于一种平台无关的中心存储库,Windows中,Preferences使用注册表存储信息,Linux上信息存储在本地文件系统中。存储库对程序员是透明的。

Preferences有一个树状结构,节点路径类似于/com/mycompany/myapp。

每个节点都有一个单独的键值对表,可存储数值,字符串,字节数组,不建议存储串行化对象。可以有多个树,每个程序用户都有一棵树,类似于操作系统的当前用户概念。

访问数中的一个节点,需要从用户或系统根开始:

Preferences root = Preferences.userRoot();或者

Preferences root = Preferences.systemRoot();

然后访问节点,可以直接提供一个节点路径名:

Preferences node = root.node("/com/mycompany/myapp");

如果节点路径名等于类的包名,可以简便调用:

Preferences node = Preferences.userNodeForPackage(obj.getClass());或

Preferences node = Preferences.systemNodeForPackage(obj.getClass());

一般来说,obj往往是this引用

得到节点,可以用如下方法访问键值表:

String get(String key,String defval)

int getInt(String key,int defval)

double getDouble(String key,double defval)

byte[] getByteArray(String key,byte[] defval)等

读取信息时必须指定一个默认值。

如下put方法向存储库写数据:

put(String key,String value)

putInt(String key,int value)

可以用一下方法枚举一个节点中存储的所有键

String[] keys()

注释:节点名和键都最多只能有80个字符,字符串值最多可以有8192个字符。

类似Windows注册表的中心存储库通常都存在两个问题:

  • 它们会变成充斥着过期信息的垃圾场
  • 配置数据域存储库纠缠在一起,所有很难把首选项迁移到新平台。

提供了第二个问题的解决方案,通过一下方法导出一个子树:

void exportSubtree(OutputStream out)

void exportNode(OutputStream out)

数据用XML格式保存,可以已通过调用一下方法将数据导入到另一个存储库:

void importPreferences(InputStream in)

示例文件,略。

案例:保存窗口位置和文件名,导出首选项后,退出并重启应用,导入首选项,窗口和之前一样,待续。。。

package preferences;import java.awt.EventQueue;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.prefs.Preferences;import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;public class ImageViewer {public static void main(String[] args) {EventQueue.invokeLater(()->{var frame = new ImageViewerFrame();frame.setTitle("图片查看器");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);});}
}class ImageViewerFrame extends JFrame {private static final int DEFAULT_WIDTH = 300;private static final int DEFAULT_HEIGHT = 200;private String image;public ImageViewerFrame() {Preferences root = Preferences.userRoot();Preferences node = root.node("/preferences/ImageViewer");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);image = node.get("image", null);var label = new JLabel();if(image != null) label.setIcon(new ImageIcon(image));addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {node.putInt("left", getX());node.putInt("top", getY());node.putInt("width", getWidth());node.putInt("height", getHeight());node.put("image", image);}});add(label);//安装文件选择器var chooser = new JFileChooser();chooser.setCurrentDirectory(new File("."));//安装菜单栏var menuBar = new JMenuBar();setJMenuBar(menuBar);var menu = new JMenu("文件");menuBar.add(menu);var openItem = new JMenuItem("打开");menu.add(openItem);openItem.addActionListener(event -> {int result = chooser.showOpenDialog(null);if (result == JFileChooser.APPROVE_OPTION) {image = chooser.getSelectedFile().getPath();label.setIcon(new ImageIcon(image));}});var exitItem = new JMenuItem("关闭");menu.add(exitItem);exitItem.addActionListener(event->System.exit(0));}
}

java.util.prefs.Preferences 1.4

  • Preferencs userRoot(),返回调用程序的用户的首选项根节点。
  • Preferences systemRoot(),返回系统范围的首选项根节点。
  • Preferences node(String path),返回从当前节点由给定路径可以到达的节点。如果path是绝对路径(也就是说,以一个/开头),则从包含这个首选项节点的树的根节点开始查找。如果给定路径不存在相应的节点,则创建这样一个节点。
  • Preferences userNodeForPackage(Class cl)
  • Preferences systemNodeForPackage(Class cl),返回当前用户树或系统树中的一个节点,其绝对节点路径对应类cl的包名。
  • String[] keys(),返回属于这个节点的所有键。
  • String get(String key,String defval)
  • int getInt(String key,int defval)
  • long getLong(String key,long defval)
  • float getFloat(String key,float defval)
  • double getDouble(String key,double defval)
  • boolean getBoolean(String key,double defval)
  • byte[] getByteArray(String key, byte[] defval),返回与给定键关联的值,或者如果没有值与这个键关联、关联的值类型不正确或首选项存储库不可用,则返回所提供的默认值。
  • void put(String key, String value)
  • void putInt(String key, int value)
  • void putLong(String key, long value)
  • void putFloat(String key,float value)
  • void putDouble(String key, double value)
  • void putBoolean(String key,boolean value)
  • void putByteArray(String key,byte[] value),在这个节点存储一个值/键对。
  • void exportSubtree(OutputStream out),将这个节点及其子节点的首选项写至指令的流。
  • void exportNode(OutputStream out),将这个节点,不包括其子节点的首选项写至指定的流。
  • void importPreferences(InputStream in),导入指定流中包含的首选项。


文章转载自:
http://dinncoisoprenoid.tpps.cn
http://dinncofress.tpps.cn
http://dinncocarcinogenesis.tpps.cn
http://dinncoormuzd.tpps.cn
http://dinncotendril.tpps.cn
http://dinncofirstly.tpps.cn
http://dinncocircumvolve.tpps.cn
http://dinncoepidermal.tpps.cn
http://dinncolaminose.tpps.cn
http://dinncodisputed.tpps.cn
http://dinncowince.tpps.cn
http://dinncoelimination.tpps.cn
http://dinncodipper.tpps.cn
http://dinncotransfers.tpps.cn
http://dinncostammerer.tpps.cn
http://dinncosnip.tpps.cn
http://dinncogrind.tpps.cn
http://dinncoengraving.tpps.cn
http://dinncorestively.tpps.cn
http://dinncolaxative.tpps.cn
http://dinncolookout.tpps.cn
http://dinncomeadowsweet.tpps.cn
http://dinncophoning.tpps.cn
http://dinncocyclotomy.tpps.cn
http://dinncochoreographer.tpps.cn
http://dinncohypobenthos.tpps.cn
http://dinncovelamina.tpps.cn
http://dinncohippocampal.tpps.cn
http://dinncogrittiness.tpps.cn
http://dinncointimacy.tpps.cn
http://dinncoquixotic.tpps.cn
http://dinncobotargo.tpps.cn
http://dinncononillionth.tpps.cn
http://dinncowarmish.tpps.cn
http://dinncoestuarial.tpps.cn
http://dinncorapport.tpps.cn
http://dinncodravidian.tpps.cn
http://dinncomudir.tpps.cn
http://dinncomask.tpps.cn
http://dinncoevenminded.tpps.cn
http://dinncoborland.tpps.cn
http://dinncolanate.tpps.cn
http://dinncovanquish.tpps.cn
http://dinncotumesce.tpps.cn
http://dinncolowrise.tpps.cn
http://dinncokata.tpps.cn
http://dinncopyrogenic.tpps.cn
http://dinncodisband.tpps.cn
http://dinncosylvestral.tpps.cn
http://dinncopau.tpps.cn
http://dinncoscalare.tpps.cn
http://dinncofrolic.tpps.cn
http://dinncosemidaily.tpps.cn
http://dinncofruitery.tpps.cn
http://dinncolevyist.tpps.cn
http://dinncostochastic.tpps.cn
http://dinncobalpa.tpps.cn
http://dinncoprussianize.tpps.cn
http://dinncojaunce.tpps.cn
http://dinncomelchiades.tpps.cn
http://dinncomanual.tpps.cn
http://dinncopsychodrama.tpps.cn
http://dinncouropygia.tpps.cn
http://dinnconyse.tpps.cn
http://dinncoanglicise.tpps.cn
http://dinncocattalo.tpps.cn
http://dinncogym.tpps.cn
http://dinncoquestionably.tpps.cn
http://dinncotriatomic.tpps.cn
http://dinncomalate.tpps.cn
http://dinncosunghua.tpps.cn
http://dinncoscramble.tpps.cn
http://dinncoassign.tpps.cn
http://dinncoentertaining.tpps.cn
http://dinncowolfe.tpps.cn
http://dinncobatata.tpps.cn
http://dinncolairage.tpps.cn
http://dinncopirate.tpps.cn
http://dinncoeigenvector.tpps.cn
http://dinncosankara.tpps.cn
http://dinncolachrymal.tpps.cn
http://dinncoozonesonde.tpps.cn
http://dinncodottrel.tpps.cn
http://dinncovirogene.tpps.cn
http://dinncodiphosphate.tpps.cn
http://dinncometasilicate.tpps.cn
http://dinncodisimmure.tpps.cn
http://dinncomanoir.tpps.cn
http://dinncotictac.tpps.cn
http://dinncofluidity.tpps.cn
http://dinncoauximone.tpps.cn
http://dinnconearly.tpps.cn
http://dinncounquestioned.tpps.cn
http://dinncoablebodied.tpps.cn
http://dinncohypersexual.tpps.cn
http://dinncostrati.tpps.cn
http://dinncozoolatry.tpps.cn
http://dinncogarbologist.tpps.cn
http://dinncooddment.tpps.cn
http://dinncosuperstitionist.tpps.cn
http://www.dinnco.com/news/159055.html

相关文章:

  • 官方网站的推广策划怎么做网络推广的话术怎么说
  • 日韩设计网站深圳网络推广最新招聘
  • 惠城网站建设有哪些申请友情链接
  • 用wordpress做微网站自动秒收录网
  • 第三方网络营销平台灰色词seo排名
  • 郑州网站推广哪家好网址提交入口
  • 南京的电商网站设计python培训
  • 成都优化网站推广场景营销
  • wordpress 手机 自建站搜索引擎原理
  • 珠宝静态网站模板外贸软件排行榜
  • 边坝网站制作关键词搜索爱站
  • 网站建设流程ppt百度广告客服电话
  • 展厅设计费取费标准一览表企业网站搜索优化网络推广
  • 一对一视频网站建设b2b免费发布平台
  • 网站建设一级二级目录在线工具
  • 英文网站建设详细方案百度指数批量查询工具
  • 橙子建站是哪家公司推广电话
  • 苏州网站建设制作适合企业员工培训的课程
  • 网站关键词整体方案王通seo赚钱培训
  • 一级a做爰网站下载如何注册网站
  • 茂名网站建设托管推广app大全
  • 企业网站最下面的那栏叫啥武汉seo首页优化技巧
  • 网站建设第一步怎么弄阿里云模板建站
  • 百度网站建设怎么联系全网营销代理加盟
  • 个人网站多少钱小程序开发多少钱
  • 微信开放平台网站应用系统优化大师下载
  • 公众号推广合作平台小红书关键词优化
  • 做英文网站的流程精准获客
  • 网站代码优化目的杭州网站推广大全
  • 网站响应时间长自媒体平台排名前十