• 第五天


    package wl;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Font;
    import java.awt.Frame;
    import java.awt.MenuBar;
    import java.util.List;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.JToolBar;
    import javax.swing.border.Border;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableModel;
    
    public class ProcessApp extends JFrame {
        static JFrame f = new JFrame("Process");
        static JPanel panel = new JPanel();
         static Object[][] cellData = {{"名称","PID","优先级","状态","用户名","cpu","内存","描述"}};
            static String[] columnNames = {"col1","col2","col3","col4","col5","col6","col7","col8"};
         static JTable table = new JTable(cellData, columnNames);
        ProcessApp() {// init frame
            f.setVisible(true);
            f.setSize(1000, 800);
            f.setLayout(new BorderLayout());
    
        }
    
        void initMenu() {// init menu
            JMenuBar menubar = new JMenuBar();
            JMenu[] menus = new JMenu[] { new JMenu("文件"), new JMenu("选项"), new JMenu("查看") };
            for (int i = 0; i < menus.length; i++) {
                menubar.add(menus[i]);
            }
            f.setJMenuBar(menubar);
        }
    
        void initToolBar() {
            JToolBar toolbar = new JToolBar();
            JButton[] buttons = new JButton[] { new JButton("应用"), new JButton("进程"), new JButton("性能"), new JButton("联网"),
                    new JButton("用户"), new JButton("性能") };
            for (int i = 0; i < buttons.length; i++) {
                toolbar.add(buttons[i]);
            }
            toolbar.setBackground(Color.white);
            f.getContentPane().add(toolbar, BorderLayout.NORTH);
        }
    
        void initPanel() {
    
            panel.setBackground(Color.WHITE);
            f.getContentPane().add(panel, BorderLayout.CENTER);
        }
    
        void initTable() {
              //(1)名称,优先级,cpu,内存,磁盘,网络,GPU的使用情况,端口,用户名,状态,描述
             
                table.setSize((int) (panel.getWidth()*0.2),40);
                table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS );
                String[] headers= {"名称","PID","优先级","状态","用户名","cpu","内存","描述"};
                DefaultTableModel model =new DefaultTableModel(cellData, headers) {
                     public boolean isCellEditable(int row, int column) {
                       return false;
                     }
                };
                table = new JTable(model);
                panel.add(table);
              
        }
        
        void setTable() {
        
            TableModel tableModel = table.getModel();
            //"名称","PID","优先级","状态","用户名","cpu","内存","描述"
            List<processModel> processs=process.Getprocess();
            for(processModel p:processs) {
                 String[] arr=new String[8];
                    arr[0]=p.getName();
                    arr[1]=p.getPID();
                    arr[2]=p.getPrio();
                    arr[3]=p.getState();
                    arr[4]=p.getUser();
                    arr[5]=Double.toString(p.getB_cpu());
                    arr[6]=Double.toString(p.getB_memory());
                    arr[7]=p.getDescribe();    
                    
            // 添加数据到表格
            ((DefaultTableModel) tableModel).addRow(arr);
           
        }
        // 更新表格
        table.invalidate();
        //table.setSize(panel.getWidth()-20, panel.getHeight());
        JScrollPane jsp;
        jsp = new JScrollPane(table);
        panel.add(jsp);
     
      
        }
    
        public static void main(String[] args) {
            ProcessApp p = new ProcessApp();
            p.initMenu();
            p.initToolBar();
            p.initPanel();
            p.initTable();
            p.setTable();
            System.out.println("finish");
        }
    }
    package wl;
    
    public class processModel {
       // "名称","PID","状态","用户名","cpu","内存","磁盘","网络","描述"
       private String name;//"名称
       private String PID;// 进程id
       private String state;//进程状态。D=不可中断的睡眠状态 R=运行 S=睡眠 T=跟踪/停止 Z=僵尸进程
       private String user;//进程所有者
       private double b_cpu;//进程所有者
       private double b_memory;//进程使用的物理内存百分比
       private double memory;//进程使用的、未被换出的物理内存大小,单位kb。RES=CODE+DATA
     
       private String describe;
       private String  prio;
       
    public String getPrio() {
        return prio;
    }
    public void setPrio(String prio) {
        this.prio = prio;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPID() {
        return PID;
    }
    public void setPID(String pID) {
        PID = pID;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
    public String getUser() {
        return user;
    }
    public void setUser(String user) {
        this.user = user;
    }
    
    public double getMemory() {
        return memory;
    }
    public void setMemory(double memory) {
        this.memory = memory;
    }
    
    public String getDescribe() {
        return describe;
    }
    public void setDescribe(String describe) {
        this.describe = describe;
    }
    public double getB_cpu() {
        return b_cpu;
    }
    public void setB_cpu(double b_cpu) {
        this.b_cpu = b_cpu;
    }
    public double getB_memory() {
        return b_memory;
    }
    public void setB_memory(double b_memory) {
        this.b_memory = b_memory;
    }
    @Override
    public String toString() {
        return "processModel [name=" + name + ", PID=" + PID + ", state=" + state + ", user=" + user + ", b_cpu=" + b_cpu
                + ", b_memory=" + b_memory + ", memory=" + memory + ", describe=" + describe + ", prio=" + prio + "]";
    }
    
    
    
       
    }
    package wl;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Properties;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class process {
        
        //get process infomation
        public static List<processModel> Getprocess() {
            
            Runtime runtime = Runtime.getRuntime();
            List<String> tasklist = new ArrayList<String>();
            java.lang.Process process = null;
            List<processModel> processs=new ArrayList<processModel>();
            int count = 0; // 统计进程数
            try {
                /*
                 * 
                 * 自适应执行查询进程列表命令
                 * 
                 */
                Properties prop = System.getProperties();
                // 获取操作系统名称
                boolean is=false;
                String os = prop.getProperty("os.name");
                if (os != null && os.toLowerCase().indexOf("linux") > -1) {
                    // 1.适应与linux
    
                    BufferedReader reader = null;
                    // 显示所有进程
                    //"名称","PID","优先级","状态","用户名","cpu","内存","磁盘","网络","描述"
                    process = Runtime.getRuntime().exec("top -b -n 1");
                    reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
    
                    String line = null;
    
                    while ((line = reader.readLine()) != null) {
                        if(line.startsWith("   PID")) {
                            is=true;
                            line=reader.readLine();
                        }
                        if(is==true) {
                            
                            Pattern p = Pattern.compile("\s+");
                            Matcher m = p.matcher(line);
                            line= m.replaceAll(",");
                            
                    
                        String[] strs=line.split(",");
                       
                        processModel pro=new processModel();
                        pro.setPID(strs[1]);
                        pro.setUser(strs[2]);
                        pro.setPrio(strs[3]);
                        pro.setMemory(Double.valueOf(strs[4]));
                        pro.setState(strs[8]);
                        pro.setB_cpu(Double.valueOf(strs[9]));
                        pro.setB_memory(Double.valueOf(strs[10]));
                        pro.setName(strs[12]);
                        System.out.println(pro.toString());
                        processs.add(pro);
                        }
                    }
    
                }
    
            } catch (Exception e) {
    
                e.printStackTrace();
    
            }
    
            return processs;
    
        }
    
        
        public static void main(String[] args) throws Exception {
            System.out.println(Getprocess());;
            
            //installTool("iotop");
        }
    }
  • 相关阅读:
    Cocos2d-X 精灵、动作效果
    android动画介绍之 自己定义Animation动画实现qq抖一抖效果
    C#将dataGridView中显示的数据导出到Excel(大数据量超有用版)
    Redis深入之对象
    【POJ 3694】 Network(割边&lt;桥&gt;+LCA)
    Swift学习笔记(十四)——字符,常量字符串与变量字符串
    关于tomcat session机制梳理
    【机器学习算法-python实现】採样算法的简单实现
    分公司=一部门——组合模式
    临渊慕鱼,不如退而结网!
  • 原文地址:https://www.cnblogs.com/wl2017/p/10836997.html
Copyright © 2020-2023  润新知