java swing程序,Ctrl+S 保存配置文件如何实现呢?

首先增加全局快捷键:

 /***
		 * 增加全局快捷键.<Br>
		 * Ctrl+S,保存参数到配置文件
		 */
		private void setGlobalShortCuts() {
			// Add global shortcuts
			Toolkit toolkit = Toolkit.getDefaultToolkit();
			// 注册应用程序全局键盘事件, 所有的键盘事件都会被此事件监听器处理.
			toolkit.addAWTEventListener(new java.awt.event.AWTEventListener() {
				public void eventDispatched(AWTEvent event) {
					if (event.getClass() == KeyEvent.class) {
						KeyEvent kE = ((KeyEvent) event);
						// 处理按键事件 Ctrl+S
						if (kE.getKeyCode() == KeyEvent.VK_S
								&& kE.isControlDown()
								&& kE.getID() == KeyEvent.KEY_PRESSED) {
//							System.out.println("save");
							saveConfig();
						} 
					}
				}
			}, java.awt.AWTEvent.KEY_EVENT_MASK);

		}
		
		/***
		 * 保存到配置文件中
		 */
		private void saveConfig(){
			File configFile=new File(configFilePath);
			if(!configFile.exists()){
				try {
					SystemHWUtil.createEmptyFile(configFile);
				} catch (IOException e) {
					e.printStackTrace();
					GUIUtil23.errorDialog(e);
				}
			}
			String content=String.format(SAVE_CONFIG_TEMP, ipTextField.getText(),portTextField.getText(),topicTextField.getText());
			CMDUtil.show(configFilePath);//因为隐藏文件是只读的
			FileUtils.writeToFile(configFilePath, content);
//			CMDUtil.executeCmd("attrib "+configFilePath+" +H");
			CMDUtil.hide(configFilePath);
		}

程序启动时读取配置文件

private void readConfig() throws IOException{
//		GenericReadPropsUtil propUtil=new GenericReadPropsUtil();
		Properties prop= GenericReadPropsUtil.getProperties(!isInjar, CONF_PATH);
		/***
		 * 从系统盘目录下读取配置文件
		 */
		Properties prop2= GenericReadPropsUtil.getProperties(false, configFilePath);
		String serverIp22=null;
		String port22=null;
		String topic22=null;
		if(!ValueWidget.isNullOrEmpty(prop2)){
		serverIp22=prop2.getProperty("ip");
		port22=prop2.getProperty("port");
		topic22=prop2.getProperty("topic");
		}
//		if(ValueWidget.isNullOrEmpty(prop)){
//			prop= GenericReadPropsUtil.getProperties(false, CONF_PATH);
//		}
		if(ValueWidget.isNullOrEmpty(prop)&&ValueWidget.isNullOrEmpty(prop2)){
			return;
		}
		String key2 = "selected_index";
		if(ValueWidget.isNullOrEmpty(prop)){
			selectedIndex=0;
		}else{
			selectedIndex=Integer.parseInt(prop.getProperty(key2));
		}

		String propValue=null;
		if(ValueWidget.isNullOrEmpty(serverIp22)){
		
		key2 = KEY_PROP_ACTIVEMQ_IP;
		propValue = prop.getProperty(key2);
		if (propValue != null ) {
			this.ipTextField.setText(propValue);
		}}else{
			this.ipTextField.setText(serverIp22);
		}
		if(ValueWidget.isNullOrEmpty(port22)){
		key2 = KEY_PROP_ACTIVEMQ_MQTT_PORT;
		propValue = prop.getProperty(key2);
		if (propValue != null ) {
			this.portTextField.setText(propValue);
		}
		}else{
			this.portTextField.setText(port22);
		}
		
		if(ValueWidget.isNullOrEmpty(topic22)){
		key2 = KEY_PROP_ACTIVEMQ_TOPIC;
		propValue = prop.getProperty(key2);
		if (propValue != null ) {
			this.topicTextField.setText(propValue);
		}
		}else{
			this.topicTextField.setText(topic22);
		}
		
		key2 = KEY_PROP_ACTIVEMQ_CLIENT_ID;
		if(ValueWidget.isNullOrEmpty(prop)){
			
		}else{
		propValue = prop.getProperty(key2);
		}
		if (propValue != null ) {
			this.clientIdTextField.setText(propValue);
		}
		
	}
	public static final String configFilePath="C:\\.mqtt_client.properties";


注意:隐藏文件是只读的,所以写入文件之前先执行CMDUtil.show(configFilePath)

程序运行状态:

wKioL1SqX-eT3tsIAAFI89k_nDI248.jpg

项目使用maven 构建

源码见附件