zoukankan      html  css  js  c++  java
  • Create小程序

    我有时候喜欢直接用命令行创建、编译、执行java文件, 每次创建一个文件都要新建一个.java文件,然后再编辑.java文件加入类名,主函数……

    这些流程我有点厌倦,于是就编写了一个超级简单的自动创建工具。下面贴出代码:

    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.util.HashMap;
    import java.util.Map;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.border.EmptyBorder;
    
    
    public class Create extends JFrame {
    
        private String classname;
        private JPanel contentPane;
        private JTextField textField;
        private JLabel lblNewLabel_1;
        private JButton btnNewButton_1;
        private JButton btnNewButton_2;
    
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        Create frame = new Create();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        /**
         * Create the frame.
         */
        public Create() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 450, 300);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            setContentPane(contentPane);
            contentPane.setLayout(null);
            
            JLabel lblNewLabel = new JLabel("Class name:");
            lblNewLabel.setBounds(49, 0, 93, 42);
            contentPane.add(lblNewLabel);
            
            textField = new JTextField();
            textField.setBounds(170, 7, 212, 26);
            contentPane.add(textField);
            textField.setColumns(10);
            
            lblNewLabel_1 = new JLabel("");
            lblNewLabel_1.setBounds(49, 34, 333, 18);
            contentPane.add(lblNewLabel_1);        
            
            JButton btnNewButton = new JButton("create");
            btnNewButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    try {
                        classname = textField.getText();
                        if(classname.equals("")){
                            lblNewLabel_1.setText("请输入需要创建的类名!");
                        }else{
                            PrintWriter out = new PrintWriter(new File(classname+".java"));
                            String str = "class "+classname+"{
    	public "+classname+"(){}
    	public static void main(String[] args){
    	// TODO Auto-generated method stub
    
    	}
    }";
                            out.print(str);
                            out.close();
                            textField.setText("");
                            lblNewLabel_1.setText(classname+".java 创建成功");
                        }
    
                    } catch (FileNotFoundException e1) {
                        // TODO Auto-generated catch block
                        JOptionPane.showMessageDialog(getComponent(0), "创建失败");
                    } 
                }
            });
            btnNewButton.setBounds(275, 62, 107, 23);
            contentPane.add(btnNewButton);
            
            JScrollPane scrollPane = new JScrollPane();
            scrollPane.setBounds(10, 95, 414, 156);
            contentPane.add(scrollPane);
            
            JTextArea textArea = new JTextArea();
            scrollPane.setViewportView(textArea);
            
            btnNewButton_1 = new JButton("javac编译");
            btnNewButton_1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if(classname.equals("")){
                        lblNewLabel_1.setText("你还没有创建类文件!");
                    }else{
                        final Map<String, String> env = new HashMap<String, String>(System.getenv());
                        final String[] strings=mapToStringArray(env);
                        BufferedReader reader=null;
                        Process p;
                        String error="";
                        try {
                            p = Runtime.getRuntime().exec("cmd /c javac "+classname+".java",strings);
                            p.waitFor(); 
                            reader=new BufferedReader(new InputStreamReader(p.getErrorStream())); 
                            String line; 
                            while((line = reader.readLine()) != null) 
                            { 
                                error+=line+"
    ";
                            }
                        } catch (Exception e1) {
                            // TODO Auto-generated catch block
                            System.out.println("编译程序出错");
                        }
                        if(error.equals("")){
                            textArea.setText("编译成功。");
                        }else{
                            textArea.setText(error);
                        }
                    }
                }
            });
            btnNewButton_1.setBounds(38, 62, 93, 23);
            contentPane.add(btnNewButton_1);
            
            btnNewButton_2 = new JButton("java运行");
            btnNewButton_2.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    final Map<String, String> env = new HashMap<String, String>(System.getenv());
                    final String[] strings=mapToStringArray(env);
                    BufferedReader reader=null;
                    Process p;
                    String error="";
                    String success="";
                    try {
                        p = Runtime.getRuntime().exec("cmd /c java "+classname,strings);
                        p.waitFor(); 
                        reader=new BufferedReader(new InputStreamReader(p.getErrorStream())); 
                        String line; 
                        while((line = reader.readLine()) != null) 
                        { 
                            error+=line+"
    ";
                        }
                        if(error.equals("")){
                            reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
                            while((line = reader.readLine()) != null) 
                            { 
                                success+=line+"
    ";
                            }
                        }
                    } catch (Exception e1) {
                        // TODO Auto-generated catch block
                        System.out.println("运行程序出错");
                    }
                    if(error.equals("")){
                        textArea.setText("运行成功.
    "+success);
                    }else{
                        textArea.setText(error);
                    }
                }
            });
            btnNewButton_2.setBounds(159, 62, 93, 23);
            contentPane.add(btnNewButton_2);
            
            this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        }
        String[] mapToStringArray(Map<String, String> map) {
            final String[] strings = new String[map.size()];
            int i = 0;
            for (Map.Entry<String, String> e : map.entrySet()) {
                strings[i] = e.getKey() + '=' + e.getValue();
                i++;
            }
            return strings;
        }
    }
    View Code

    程序运行图:

    我们在Class name栏目输入我们需要创建的类名。

    点击create按钮后就会在当前路径下生成Hello.java文件。文件内容是:

    class Hello{
        public Hello(){}
        public static void main(String[] args){
        // TODO Auto-generated method stub
    
        }
    }

    然后我们在源文件中添加一行测试代码:System.out.println("hello");

    class Hello{
        public Hello(){}
        public static void main(String[] args){
        // TODO Auto-generated method stub
            System.out.println("hello");
        }
    }

    点击编译就能编译成功生成.class文件。

    点击运行就能出现运行结果:

    虽然很简单,但是还是挺实用的,以后每次就用它来创建我的类文件了,O(∩_∩)O!

    当然也可以写几行脚本来完成这个小小的功能……

  • 相关阅读:
    使用密码解密TACACS+的报文
    C9K Stackwise Virtual(三)
    Webhook Configuration Example
    sup-bootflash和bootflash
    WLC5508 license没有500个?
    AAA Server Groups
    关于FlexConnect的Bug!
    Bug搬运工-CSCve57121--Cisco 2800, 3800 and 1560 series APs fail to pass traffic
    Bug搬运工-CSCvb29354-1810 OEAP cannot join vWLC
    阿里云云计算认证ACP模拟考试练习题第1套模拟题分享(共10套)
  • 原文地址:https://www.cnblogs.com/yuanzhenliu/p/5675958.html
Copyright © 2011-2022 走看看