⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 console.java

📁 用于串口通讯测试的工具软件。完全使用java语言编写。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			if (convertorDialog == null)
			{
				convertorDialog = ZCFixPanel.createDialog((Component)e.getSource(), this.getName(), false);
				RadixConvertor contentPane = new RadixConvertor();
				contentPane.setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 5));
				convertorDialog.setContentPane(contentPane);
				convertorDialog.pack();
				convertorDialog.setLocationRelativeTo((Component)e.getSource());
				contentPane.registerKeyboardAction(new QuitAction()
						, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0)
						, JComponent.WHEN_IN_FOCUSED_WINDOW);
			}
			convertorDialog.show();
		}
	}

	/**
	 * @todo 退出窗口事件处理
	 */
	private static class QuitAction extends com.zcsoft.swing.ZCAction
	{

		/**
		 * 事件处理实现
		 * 关闭所有打开了的内部窗体
		 * @param e 事件
		 */
		public void actionPerformed(java.awt.event.ActionEvent e)
		{
			Component c = (Component)e.getSource();
			while (c != null && !(c instanceof Window))
			{
				if (c instanceof JPopupMenu)
				{
					c = ((JPopupMenu)c).getInvoker();
				}
				c = c.getParent();
			}
			if (c != null)
			{
				((Window)c).dispose();
			}
		}
	}

	/** 是否显示工具条之切换菜单的事件实现 */
	private class ShowToolBarListener implements ItemListener
	{
		public void itemStateChanged(ItemEvent e)
		{
			if (((JMenuItem)e.getSource()).isSelected())
			{
				installToolBar();
			}
			else
			{
				Console.this.remove(tb);
				tb = null;
			}
			Console.this.validate();
		}
	}

	/**
	 * @todo 显示关于内容的事件处理
	 */
	private class ShowAboutAction extends com.zcsoft.swing.ZCAction
	{

		private ShowAboutAction()
		{
			super(res.getString("menuItem.about.caption"));
		}

		/**
		 * 事件处理实现
		 * @param e 事件
		 */
		public void actionPerformed(java.awt.event.ActionEvent e)
		{
			JOptionPane.showMessageDialog(Console.this
													, res.getString("about.content")
													, getName()
													, JOptionPane.INFORMATION_MESSAGE);
		}
	}

	/**
	 * @todo 设定工作目录的事件处理
	 */
	private class SetWorkDirAction extends com.zcsoft.swing.ZCAction
	{

		/**
		 * 事件处理实现
		 *
		 * @param e 事件
		 */
		public void actionPerformed(java.awt.event.ActionEvent e)
		{
			JFileChooser fc = new JFileChooser();
			fc.setAcceptAllFileFilterUsed(false);
			fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			File oldDir = new File(System.getProperty("work.dir")).getAbsoluteFile();
			fc.setCurrentDirectory(new File(oldDir.getParent()));
			fc.setSelectedFile(oldDir);

			int cmd = fc.showOpenDialog(Console.this);
			if (cmd == JFileChooser.APPROVE_OPTION)
			{
				File newDir = fc.getSelectedFile().getAbsoluteFile();
				if (!newDir.equals(oldDir))
				{
					System.setProperty("work.dir", newDir.getPath());
				}
			}
		}
	}



	/** @todo 入口 */
	public static void main(String[] args)
	{
//		setWorkDir(args);
		try
		{
			String propertyFileURL = findOptionValue(args, "property.file.url");
			tryCopyJavaCommPropertiesFile(propertyFileURL);
		}
		catch (IOException ex)
		{
			ex.printStackTrace();
			return;
		}
		loadOtherLibaries(args);
		setUI(args);
		showConsole();
	}

