evgeniyosipov

SimpleBench.java

Dec 27th, 2014
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.30 KB | None | 0 0
  1. import javax.swing.JMenu;
  2. import javax.swing.JFrame;
  3. import javax.swing.JButton;
  4. import javax.swing.JOptionPane;
  5. import javax.swing.JMenuBar;
  6. import javax.swing.JTextField;
  7. import javax.swing.AbstractAction;
  8.  
  9. import java.awt.GridLayout;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12.  
  13. public class SimpleBench extends JFrame implements ActionListener, Runnable {
  14.  
  15.     public static void main(String[] args) {
  16.  
  17.         SimpleBench jframe = new SimpleBench();
  18.         jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19.         jframe.setVisible(true);
  20.         jframe.setResizable(false);
  21.  
  22.     }
  23.  
  24.     SimpleBench() {
  25.  
  26.         setTitle("Простой Java Benchmark");
  27.         setSize(400, 300);
  28.  
  29.         GridLayout gl = new GridLayout(2, 1);
  30.         setLayout(gl);
  31.  
  32.         JMenu fileMenu = new JMenu("Файл");
  33.         JMenu aboutMenu = new JMenu("Информация");
  34.  
  35.         fileMenu.add(new AbstractAction("Выход (Alt + F4)") {
  36.             @Override
  37.             public void actionPerformed(ActionEvent event) {
  38.                 System.exit(0);
  39.             }
  40.         });
  41.  
  42.         aboutMenu.add(new AbstractAction("Об Авторе") {
  43.             @Override
  44.             public void actionPerformed(ActionEvent event) {
  45.                 JOptionPane
  46.                         .showConfirmDialog(
  47.                                 null,
  48.                                 "Евгений Осипов, 2012 год. \n"
  49.                                         + "[email protected], www.fastandclever.ru",
  50.                                 "Об Авторе", JOptionPane.PLAIN_MESSAGE,
  51.                                 JOptionPane.PLAIN_MESSAGE);
  52.             }
  53.         });
  54.  
  55.         jb = new JButton("Начать Benchmark!");
  56.         jb.addActionListener(this);
  57.  
  58.         jtf = new JTextField();
  59.         jtf.setEditable(false);
  60.  
  61.         JMenuBar menuBar = new JMenuBar();
  62.         setJMenuBar(menuBar);
  63.  
  64.         menuBar.add(fileMenu);
  65.         menuBar.add(aboutMenu);
  66.  
  67.         add(jb);
  68.         add(jtf);
  69.  
  70.     }
  71.  
  72.     @Override
  73.     public void actionPerformed(ActionEvent e) {
  74.  
  75.         thd = new Thread(this);
  76.         thd.start();
  77.  
  78.     }
  79.  
  80.     @Override
  81.     public void run() {
  82.  
  83.         jb.setEnabled(false);
  84.  
  85.         int i;
  86.  
  87.         double epochS = System.currentTimeMillis();
  88.  
  89.         for (i = 0; i < 10000000; i++) {
  90.             jb.setText("(0-9999999): " + i);
  91.  
  92.         }
  93.         double epochF = System.currentTimeMillis();
  94.         double resms = epochF - epochS;
  95.         double ress = resms / 1000;
  96.  
  97.         jtf.setText(i + " tics // " + resms + " ms ~ " + ress + " s");
  98.         jb.setEnabled(true);
  99.  
  100.     }
  101.  
  102.     private Thread thd = null;
  103.     private JButton jb = null;
  104.     private JTextField jtf = null;
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment