java中输出进程的映像名称_【java程序】没啥用系列第一弹——结束进程加强版(进程监控)...

[Java] 纯文本查看 复制代码package monitor;

import java.awt.Font;

import java.awt.Point;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Collection;

import java.util.HashSet;

import java.util.LinkedHashSet;

import java.util.List;

import java.util.Scanner;

import java.util.Set;

import java.util.Vector;

import javax.swing.BorderFactory;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JList;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JScrollBar;

import javax.swing.JScrollPane;

import javax.swing.JSlider;

import javax.swing.JTextField;

import javax.swing.border.EmptyBorder;

import javax.swing.event.ChangeEvent;

import javax.swing.event.ChangeListener;

public class Main extends Thread {

public volatile static boolean exit = true;

private volatile static int second = 1;

private volatile static List newlist;

private volatile static List addlist = new ArrayList();

private volatile static List dellist = new ArrayList();

private volatile static int position;

private volatile static String neirong;

private static JFrame jf = new JFrame("Process Monitor");

private static JPanel jp = new JPanel();

private static JLabel interval = new JLabel();// 检测间隔

private static JLabel title = new JLabel();// 标题

private static JLabel total = new JLabel();// 总数

private static JLabel realtime = new JLabel();// 实时检测

private static JLabel name = new JLabel();// 变化名

private static JList list1 = null;

private static JList list2 = null;

private static JList list3 = null;

private static Vector v = new Vector();

private static Vector n = new Vector();

private static Vector t = new Vector();

private static JScrollPane jsp;

private static JScrollPane jsp2;

private static JScrollPane jsp3;

Main() {

newlist = removeStringListDupli(showTaskList());

}

public static void show() {

jf.setSize(500, 650);

final JSlider slider = new JSlider(1, 10, 1);

// 设置主刻度间隔

slider.setMajorTickSpacing(1);

// 设置次刻度间隔

slider.setMinorTickSpacing(1);

// 绘制 刻度 和 标签

slider.setPaintTicks(true);

slider.setPaintLabels(true);

slider.setBounds(43, 520, 400, 50);

// 添加刻度改变监听器

for (String xx : newlist) {

v.add(xx);

}

list1 = new JList(v);

list1.setBorder(BorderFactory.createTitledBorder("进程列表"));

list2 = new JList();

list2.setBorder(BorderFactory.createTitledBorder("增加列表"));

list3 = new JList();

list3.setBorder(BorderFactory.createTitledBorder("减少列表"));

JButton refresh = new JButton("刷新列表");

JButton stop = new JButton("停止监测");

JTextField jtf = new JTextField();

JButton sou = new JButton("搜");

sou.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

String str = jtf.getText();

if (!str.equals("")) {

int i;

if (str.equals(neirong)) {

i = position + 1;

} else {

i = 0;

}

k: for (; i < newlist.size(); i++) {

if (newlist.get(i).toLowerCase().contains(str.toLowerCase())) {

list1.setSelectedIndex(i);

Point p = list1.indexToLocation(i);

JScrollPane jScrollPane = jsp;

JScrollBar jScrollBar = jScrollPane.getVerticalScrollBar();// 获得垂直滚动条

jScrollBar.setValue(p.y);

neirong = str;

position = i;

break k;

}

}

if (i == newlist.size()&&str.equals(neirong)) {

i=0;

k: for (; i < newlist.size(); i++) {

if (newlist.get(i).toLowerCase().contains(str.toLowerCase())) {

list1.setSelectedIndex(i);

Point p = list1.indexToLocation(i);

JScrollPane jScrollPane = jsp;

JScrollBar jScrollBar = jScrollPane.getVerticalScrollBar();// 获得垂直滚动条

jScrollBar.setValue(p.y);

neirong = str;

position = i;

break k;

}

}

}else if(i == newlist.size()){

JOptionPane.showMessageDialog(null, "未发现该进程");

}

} else {

JOptionPane.showMessageDialog(null, "不能为空");

}

}

});

JButton close = new JButton("退出");

JButton kill = new JButton("结束进程");

close.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent evt) {

int exi = JOptionPane.showConfirmDialog(null, "要退出该程序吗?", "友情提示", JOptionPane.YES_NO_OPTION,

JOptionPane.QUESTION_MESSAGE);

if (exi == JOptionPane.YES_OPTION) {

System.exit(0);

} else {

return;

}

}

});

title.setHorizontalAlignment(JLabel.CENTER);

title.setText("进程监测器");

title.setBounds(0, 0, 500, 50);

title.setFont(new Font("宋体", Font.BOLD, 30));

total.setText("总进程数:" + newlist.size());

total.setBounds(0, 50, 500, 20);

total.setHorizontalAlignment(JLabel.CENTER);

jp.add(total);

refresh.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

newlist = removeStringListDupli(showTaskList());

jp.remove(total);

total.setText("总进程数:" + newlist.size());

total.setBounds(0, 50, 500, 20);

total.setHorizontalAlignment(JLabel.CENTER);

jp.add(total);

jp.remove(jsp);

v.clear();

for (String xx : newlist) {

v.add(xx);

}

list1 = new JList(v);

list1.setBorder(BorderFactory.createTitledBorder("进程列表"));

jsp = new JScrollPane(list1);

jsp.setBounds(10, 200, 460, 200);

jp.add(jsp);

jp.updateUI();

}

});

stop.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

if (stop.getText().equals("停止监测")) {

exit = false;

stop.setText("开始监测");

jp.updateUI();

} else {

Main main = new Main();

main.start();

exit = true;

stop.setText("停止监测");

jp.updateUI();

}

}

});

interval.setText("检测间隔:" + second + "秒");

interval.setBounds(0, 70, 500, 20);

interval.setHorizontalAlignment(JLabel.CENTER);

jp.add(interval);

slider.addChangeListener(new ChangeListener() {

@Override

public void stateChanged(ChangeEvent e) {

second = slider.getValue();

jp.remove(interval);

interval.setText("检测间隔:" + second + "秒");

interval.setBounds(0, 70, 500, 20);

interval.setHorizontalAlignment(JLabel.CENTER);

jp.add(interval);

jp.updateUI();

}

});

jp.add(title);

realtime.setText("已获取全部进程");

realtime.setBounds(0, 90, 500, 20);

realtime.setHorizontalAlignment(JLabel.CENTER);

jp.add(realtime);

name.setText("第一次获取无变化");

name.setBounds(0, 110, 500, 20);

name.setHorizontalAlignment(JLabel.CENTER);

jp.add(name);

refresh.setBounds(10, 160, 90, 30);

stop.setBounds(1 * 90 + 2 * 10, 160, 90, 30);

jtf.setBounds(2 * 90 + 3 * 10, 160, 55, 30);

sou.setBounds(2 * 90 + 4 * 10 + 40, 160, 35, 30);

sou.setBorder(new EmptyBorder(0, 0, 0, 0));

jtf.setBorder(new EmptyBorder(0, 0, 0, 0));

kill.setBounds(3 * 90 + 4 * 10, 160, 90, 30);

close.setBounds(4 * 90 + 5 * 10, 160, 60, 30);

kill.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

if (list1.getSelectedIndex() != -1) {

killTask(list1.getSelectedValue());

} else {

if (realtime.getText().contains("减少")) {

JOptionPane.showMessageDialog(null, list2.getSelectedValue() + "已被终止");

} else {

killTask(list2.getSelectedValue());

}

}

}

});

jsp = new JScrollPane(list1);

jsp2 = new JScrollPane(list2);

jsp3 = new JScrollPane(list3);

jsp.setBounds(10, 200, 460, 200);

jsp2.setBounds(10, 410, 230, 100);

jsp3.setBounds(250, 410, 230, 100);

jp.add(jsp);

jp.add(jsp2);

jp.add(jsp3);

jp.add(refresh);

jp.add(stop);

jp.add(jtf);

jp.add(sou);

jp.add(kill);

jp.add(close);

jp.add(slider);

jp.setLayout(null);

jf.add(jp);

jf.setLocationRelativeTo(null);

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jf.setVisible(true);

}

/**

* 杀死一个进程

*

* @param task

*/

public static void killTask(String task) {

try {

Process process = Runtime.getRuntime().exec("taskList");

Scanner in = new Scanner(process.getInputStream());

while (in.hasNextLine()) {

String temp = in.nextLine();

if (temp.contains(task)) {

// 判断该进程所占内存是否大于20M

// if (Integer.parseInt(t[t.length - 2].replace(",", "")) > 20000) {

temp = temp.replaceAll(" ", "");

// 获得pid

String pid = temp.substring(temp.indexOf(".exe") + 4, temp.indexOf("Console"));

Runtime.getRuntime().exec("taskkill /f /im " + pid);

}

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

JOptionPane.showMessageDialog(null, list2.getSelectedValue() + "删除失败!"+e.getMessage());

}

}

public static List showTaskList() {

List test = new ArrayList();

try {

Process process = Runtime.getRuntime().exec("taskList");

Scanner in = new Scanner(process.getInputStream(), "GB2312");

while (in.hasNext()) {

String xxx = in.nextLine();

if (!(xxx.contains("===") || xxx.contains("映像名称") || !xxx.contains(" "))) {

String[] ds = xxx.split(" ");

test.add(ds[0]);

}

}

in.close();

process.destroyForcibly();

return test;

} catch (Exception e) {

e.printStackTrace();

return test;

}

}

/**

* list去重

*/

public static List removeStringListDupli(List stringList) {

Set set = new LinkedHashSet<>();

set.addAll(stringList);

stringList.clear();

stringList.addAll(set);

return stringList;

}

@Override

public void run() {

List a = new ArrayList();

List o = new ArrayList();

do {

try {

Thread.sleep(second * 1000);

} catch (InterruptedException e) {

// TODO 自动生成的 catch 块

e.printStackTrace();

}

a = showTaskList();

a = removeStringListDupli(a);

if (a.size() > o.size()) {

int i = a.size() - o.size();

if (o.size() != 0) {

newlist = a;

jp.remove(total);

total.setText("总进程数:" + newlist.size());

total.setBounds(0, 50, 500, 20);

total.setHorizontalAlignment(JLabel.CENTER);

jp.add(total);

jp.remove(jsp);

v.clear();

for (String xx : newlist) {

v.add(xx);

}

list1 = new JList(v);

list1.setBorder(BorderFactory.createTitledBorder("进程列表"));

jsp = new JScrollPane(list1);

jsp.setBounds(10, 200, 460, 200);

jp.add(jsp);

jp.remove(realtime);

realtime.setText("增加" + i + "个进程");

realtime.setBounds(0, 90, 500, 20);

realtime.setHorizontalAlignment(JLabel.CENTER);

jp.add(realtime);

jp.remove(name);

StringBuffer ad = new StringBuffer();

ad.setLength(0);

List changelist = change(a, o);

addlist.addAll(changelist);

for (String xx : changelist) {

ad.append(xx + ",");

}

n.clear();

for (int h = addlist.size() - 1; h >= 0; h--) {

n.add(addlist.get(h));

}

jp.remove(jsp2);

list2 = new JList(n);

list2.setBorder(BorderFactory.createTitledBorder("增加列表"));

jsp2 = new JScrollPane(list2);

jsp2.setBounds(10, 410, 230, 100);

jp.add(jsp2);

ad.deleteCharAt(ad.length() - 1);

name.setText("进程为:" + ad);

name.setBounds(0, 110, 500, 20);

name.setHorizontalAlignment(JLabel.CENTER);

jp.add(name);

jp.updateUI();

}

o = a;

} else if (a.size() < o.size()) {

int i = o.size() - a.size();

newlist = a;

jp.remove(total);

total.setText("总进程数:" + newlist.size());

total.setBounds(0, 50, 500, 20);

total.setHorizontalAlignment(JLabel.CENTER);

jp.add(total);

jp.remove(jsp);

v.clear();

for (String xx : newlist) {

v.add(xx);

}

list1 = new JList(v);

list1.setBorder(BorderFactory.createTitledBorder("进程列表"));

jsp = new JScrollPane(list1);

jsp.setBounds(10, 200, 460, 200);

jp.add(jsp);

jp.remove(realtime);

realtime.setText("减少" + i + "个进程");

realtime.setBounds(0, 90, 500, 20);

realtime.setHorizontalAlignment(JLabel.CENTER);

jp.add(realtime);

jp.remove(name);

List changelist = change(a, o);

dellist.addAll(changelist);

StringBuffer ad = new StringBuffer();

ad.setLength(0);

for (String xx : changelist) {

ad.append(xx + ",");

}

t.clear();

for (int h = dellist.size() - 1; h >= 0; h--) {

t.add(dellist.get(h));

}

jp.remove(jsp3);

list3 = new JList(t);

list3.setBorder(BorderFactory.createTitledBorder("减少列表"));

jsp3 = new JScrollPane(list3);

jsp3.setBounds(250, 410, 230, 100);

jp.add(jsp3);

ad.deleteCharAt(ad.length() - 1);

name.setText("进程为:" + ad);

name.setBounds(0, 110, 500, 20);

name.setHorizontalAlignment(JLabel.CENTER);

jp.add(name);

jp.updateUI();

o = a;

}

} while (exit);

}

/**

* list变化

*

* @param _a

* @param _b

* @return

*/

public static List change(List _a, List _b) {

// 创建集合

Collection realA = new ArrayList(_a);

Collection realB = new ArrayList(_b);

// 求交集

realA.retainAll(realB);

Set result = new HashSet();

// 求全集

result.addAll(_a);

result.addAll(_b);

// 求差集:结果

Collection aa = new ArrayList(realA);

Collection bb = new ArrayList(result);

bb.removeAll(aa);

Object[] dsa = bb.toArray();

List xsxs = Arrays.asList(dsa);

List ddd = new ArrayList();

for (Object x : xsxs) {

ddd.add(x.toString());

}

return ddd;

}

public static void main(String[] args) {

/*

* try { BeautyEyeLNFHelper.frameBorderStyle =

* BeautyEyeLNFHelper.FrameBorderStyle.translucencyAppleLike;

* UIManager.put("RootPane.setupButtonVisible", false);

* BeautyEyeLNFHelper.translucencyAtFrameInactive = true;

* org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF(); } catch

* (Exception e) { e.printStackTrace(); }

*/

Main main = new Main();

main.start();

show();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值