//
//	/**
//	 * 将第三方提供的用于串口通信的类文件加入虚拟机的类装在路径
//	 * @param args 可能包含了选项“-cp:a”的命令行参数
//	 * @throws MissingResourceException
//	 */
//	private static void appendClassPath(String[] args)
//	{
//		String classAppended = findOptionValue(args, "-cp:a");
//		if (classAppended == null)
//		{
//			File startupDir = new File(".");
//			File allJars[] = startupDir.listFiles(new java.io.FileFilter()
//					{
//						public boolean accept(File pathname)
//						{
//							return pathname.getName().endsWith(".jar");
//						}
//					});
//			classAppended = "";
//			for (int i = 0; i < allJars.length; i++)
//			{
//				if (i > 0)
//				{
//					classAppended += File.pathSeparator;
//				}
//				classAppended += allJars[i].getName();
//			}
//		}
//		if (classAppended != null)
//		{
//			String newClasspath = System.getProperty("java.class.path")
//							 + File.pathSeparator
//							 + classAppended;
//			System.setProperty("java.class.path", newClasspath);
//		}
//	}

	private static void showConsole()
	{
		Console console1 = new Console();
		try
		{
			console1.setIconImage(Toolkit.getDefaultToolkit().createImage(Console.class.getResource("/images/frameIcon.gif")));
			UIManager.put("InternalFrame.icon", new ImageIcon(console1.getIconImage()));
		}
		catch (Exception ex){}
		console1.show();
	}

	private static void setUI(String args[])
	{
		String uiClass = findOptionValue(args, "ui.class");
		if (uiClass == null)
		{
			uiClass = UIManager.getSystemLookAndFeelClassName();
		}
		try
		{
			UIManager.setLookAndFeel(uiClass);
			UIManager.put("Button.margin", new Insets(1, 5, 1, 5));
			UIManager.put("ToggleButton.margin", new Insets(3, 6, 3, 6));
			UIManager.put("ZCComboBoxUI", UIManager.get("ComboBoxUI"));
		}
		catch (Exception ex)
		{
			System.out.println(JpscToolkit.getFormatedString("startup.invalid.lookAndFeel", ex));
		}
	}

	//在windows上使用gnu.io.RXTXCommDriver时,必须装载win32com.dll本地库文件,
	//不然在javax.comm.CommPortIdentifier调用RXTXCommDriver的initialize方法时,
	//会出现错误:"Caught java.lang.UnsatisfiedLinkError: nSetOwner while loading driver gnu.io.RXTXCommDriver"
	//由此可见RXTXCommDriver所调用的本地库没有实现本地方法:nSetOwner
	private static void loadOtherLibaries(String args[])
	{
		String libraries = findOptionValue(args, "library.patch");
		if (libraries != null)
		{
			StringTokenizer st = new StringTokenizer(libraries, File.pathSeparator.concat(String.valueOf(' ')));
			while (st.hasMoreTokens())
			{
				System.loadLibrary(st.nextToken());
			}
		}
	}

	/**
	 * 找到命令行参数中,指定选项的值。选项以字符'-'开头,其值就是紧跟其后的字符串
	 * @param args 命令行参数列表
	 * @param option 以字符'-'开头的选项
	 * @return 选项对应的值。args没有该选项,或紧跟该选项后的参数是以字符'-'开头的字符串,则返回null。
	 */
	private static String findOptionValue(String[] args, String option)
	{
		String value = null;
		option = '-' + option;
		for (int i = 0; i < args.length; i++)
		{
			String arg = args[i];
			if (option.startsWith(arg))
			{
				int next = i + 1;
				if (next < args.length && args[next].charAt(0) != '-')
				{
					value = args[next];
				}
				break;
			}
		}
		return value;
	}

	/**
	 * 如果客户端JRE目录下没有串口通信配置文件,则从服务器端拷贝一个过来放到相应目录下。
	 * @return 带绝对路径的本地串口通信配置文件对象
	 */
	private static java.io.File tryCopyJavaCommPropertiesFile(String fileURL) throws IOException
	{
		java.io.File commPropertiesFile
				= new File( System.getProperty("java.home") + File.separator
							+ "lib" + File.separator + "javax.comm.properties");
		if (!commPropertiesFile.exists() || commPropertiesFile.length() == 0)
		{//之所以commPropertiesFile.length() = 0,是因为读取源文件时出现异常导致文件已经创建但未写入内容
			java.net.URL source = null;
			if (fileURL == null)
			{
				fileURL = "file:./javax.comm.properties";//从启动目录下找
			}
			try
			{
				source = new java.net.URL(fileURL);
			}
			catch (java.net.MalformedURLException ex)
			{
				System.err.println(ex);
				source = Console.class.getResource(fileURL);
			}
			if (source == null)
			{
				throw new FileNotFoundException("cannot.find.file " + fileURL);
			}
			copyFile(source, commPropertiesFile);
		}
		return commPropertiesFile;
	}

	/**
	 * 将源文件sourceFile拷贝成目标文件destFile.
	 */
	private static void copyFile(java.net.URL sourceFile, File destFile) throws IOException
	{
		java.io.InputStream ins = sourceFile.openStream();
		java.io.FileOutputStream fos = new java.io.FileOutputStream(destFile);
		int c = -1;
		byte[] buffer = new byte[1024];
		while ((c = ins.read(buffer)) > 0)
		{
			fos.write(buffer, 0, c);
		}
		ins.close();
		fos.close();
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -