自己写的java界面程序,不足之处望指导:
tableModel:
package java_guard_M;
import java.util.List;
import javax.swing.table.AbstractTableModel;
public class tableModel extends AbstractTableModel {
List YX_list;
String[] n = {"时间", "软件名称", "软件路径"};
public tableModel(List YX_list) {
this.YX_list = YX_list;
}
public int getColumnCount() {
return n.length;
}
public int getRowCount() {
return YX_list.size();
}
public String getColumnName(int col) {
return n[col];
}
public Object getValueAt(int row, int col) {
List YX_list_sun = (List) YX_list.get(row);
Object value = YX_list_sun.get(col);
return value;
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
}
JFileChooserDemo:
package java_guard_M;
import java.awt.Dimension;
import java.io.File;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class JFileChooserDemo {
public JFileChooserDemo() {
try {
// 是windows
if (System.getProperty("os.name").toUpperCase().indexOf("WINDOWS") != -1) {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException | UnsupportedLookAndFeelException e) {
System.out.println("设置界面感官异常!");
}
}
public String getSelectPath() {
String path = null;
JFileChooser openLicenseFile = new JFileChooser();
exe_Filter xml_file = new exe_Filter();
openLicenseFile.addChoosableFileFilter(xml_file);
openLicenseFile.setFileFilter(xml_file);
openLicenseFile.setDialogTitle("文件选择");
openLicenseFile.setApproveButtonText("选择");
openLicenseFile.setApproveButtonToolTipText("文件选择");
openLicenseFile.setSelectedFile(new File(""));
openLicenseFile.setPreferredSize(new Dimension(600, 480));
String dirName = openLicenseFile.getCurrentDirectory().toString().trim();
if (dirName == null || dirName.trim().length() == 0) {
openLicenseFile.setCurrentDirectory(new File("."));
} else {
openLicenseFile.setCurrentDirectory(new File(dirName));
}
int rtVal = openLicenseFile.showOpenDialog(new JDialog());
if (rtVal == JFileChooser.APPROVE_OPTION) {
path = openLicenseFile.getSelectedFile().getAbsolutePath();
}
return path;
}
}
exe_Filter:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package java_guard_M;
import java.io.File;
import javax.swing.filechooser.FileFilter;
/**
*
* @author Administrator
*/
public class exe_Filter extends FileFilter {
public String getDescription() {
return "*.exe;*.EXE";
}
@Override
public boolean accept(File file) {
String name = file.getName();
return file.isDirectory() || name.toLowerCase().endsWith(".exe") || name.toLowerCase().endsWith(".EXE"); // 仅显示目录和xls、xlsx文件
}
}
Java_guard:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package java_guard_M;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import static java.lang.Thread.sleep;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;
/**
*
* @author Administrator
*/
public class Java_guard {
DOMForXml domXml = new DOMForXml();
// public static void main(String[] args) throws ParseException, IOException {
TimerTaskTest config = new TimerTaskTest();
// Java_guard guard = new Java_guard();
//
// guard.guard(".\\guard.xml");
// }
public void guard(String path_xml) throws ParseException {
Timer timer = new Timer();
Properties props = System.getProperties();
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
List list_xml = domXml.paseXml(path_xml);
if (props.getProperty("os.name").contains("Windows")) {
for (int i = 0; i < list_xml.size(); i++) {
List list = (List) list_xml.get(i);
Date date = fmt.parse(getDate()+" "+list.get(0).toString());
final String path = (String) list.get(2);
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
try {
if (false) {
} else {
closeExe(path);
openExe(path);
}
} catch (IOException | InterruptedException ex) {
Logger.getLogger(Java_guard.class.getName()).log(Level.SEVERE, null, ex);
}
}
}, date, 24 * 60 * 60 * 1000);
}
} else {
JOptionPane.showMessageDialog(null, "暂时无其他系统版本", "来自java_guard", JOptionPane.INFORMATION_MESSAGE);
}
}
public void openExe(String path) throws IOException {
String ProcessName = new String();
Pattern pattern = Pattern.compile("\\\\(\\w+).exe");
Matcher matcher = pattern.matcher(path);
if (matcher.find()) {
ProcessName = matcher.group(1).trim();
}
Boolean contain_Proces = true;
Process proc = Runtime.getRuntime().exec("tasklist");
BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String info = br.readLine(); //空格
info = br.readLine(); //汉字
br.readLine();//==
while (info != null) {
info = br.readLine();
if (info != null) {
if (info.contains(ProcessName)) {
JOptionPane.showMessageDialog(null, "相关进程已存在,请手动清除", "提示", JOptionPane.INFORMATION_MESSAGE);
contain_Proces = false;
break;
}
}
}
if (contain_Proces) {
Runtime.getRuntime().exec(path);
}
}
public void closeExe(String path) throws IOException, InterruptedException {
String ProcessName = new String();
Pattern pattern = Pattern.compile("\\\\(\\w+).exe");
Matcher matcher = pattern.matcher(path);
if (matcher.find()) {
ProcessName = matcher.group(1).trim();
}
Process proc = Runtime.getRuntime().exec("tasklist");
BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String info = br.readLine(); //空格
info = br.readLine(); //汉字
br.readLine();//==
while (info != null) {
info = br.readLine();
if (info != null) {
if (info.contains(ProcessName)) {
String command = "taskkill /f /im " + ProcessName + ".exe";
Runtime.getRuntime().exec(command);
sleep(1000 * 10);
break;
}
}
}
}
public String gettimeS() {
Date d = new Date();
long longtime = d.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");
return sdf.format(longtime);
}
public String getDate(){
Date d = new Date();
long longtime = d.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(longtime);
}
public void openConfig() {
try {
File file = new File(".");
String prect_path = file.getAbsolutePath().substring(0, file.getAbsolutePath().length() - 1);
String config_path = "C:\\WINDOWS\\system32\\notepad.exe " + prect_path + "guard.xml";
Runtime.getRuntime().exec(config_path);
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "找不到配置文件", "提示", JOptionPane.INFORMATION_MESSAGE);
}
}
}
JavaGuard:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package java_guard_V;
import java_guard_M.DOMForXml;
import java_guard_M.Java_guard;
import java_guard_M.tableModel;
import java.awt.Color;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java_guard_M.JFileChooserDemo;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableCellRenderer;
/**
*
* @author Administrator
*/
public class JavaGuard extends javax.swing.JFrame {
public static int limit_num = 1;
DOMForXml domXml = new DOMForXml();
Java_guard guard = new Java_guard();
JFileChooserDemo fileChoose = new JFileChooserDemo();
close_V closeing = new close_V();
String Scada_value;
String Software_name;
/**
* Creates new form JavaGuard
*/
public JavaGuard() {
initComponents();
this.setTitle("进程守护程序");
this.getContentPane().setBackground(new Color(0xC8D1FF));
this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jTextField1 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jScrollPane2 = new javax.swing.JScrollPane();
table_Guard = new javax.swing.JTable();
jButton3 = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem2 = new javax.swing.JMenuItem();
jMenuItem1 = new javax.swing.JMenuItem();
jTextField1.setText("jTextField1");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
formMouseClicked(evt);
}
});
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosed(java.awt.event.WindowEvent evt) {
formWindowClosed(evt);
}
public void windowClosing(java.awt.event.WindowEvent evt) {
formWindowClosing(evt);
}
});
jLabel1.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
jLabel1.setText("守护程序:");
jButton1.setBackground(new java.awt.Color(153, 153, 255));
jButton1.setFont(new java.awt.Font("宋体", 0, 14)); // NOI18N
jButton1.setText("手动启动");
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton1MouseClicked(evt);
}
});
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
List list_guards= domXml.paseXml(".\\guard.xml");
table_Guard.setModel(new tableModel(list_guards)
);
setTable();
table_Guard.setRowHeight(30);
DefaultTableCellRenderer renderer_Hscada = (DefaultTableCellRenderer) table_Guard.getTableHeader().getDefaultRenderer();
renderer_Hscada.setHorizontalAlignment(DefaultTableCellRenderer.CENTER); //表头居中
table_Guard.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
table_GuardMouseClicked(evt);
}
});
jScrollPane2.setViewportView(table_Guard);
jButton3.setFont(new java.awt.Font("宋体", 0, 14)); // NOI18N
jButton3.setText("删除当前");
jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton3MouseClicked(evt);
}
});
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jMenu1.setText("文件");
jMenu1.setFont(new java.awt.Font("微软雅黑", 0, 18)); // NOI18N
jMenuItem2.setFont(new java.awt.Font("微软雅黑", 0, 16)); // NOI18N
jMenuItem2.setText("打开配置文件");
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem2ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem2);
jMenuItem1.setFont(new java.awt.Font("微软雅黑", 0, 16)); // NOI18N
jMenuItem1.setText("添加守护程序");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem1);
jMenuBar1.add(jMenu1);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(30, 30, 30)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 425, Short.MAX_VALUE)
.addGap(28, 28, 28)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(25, 25, 25))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(19, 19, 19)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(32, 32, 32)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(29, Short.MAX_VALUE))
);
setBounds(500, 200, 613, 382);
}// </editor-fold>//GEN-END:initComponents
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
guard.openConfig();
}//GEN-LAST:event_jMenuItem2ActionPerformed
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
}//GEN-LAST:event_jButton1ActionPerformed
private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
table_Guard.setModel(new tableModel(domXml.paseXml(".\\guard.xml")));
setTable();
}//GEN-LAST:event_formMouseClicked
private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
}//GEN-LAST:event_formWindowClosed
private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
// int i = javax.swing.JOptionPane.showConfirmDialog(this,"确定退出?","提示",javax.swing.JOptionPane.OK_CANCEL_OPTION,javax.swing.JOptionPane.QUESTION_MESSAGE);
new close_V().setVisible(true);
}//GEN-LAST:event_formWindowClosing
private void table_GuardMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_table_GuardMouseClicked
int scadaRow = table_Guard.getSelectedRow(); //获得鼠标选定的行
Scada_value = (String) table_Guard.getValueAt(scadaRow, 0); //获得选定行的想要的列的值
Software_name=(String) table_Guard.getValueAt(scadaRow, 2);
}//GEN-LAST:event_table_GuardMouseClicked
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jButton3ActionPerformed
private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton3MouseClicked
domXml.deleteNode( Scada_value );
List list_guards= domXml.paseXml(".\\guard.xml");
table_Guard.setModel(new tableModel(list_guards));
}//GEN-LAST:event_jButton3MouseClicked
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
new java_protect().setVisible(true);
}//GEN-LAST:event_jMenuItem1ActionPerformed
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
try {
guard.openExe( Software_name);
} catch (IOException ex) {
Logger.getLogger(JavaGuard.class.getName()).log(Level.SEVERE, null, ex);
}
}//GEN-LAST:event_jButton1MouseClicked
public void setTable() {
table_Guard.getColumnModel().getColumn(0).setMaxWidth(130);
table_Guard.getColumnModel().getColumn(0).setMinWidth(130);
table_Guard.getColumnModel().getColumn(1).setMaxWidth(130);
table_Guard.getColumnModel().getColumn(1).setMinWidth(130);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(JavaGuard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(JavaGuard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(JavaGuard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(JavaGuard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JavaGuard().setVisible(true);
Java_guard guard = new Java_guard();
try {
guard.guard(".\\guard.xml");
} catch (ParseException ex) {
Logger.getLogger(JavaGuard.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(null, "内部程序出错", "来自java_guard", JOptionPane.INFORMATION_MESSAGE);
}
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextField jTextField1;
public static javax.swing.JTable table_Guard;
// End of variables declaration//GEN-END:variables
}