用Java Swing编写简单的测试小工具界面(源码)

Author:Pirate Leo

前段时间我负责在系统中添加了HBase用于管理海量数据,功能实现后自然到了性能测试阶段,性能、可靠性、稳定性,性性都不能少。

因此就有了个内部的小需求:“给测试部提供一个工具,可以向HBase写入数据,以达到测试所需的数据量”。

由于我们的系统是由Java编写,Client的代码也用的Hadoop的Java API,因此小工具的界面编写理所应当的选择了Java Swing。

之前没有用过Swing,本次完全是在网上一边翻博一边编码。

myBlog:http://blog.csdn.net/pirateleo/

myEmail:[email protected]

转载请注明出处,谢谢。

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的ContentPane
		Container contentPane = getContentPane();
		
		GridBagLayout gridbag = new GridBagLayout();
		contentPane.setLayout(gridbag);
		GridBagConstraints c = new GridBagConstraints();
		
		// Title
		welcomeLbl = 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);
		
		// IP
		ipLbl = 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;
	}
	
	@Override
	public 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 here
				Thread.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编写简单的测试小工具界面(源码))