用Java Swing编写简单的测试小工具界面(源码)
来源:互联网 发布:《鸟哥的linux私房菜》 编辑:程序博客网 时间:2024/06/05 23:42
Author:Pirate Leo
前段时间我负责在系统中添加了HBase用于管理海量数据,功能实现后自然到了性能测试阶段,性能、可靠性、稳定性,性性都不能少。
因此就有了个内部的小需求:“给测试部提供一个工具,可以向HBase写入数据,以达到测试所需的数据量”。
由于我们的系统是由Java编写,Client的代码也用的Hadoop的Java API,因此小工具的界面编写理所应当的选择了Java Swing。
之前没有用过Swing,本次完全是在网上一边翻博一边编码。
myBlog: http://blog.csdn.net/pirateleo/
myEmail: codeevoship@gmail.com
转载请注明出处,谢谢。
Begin!首先是成品图:
OK,我知道很丑,咱不就图个实用么……
这里可以输入Hadoop集群的IP,可以选择需要操作的表,可以清空表中的记录,还有写入耗时统计。
下面是代码,代码分为两个文件,一个是MainView负责界面展示;另一个是Worker线程,负责干活和刷新进度条与显示数据。
MainView代码:
package com.codeevoship.gui;import java.awt.Container;import java.awt.Font;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.Insets;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JProgressBar;import javax.swing.JRadioButton;import javax.swing.JTextField;@SuppressWarnings("serial")public class MainWindow extends JFrame {private JLabel welcomeLbl;private JLabel ipLbl;private JTextField ipTxt;private JLabel numLbl;private JTextField numTxt;private JProgressBar probar;private JTextField txtResultShow;private JRadioButton archiveSel;private JRadioButton contentSel;private JRadioButton systemLogSel;private JRadioButton clearDataSel;private JButton startBtn;private JButton exitBtn;private Thread thdWorker;public MainWindow() {// 获取本JFrame的ContentPaneContainer contentPane = getContentPane();GridBagLayout gridbag = new GridBagLayout();contentPane.setLayout(gridbag);GridBagConstraints c = new GridBagConstraints();// TitlewelcomeLbl = new JLabel("CodeEvoShip HBase 数据填充工具");welcomeLbl.setFont(new Font("Serif",Font.BOLD,20));c = getGridBagConstraints(0, 0, 0, 1, 0, 0,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(10, 0, 0, 0), 10, 0);gridbag.setConstraints(welcomeLbl, c);contentPane.add(welcomeLbl);// IPipLbl = new JLabel("Cluster IP:");c = getGridBagConstraints(0, 1, 1, 1, 0, 0,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10, 0, 10, 0), 10, 0);gridbag.setConstraints(ipLbl, c);contentPane.add(ipLbl);ipTxt = new JTextField("127.0.0.1;127.0.0.2;127.0.0.3");c = getGridBagConstraints(1, 1, 0, 1, 0, 0,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10, 0, 10, 0), 85, 0);gridbag.setConstraints(ipTxt, c);contentPane.add(ipTxt);// 数量填写numLbl = new JLabel("Write Number:");c = getGridBagConstraints(0, 2, 1, 1, 0, 0,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10, 0, 10, 0), 10, 0);gridbag.setConstraints(numLbl, c);contentPane.add(numLbl);numTxt = new JTextField("10000");c = getGridBagConstraints(1, 2, 0, 1, 0, 0,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10, 0, 10, 0), 230, 0);gridbag.setConstraints(numTxt, c);contentPane.add(numTxt);// 多选框archiveSel = new JRadioButton("Archive Table");c = getGridBagConstraints(0, 3, 1, 1, 0, 0,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0);gridbag.setConstraints(archiveSel, c);contentPane.add(archiveSel);contentSel = new JRadioButton("Content Table");c = getGridBagConstraints(1, 3, 1, 1, 0, 0,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0);gridbag.setConstraints(contentSel, c);contentPane.add(contentSel);systemLogSel = new JRadioButton("SystemLog Table");c = getGridBagConstraints(2, 3, 1, 1, 0, 0,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 5, 0), 0, 0);gridbag.setConstraints(systemLogSel, c);contentPane.add(systemLogSel);clearDataSel = new JRadioButton("Clear old data of selected Tables!!");c = getGridBagConstraints(0, 4, 0, 1, 0, 0,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 15, 0), 0, 0);gridbag.setConstraints(clearDataSel, c);contentPane.add(clearDataSel);// 进度条probar = new JProgressBar(0, 100);probar.setStringPainted(true);c = getGridBagConstraints(0, 6, 0, 1, 0, 0,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 250, 0);gridbag.setConstraints(probar, c);contentPane.add(probar);txtResultShow = new JTextField("Not Begin");txtResultShow.setEditable(false);txtResultShow.setHorizontalAlignment(JTextField.CENTER);c = getGridBagConstraints(0, 7, 0, 1, 0, 0,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 5, 0), 325, 0);gridbag.setConstraints(txtResultShow, c);contentPane.add(txtResultShow);//执行与退出startBtn = new JButton("Start");c = getGridBagConstraints(0, 5, 0, 1, 0, 0,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 5, 0), 50, 0);gridbag.setConstraints(startBtn, c);startBtn.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {if (!paraPassCheck()) {return;}if (null != thdWorker && thdWorker.isAlive()) {JOptionPane.showMessageDialog(null,"There already is a task running now, please wait");return;}DoWork doWork = new DoWork(ipTxt.getText(),Long.parseLong(numTxt.getText()),contentSel.isSelected(),archiveSel.isSelected(),systemLogSel.isSelected(),clearDataSel.isSelected(),probar, txtResultShow);thdWorker = new Thread(doWork);thdWorker.start();}});contentPane.add(startBtn);exitBtn = new JButton("Exit");c = getGridBagConstraints(1, 5, 0, 1, 0, 0,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 5, 0), 50, 0);gridbag.setConstraints(exitBtn, c);exitBtn.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {System.exit(0);}});contentPane.add(exitBtn);}private boolean paraPassCheck() {try {Long.parseLong(numTxt.getText());return true;} catch (Exception e) {JOptionPane.showMessageDialog(null,"Invalid number ");return false;}}private static GridBagConstraints getGridBagConstraints(int gridx,int gridy, int gridwidth, int gridheight, double weightx,double weighty, int anchor, int fill, Insets insets, int ipadx,int ipady) {return new GridBagConstraints(gridx, gridy, gridwidth, gridheight,weightx, weighty, anchor, fill, insets, ipadx, ipady);}public static void main(String args[]) {MainWindow mainWindow = new MainWindow();mainWindow.setTitle("CodeEvoShip");mainWindow.pack();mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);mainWindow.setVisible(true);}}
Worker代码:
package com.codeevoship.gui;import javax.swing.JProgressBar;import javax.swing.JTextField;public class DoWork implements Runnable {JProgressBar probar;JTextField curTxtShow;long totalNum = 0;long curNum = 0;long startTime = 0;long curTime = 0;public DoWork(String ip, long num, boolean isWriteCon, boolean isWriteArc, boolean isWriteSys, boolean isClearData, JProgressBar probar, JTextField curTxtShow) {System.out.println("IP:" + ip + ", Num:" + num);System.out.println("Write Content[" + isWriteCon + "], Write Archive[" + isWriteArc + "], Write SystemLog[" + isWriteSys + "].");startTime = 0;curNum = 0;totalNum = num;this.probar = probar;this.curTxtShow = curTxtShow;}@Overridepublic void run() {startTime = System.currentTimeMillis();// 开始工作System.out.println("Do Work Start!");StringBuilder sb = new StringBuilder();while (true) {if (curNum >= totalNum) {break;}try {// TODO: Real work hereThread.sleep(10);// 更新界面显示curNum += 1;probar.setValue((int) (curNum*100/totalNum));curTime = System.currentTimeMillis();sb.setLength(0);sb.append(curNum);sb.append('/');sb.append(totalNum);sb.append(" cost time: ");sb.append((curTime - startTime)/1000);sb.append('s');curTxtShow.setText(sb.toString());} catch (Exception e) {e.printStackTrace();}}System.out.println("Do Work End!");}}
大家如需要可随意使用,改成自己的小工具,只要把Worker中的// TODO: 改为实际运行的程序即可。
- 用Java Swing编写简单的测试小工具界面(源码)
- 用Java Swing编写简单的测试小工具界面(源码)
- Java Swing编写小工具图形化界面
- 一个简单的猴子测试小工具
- Java-Swing写的一个定时关机的小工具
- 用Java swing 编写简单的计算器
- Java Swing实用小工具开发
- “进程监控器”小工具的编写(包含整个项目源码)
- 【小工具】简单的倒计时(原生)
- 用swing写的整合两个Excel表小工具 阶段(一)
- Python编写串口测试小工具
- java的Applet编写的日历小工具
- 基于java GUI编写的身份证查询小工具
- 用Java编写获取文件MD5小工具
- 取证的简单小工具
- 简单文本除重小工具源码
- Java正则表达式学习-简单的小工具
- java--自己的小工具
- CSS中行内元素与块级元素
- 第三十一,swtich是否能作用在byte上,是否能作用在long上,是否能作用在String上?
- 获取表格的绝对位置
- 第十一章 异常处理
- 已知二叉树的中序遍历和后序遍历,如何求前序遍历
- 用Java Swing编写简单的测试小工具界面(源码)
- tcp短连接TIME_WAIT问题解决方法大全(4)——tcp_tw_reuse
- 系统命令跟踪命令工具
- 直方图之calcHist使用 .
- 如运行程序提示:“没有找到libmysql.dll,因此这个应用程序未能启动。重新安装应用程序可能会修复此问题。
- Long也是整数,为什么不能传给SWITCH呢?
- tcp短连接TIME_WAIT问题解决方法大全(5)——tcp_max_tw_buckets
- typedef的用法
- 第三十三 Hashtable和HashMap