谈谈如何利用swing构建一个文本批量替换桌面客户端应用

最早接触swing是很多年的一个ERP开源项目Adempiere,当时就比较惊讶,java居然还能开发桌面客户端。若干年后,有个小工具需要做成桌面客户端,除了donet也就swing可选。因电脑没安装visual studio,无奈只好开始一段苦逼swing之旅。

1、工具简介

lm-txtopr,github地址:https://github.com/zongtong2046/lm-txtopr,界面截图如下:
在这里插入图片描述
在这里插入图片描述

功能描述:比如jsp页面或js文件中引用的组件ZJ_MKT_DIST改名为ZJ_Market,则:

  • 文件过滤符:*.jsp, *.js
  • 查找字符串:ZJ_MKT_DIST
  • 替换字符串:ZJ_Market
  • 点击“全部替换”按钮,即可将指定目录下的所有*.jsp,*.js文件,字符串“ZJ_MKT_DIST”将被替换为"ZJ_Market"
  • 查找、替换结果中,双击列表项,可以打开Dialog框,显示文件具体内容

2、swing组件结构图

1)swing组件结构图

在这里插入图片描述
图片来自百度文库:https://wenku.baidu.com/view/85305bdf6f1aff00bed51e23.html

2)本项目swing组件结构图

一个主界面:FrameMain(Frame),FrameMain内含一个panel组件,文本框、label、复选框、按钮等组件都在panel里,所有组件元素采用决定定位布局;FrameMain内可以打开提示框、文件内容显示Dialog。

FrameMain(JFrame)
	|--JPanel
		|--JLabel, JButton, JTextField, JComboBox, JCheckBox, JList
		
FrameMain可以打开DialogFileContent(JDialog)
DialogFileContent(JDialog)
	|--JPanel	
		|--JButton, JTextField, JTextArea, JScrollPane

3、各组件具体使用

1)JSeparator分割线组件
JSeparator sep = new JSeparator(JSeparator.HORIZONTAL);
sep.setBounds(20, 95, 760, 10);
panel.add(sep);
2)图片按钮
String ICON_QUESTION = "src/assets/question16x16.png";		//注意文件路径
JButton btnIconFile = new JButton(new ImageIcon(ICON_QUESTION));
3)JComboBox,可编辑下拉选择框
JComboBox<String> cmbFile = new JComboBox<String>();
cmbFile.setEditable(true);
//获取当前输入值,如果该值在列表中没有则添加到列表,如果已经存在则移动到第一个
String strItem = cmbFile.getEditor().getItem()!=null ? cmbFile.getEditor().getItem().toString() : "";
if (! StringUtils.isEmpty(strItem)) {
	int count = cmbFile.getItemCount();
	for(int i = 0; i<count; i++) {
		if (strItem.equals(cmbFile.getItemAt(i))) {
			cmbFile.removeItemAt(i);
			break;
		}
	}
	cmbFile.insertItemAt(strItem, 0);
	cmbFile.setSelectedIndex(0);				
}
4)其它组件使用说明,请github clone后查看代码

4、事件使用说明

通常两种处理方式:

  • 使用匿名函数,查找/替换结果列表添加“鼠标click”事件处理
		jList.addMouseListener(new MouseAdapter() {
			@SuppressWarnings({ "unchecked" })
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					String filePath = listModel.getElementAt(((JList<String>)e.getSource()).getSelectedIndex());
					if (filePath.indexOf(". ") > 0) {
						filePath = filePath.substring(filePath.indexOf(". ") + 2);
					}
					dialogFileContent= new DialogFileContent(FrameMain.this);
					dialogFileContent.loadFileContent(filePath);
					dialogFileContent.setVisible(true);
				}
			}
		});
  • 多个组件绑定同一个事件处理函数
btnFindOne.addActionListener(new BtnSrchReplaceHandler());
btnFindAll.addActionListener(new BtnSrchReplaceHandler());
private class BtnSrchReplaceHandler implements ActionListener {
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnFindOne) {	// find one
				oprFlag = oprFlag | FileOprUtils.OPR_FIRST | FileOprUtils.OPR_SRCH;
			} else if (e.getSource() == btnFindAll) {	// find all
				oprFlag = oprFlag | FileOprUtils.OPR_SRCH;
			} else if (e.getSource() == btnReplaceOne) {
				oprFlag = oprFlag | FileOprUtils.OPR_FIRST | FileOprUtils.OPR_REPLACE;
			} else if (e.getSource() == btnReplaceAll) {
				oprFlag = oprFlag | FileOprUtils.OPR_REPLACE;
			}
		}
		...
	}
}

5、后记

关于swing,我一直不认为其是开发首选,业务实践过程中,能用其解决问题即可,不求精通。

发布了294 篇原创文章 · 获赞 98 · 访问量 77万+

猜你喜欢

转载自blog.csdn.net/chuangxin/article/details/99688576