标签:
源码文件2:Launcher.java package com.teleca.robin; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.SwingConstants; public class Launcher extends JFrame implements WindowListener{ private long interval; private String executableFileName; Launcher(String file,long time) { if(file==null) executableFileName=""; else executableFileName=file; interval=time; initComponents(); this.addWindowListener(this); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jButtonStart = new javax.swing.JButton(); jButtonPause = new javax.swing.JButton(); jButtonExit = new javax.swing.JButton(); jTextFieldFile = new javax.swing.JTextField(20); jTextFieldTime = new javax.swing.JTextField(5); jLabelFile = new javax.swing.JLabel(); jLabelTime = new javax.swing.JLabel(); jLabelTip = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setName("Form"); // NOI18N jButtonStart.setText("Start"); // NOI18N jButtonStart.setName("jButtonStart"); // NOI18N jButtonStart.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { start(); } }); jButtonPause.setText("Pause"); // NOI18N jButtonPause.setName("jButtonPause"); // NOI18N jButtonPause.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { pause(); } }); jButtonExit.setText("Exit"); // NOI18N jButtonExit.setName("jButtonExit"); // NOI18N jButtonExit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { stop(); System.exit(0); } }); jButtonStart.setEnabled(true); jButtonPause.setEnabled(false); jTextFieldFile.setText(executableFileName); // NOI18N jTextFieldFile.setName("jTextFieldFile"); // NOI18N jTextFieldFile.setHorizontalAlignment(JTextField.CENTER); jTextFieldTime.setText(""+interval); // NOI18N jTextFieldTime.setName("jTextFieldTime"); // NOI18N jTextFieldTime.setHorizontalAlignment(JTextField.CENTER); jLabelFile.setText("the executable file"); // NOI18N jLabelFile.setName("jLabelFile"); // NOI18N jLabelTime.setText("the interval time(millisecond)"); // NOI18N jLabelTime.setName("jLabelTime"); // NOI18N jLabelTip.setText("idle"); // NOI18N jLabelTip.setName("jLabelTip"); // NOI18N jLabelTip.setHorizontalAlignment(SwingConstants.CENTER); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(71, 71, 71) .addComponent(jLabelTime)) .addGroup(layout.createSequentialGroup() .addGap(70, 70, 70) .addComponent(jLabelFile)) .addGroup(layout.createSequentialGroup() .addGap(90, 90, 90) .addComponent(jButtonStart))) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(100, 100, 100) .addComponent(jTextFieldTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addComponent(jTextFieldFile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) ) .addGroup(layout.createSequentialGroup() .addGap(10, 10, 10) .addComponent(jButtonPause) ) .addGroup(layout.createSequentialGroup() .addGap(20, 20, 20) .addComponent(jLabelTip) ) .addGroup(layout.createSequentialGroup() .addGap(180, 180, 180) .addComponent(jButtonExit) ) )) ) .addContainerGap(151, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(63, 63, 63) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabelFile) .addComponent(jTextFieldFile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(29, 29, 29) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabelTime) .addComponent(jTextFieldTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(62, 62, 62) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButtonStart) .addComponent(jButtonPause) .addComponent(jButtonExit)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE) .addComponent(jLabelTip) .addGap(32, 32, 32)) ); pack(); }// </editor-fold>//GEN-END:initComponents Worker worker; private void stop() {//GEN-FIRST:event_stop // TODO add your handling code here: if(worker!=null) { worker.die(); worker=null; } }//GEN-LAST:event_stop private void pause() {//GEN-FIRST:event_pause // TODO add your handling code here: worker.doPause(); jButtonStart.setEnabled(true); jButtonPause.setEnabled(false); jTextFieldFile.setEditable(true); jTextFieldTime.setEditable(true); }//GEN-LAST:event_pause private void start() {//GEN-FIRST:event_start // TODO add your handling code here: String fileName=jTextFieldFile.getText().trim(); if(checkFileName(fileName)) { executableFileName=fileName; } else { return; } String time=jTextFieldTime.getText(); if(time.length()==0) { interval=defaultInterval; } else { try{ long t=Long.parseLong(time); interval=t; }catch(NumberFormatException e) { jLabelTip.setText("the time "+time+"is not correct!"); return; } } if(worker==null) { worker=new Worker(this.jLabelTip); worker.setExecutableFileName(executableFileName); worker.setInterval(interval); worker.start(); jLabelTip.setText("start to execute file"); } else { worker.setExecutableFileName(executableFileName); worker.setInterval(interval); worker.doResume(); jLabelTip.setText("Restart to execute file"); } jButtonStart.setEnabled(false); jButtonPause.setEnabled(true); jTextFieldFile.setEditable(false); jTextFieldTime.setEditable(false); }//GEN-LAST:event_start final static long defaultInterval=1000; /** * @param args */ public static void main(String[] args) { String fileName=""; long time=defaultInterval; if(args.length>0) { fileName=args[0]; if(!checkFileName(fileName)) { fileName=""; } } if(args.length>1) { time=Long.parseLong(args[1]); } // TODO Auto-generated method stub new Launcher(fileName,time).setVisible(true); } static boolean checkFileName(String fileName) { if(fileName==null) { return false; } if(!(fileName.endsWith(".exe")||fileName.endsWith(".bat")||fileName.endsWith(".cmd"))) { System.out.println("the file must be a executable file!"); System.out.println("Now only support *.exe , *.bat,*.cmd"); System.out.println("Please check the file again"); return false; } File file=new File(fileName); if(!file.exists()) { System.out.println("the file:"+fileName+" is not exists"); return false; } return true; } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButtonExit; private javax.swing.JButton jButtonPause; private javax.swing.JButton jButtonStart; private javax.swing.JLabel jLabelFile; private javax.swing.JLabel jLabelTime; private javax.swing.JLabel jLabelTip; private javax.swing.JTextField jTextFieldFile; private javax.swing.JTextField jTextFieldTime; // End of variables declaration//GEN-END:variables public void windowDeactivated(WindowEvent e) { //System.out.println("window is deactivated"); } public void windowDeiconified(WindowEvent e) { //System.out.println("window is Deiconified"); } public void windowActivated(WindowEvent e) { //System.out.println("window is actived"); } public void windowOpened(WindowEvent e) { //System.out.println("window is Opened"); } public void windowClosing(WindowEvent e) { stop(); } public void windowClosed(WindowEvent e) { //System.out.println("window is Closed"); } public void windowIconified(WindowEvent e) { //System.out.println("window is Closing"); } }
标签:
原文地址:http://www.cnblogs.com/Free-Thinker/p/4580682.html