歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

用Java Swing編寫簡單的測試小工具界面(源碼)

前段時間我負責在系統中添加了HBase用於管理海量數據,功能實現後自然到了性能測試階段,性能、可靠性、穩定性,性性都不能少。

因此就有了個內部的小需求:“給測試部提供一個工具,可以向HBase寫入數據,以達到測試所需的數據量”。

由於我們的系統是由Java編寫,Client的代碼也用的Hadoop的Java API,因此小工具的界面編寫理所應當的選擇了Java Swing。

之前沒有用過Swing,本次完全是在網上一邊翻博一邊編碼。

首先是成品圖:

OK,我知道很丑,咱不就圖個實用麼……

這裡可以輸入Hadoop集群的IP,可以選擇需要操作的表,可以清空表中的記錄,還有寫入耗時統計。

下面是代碼,代碼分為兩個文件,一個是MainView負責界面展示;另一個是Worker線程,負責干活和刷新進度條與顯示數據。

MainView代碼:

  1. package com.codeevoship.gui;
  2. import java.awt.Container;
  3. import java.awt.Font;
  4. import java.awt.GridBagConstraints;
  5. import java.awt.GridBagLayout;
  6. import java.awt.Insets;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import javax.swing.JButton;
  10. import javax.swing.JFrame;
  11. import javax.swing.JLabel;
  12. import javax.swing.JOptionPane;
  13. import javax.swing.JProgressBar;
  14. import javax.swing.JRadioButton;
  15. import javax.swing.JTextField;
  16. @SuppressWarnings("serial")
  17. publicclass MainWindow extends JFrame {
  18. private JLabel welcomeLbl;
  19. private JLabel ipLbl;
  20. private JTextField ipTxt;
  21. private JLabel numLbl;
  22. private JTextField numTxt;
  23. private JProgressBar probar;
  24. private JTextField txtResultShow;
  25. private JRadioButton archiveSel;
  26. private JRadioButton contentSel;
  27. private JRadioButton systemLogSel;
  28. private JRadioButton clearDataSel;
  29. private JButton startBtn;
  30. private JButton exitBtn;
  31. private Thread thdWorker;
  32. public MainWindow() {
  33. // 獲取本JFrame的ContentPane
  34. Container contentPane = getContentPane();
  35. GridBagLayout gridbag = new GridBagLayout();
  36. contentPane.setLayout(gridbag);
  37. GridBagConstraints c = new GridBagConstraints();
  38. // Title
  39. welcomeLbl = new JLabel("CodeEvoShip HBase 數據填充工具");
  40. welcomeLbl.setFont(new Font("Serif",Font.BOLD,20));
  41. c = getGridBagConstraints(0, 0, 0, 1, 0, 0,
  42. GridBagConstraints.CENTER, GridBagConstraints.NONE,
  43. new Insets(10, 0, 0, 0), 10, 0);
  44. gridbag.setConstraints(welcomeLbl, c);
  45. contentPane.add(welcomeLbl);
  46. // IP
  47. ipLbl = new JLabel("Cluster IP:");
  48. c = getGridBagConstraints(0, 1, 1, 1, 0, 0,
  49. GridBagConstraints.WEST, GridBagConstraints.NONE,
  50. new Insets(10, 0, 10, 0), 10, 0);
  51. gridbag.setConstraints(ipLbl, c);
  52. contentPane.add(ipLbl);
  53. ipTxt = new JTextField("127.0.0.1;127.0.0.2;127.0.0.3");
  54. c = getGridBagConstraints(1, 1, 0, 1, 0, 0,
  55. GridBagConstraints.WEST, GridBagConstraints.NONE,
  56. new Insets(10, 0, 10, 0), 85, 0);
  57. gridbag.setConstraints(ipTxt, c);
  58. contentPane.add(ipTxt);
  59. // 數量填寫
  60. numLbl = new JLabel("Write Number:");
  61. c = getGridBagConstraints(0, 2, 1, 1, 0, 0,
  62. GridBagConstraints.WEST, GridBagConstraints.NONE,
  63. new Insets(10, 0, 10, 0), 10, 0);
  64. gridbag.setConstraints(numLbl, c);
  65. contentPane.add(numLbl);
  66. numTxt = new JTextField("10000");
  67. c = getGridBagConstraints(1, 2, 0, 1, 0, 0,
  68. GridBagConstraints.WEST, GridBagConstraints.NONE,
  69. new Insets(10, 0, 10, 0), 230, 0);
  70. gridbag.setConstraints(numTxt, c);
  71. contentPane.add(numTxt);
  72. // 多選框
  73. archiveSel = new JRadioButton("Archive Table");
  74. c = getGridBagConstraints(0, 3, 1, 1, 0, 0,
  75. GridBagConstraints.WEST, GridBagConstraints.NONE,
  76. new Insets(0, 0, 0, 0), 0, 0);
  77. gridbag.setConstraints(archiveSel, c);
  78. contentPane.add(archiveSel);
  79. contentSel = new JRadioButton("Content Table");
  80. c = getGridBagConstraints(1, 3, 1, 1, 0, 0,
  81. GridBagConstraints.WEST, GridBagConstraints.NONE,
  82. new Insets(0, 0, 0, 0), 0, 0);
  83. gridbag.setConstraints(contentSel, c);
  84. contentPane.add(contentSel);
  85. systemLogSel = new JRadioButton("SystemLog Table");
  86. c = getGridBagConstraints(2, 3, 1, 1, 0, 0,
  87. GridBagConstraints.WEST, GridBagConstraints.NONE,
  88. new Insets(0, 0, 5, 0), 0, 0);
  89. gridbag.setConstraints(systemLogSel, c);
  90. contentPane.add(systemLogSel);
  91. clearDataSel = new JRadioButton("Clear old data of selected Tables!!");
  92. c = getGridBagConstraints(0, 4, 0, 1, 0, 0,
  93. GridBagConstraints.WEST, GridBagConstraints.NONE,
  94. new Insets(0, 0, 15, 0), 0, 0);
  95. gridbag.setConstraints(clearDataSel, c);
  96. contentPane.add(clearDataSel);
  97. // 進度條
  98. probar = new JProgressBar(0, 100);
  99. probar.setStringPainted(true);
  100. c = getGridBagConstraints(0, 6, 0, 1, 0, 0,
  101. GridBagConstraints.WEST, GridBagConstraints.NONE,
  102. new Insets(5, 5, 5, 5), 250, 0);
  103. gridbag.setConstraints(probar, c);
  104. contentPane.add(probar);
  105. txtResultShow = new JTextField("Not Begin");
  106. txtResultShow.setEditable(false);
  107. txtResultShow.setHorizontalAlignment(JTextField.CENTER);
  108. c = getGridBagConstraints(0, 7, 0, 1, 0, 0,
  109. GridBagConstraints.WEST, GridBagConstraints.NONE,
  110. new Insets(0, 0, 5, 0), 325, 0);
  111. gridbag.setConstraints(txtResultShow, c);
  112. contentPane.add(txtResultShow);
  113. //執行與退出
  114. startBtn = new JButton("Start");
  115. c = getGridBagConstraints(0, 5, 0, 1, 0, 0,
  116. GridBagConstraints.WEST, GridBagConstraints.NONE,
  117. new Insets(0, 0, 5, 0), 50, 0);
  118. gridbag.setConstraints(startBtn, c);
  119. startBtn.addActionListener(new ActionListener() {
  120. publicvoid actionPerformed(ActionEvent arg0) {
  121. if (!paraPassCheck()) {
  122. return;
  123. }
  124. if (null != thdWorker && thdWorker.isAlive()) {
  125. JOptionPane.showMessageDialog(null,"There already is a task running now, please wait");
  126. return;
  127. }
  128. DoWork doWork = new DoWork(ipTxt.getText(),Long.parseLong(numTxt.getText()),
  129. contentSel.isSelected(),archiveSel.isSelected(),
  130. systemLogSel.isSelected(),clearDataSel.isSelected(),
  131. probar, txtResultShow);
  132. thdWorker = new Thread(doWork);
  133. thdWorker.start();
  134. }
  135. });
  136. contentPane.add(startBtn);
  137. exitBtn = new JButton("Exit");
  138. c = getGridBagConstraints(1, 5, 0, 1, 0, 0,
  139. GridBagConstraints.EAST, GridBagConstraints.NONE,
  140. new Insets(0, 0, 5, 0), 50, 0);
  141. gridbag.setConstraints(exitBtn, c);
  142. exitBtn.addActionListener(new ActionListener() {
  143. publicvoid actionPerformed(ActionEvent arg0) {
  144. System.exit(0);
  145. }
  146. });
  147. contentPane.add(exitBtn);
  148. }
  149. privateboolean paraPassCheck() {
  150. try {
  151. Long.parseLong(numTxt.getText());
  152. returntrue;
  153. } catch (Exception e) {
  154. JOptionPane.showMessageDialog(null,"Invalid number ");
  155. returnfalse;
  156. }
  157. }
  158. privatestatic GridBagConstraints getGridBagConstraints(int gridx,
  159. int gridy, int gridwidth, int gridheight, double weightx,
  160. double weighty, int anchor, int fill, Insets insets, int ipadx,
  161. int ipady) {
  162. returnnew GridBagConstraints(gridx, gridy, gridwidth, gridheight,
  163. weightx, weighty, anchor, fill, insets, ipadx, ipady);
  164. }
  165. publicstaticvoid main(String args[]) {
  166. MainWindow mainWindow = new MainWindow();
  167. mainWindow.setTitle("CodeEvoShip");
  168. mainWindow.pack();
  169. mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  170. mainWindow.setVisible(true);
  171. }
  172. }

Worker代碼:

  1. package com.codeevoship.gui;
  2. import javax.swing.JProgressBar;
  3. import javax.swing.JTextField;
  4. publicclass DoWork implements Runnable {
  5. JProgressBar probar;
  6. JTextField curTxtShow;
  7. long totalNum = 0;
  8. long curNum = 0;
  9. long startTime = 0;
  10. long curTime = 0;
  11. public DoWork(String ip, long num, boolean isWriteCon, boolean isWriteArc, boolean isWriteSys, boolean isClearData, JProgressBar probar, JTextField curTxtShow) {
  12. System.out.println("IP:" + ip + ", Num:" + num);
  13. System.out.println("Write Content[" + isWriteCon + "], Write Archive[" + isWriteArc + "], Write SystemLog[" + isWriteSys + "].");
  14. startTime = 0;
  15. curNum = 0;
  16. totalNum = num;
  17. this.probar = probar;
  18. this.curTxtShow = curTxtShow;
  19. }
  20. @Override
  21. publicvoid run() {
  22. startTime = System.currentTimeMillis();
  23. // 開始工作
  24. System.out.println("Do Work Start!");
  25. StringBuilder sb = new StringBuilder();
  26. while (true) {
  27. if (curNum >= totalNum) {
  28. break;
  29. }
  30. try {
  31. // TODO: Real work here
  32. Thread.sleep(10);
  33. // 更新界面顯示
  34. curNum += 1;
  35. probar.setValue((int) (curNum*100/totalNum));
  36. curTime = System.currentTimeMillis();
  37. sb.setLength(0);
  38. sb.append(curNum);
  39. sb.append('/');
  40. sb.append(totalNum);
  41. sb.append(" cost time: ");
  42. sb.append((curTime - startTime)/1000);
  43. sb.append('s');
  44. curTxtShow.setText(sb.toString());
  45. } catch (Exception e) {
  46. e.printStackTrace();
  47. }
  48. }
  49. System.out.println("Do Work End!");
  50. }
  51. }

大家如需要可隨意使用,改成自己的小工具,只要把Worker中的// TODO: 改為實際運行的程序即可。

Copyright © Linux教程網 All Rights Reserved