计算机网络第五次作业

来源:互联网 发布:office2007 mac破解版 编辑:程序博客网 时间:2024/06/03 15:57

                                                                          编写一个GUI程序实现ping功能

一、GUI程序界面

        用MyEclipse中Java模块有一个WindowBuilder模块,用WindowBuilder设计GUI的界面,如下图所示:


二、源代码展示

       1.实现ping功能的函数代码

    

    2.实现JFrame窗口的完整代码:

public class Ping extends JFrame {

    private JPanel contentPane;
    private JTextField address_textField;
    private JTextArea textArea;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Ping frame = new Ping();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Ping() {
        setIconImage(Toolkit.getDefaultToolkit().getImage("D:\\Java_code\\Ping\\images\\banana.png"));
        setTitle("Ping GUI");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 606, 414);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        
        JPanel panel = new JPanel();
        
        JPanel panel_1 = new JPanel();
        panel_1.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Results", TitledBorder.LEFT, TitledBorder.TOP, null, new Color(0, 0, 0)));
        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(
            gl_contentPane.createParallelGroup(Alignment.LEADING)
                .addComponent(panel, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 580, Short.MAX_VALUE)
                .addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 580, Short.MAX_VALUE))
        );
        gl_contentPane.setVerticalGroup(
            gl_contentPane.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_contentPane.createSequentialGroup()
                    .addComponent(panel, GroupLayout.PREFERRED_SIZE, 76, GroupLayout.PREFERRED_SIZE)
                    .addGap(18)
                    .addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE)
                    .addContainerGap())
        );
        
        textArea = new JTextArea();
        textArea.setFont(new Font("Monospaced", Font.PLAIN, 14));
        textArea.setBackground(SystemColor.control);
        textArea.setEditable(false);
        GroupLayout gl_panel_1 = new GroupLayout(panel_1);
        gl_panel_1.setHorizontalGroup(
            gl_panel_1.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_panel_1.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(textArea, GroupLayout.DEFAULT_SIZE, 538, Short.MAX_VALUE)
                    .addContainerGap())
        );
        gl_panel_1.setVerticalGroup(
            gl_panel_1.createParallelGroup(Alignment.LEADING)
                .addComponent(textArea, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
        );
        panel_1.setLayout(gl_panel_1);
        
        JLabel IP_AddressLabel = new JLabel("IP Address:");
        
        address_textField = new JTextField();
        address_textField.setText("127.0.0.1");
        address_textField.setColumns(10);
        
        JButton PingButton = new JButton("ping");
        PingButton.setFont(new Font("宋体", Font.PLAIN, 17));
        PingButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                pingAction(e);
            }
        });
        GroupLayout gl_panel = new GroupLayout(panel);
        gl_panel.setHorizontalGroup(
            gl_panel.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_panel.createSequentialGroup()
                    .addGap(20)
                    .addComponent(IP_AddressLabel, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(ComponentPlacement.RELATED)
                    .addComponent(address_textField, GroupLayout.PREFERRED_SIZE, 205, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(ComponentPlacement.RELATED, 120, Short.MAX_VALUE)
                    .addComponent(PingButton)
                    .addGap(99))
        );
        gl_panel.setVerticalGroup(
            gl_panel.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_panel.createSequentialGroup()
                    .addGap(34)
                    .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                        .addComponent(IP_AddressLabel, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
                        .addComponent(address_textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(PingButton))
                    .addContainerGap(17, Short.MAX_VALUE))
        );
        panel.setLayout(gl_panel);
        contentPane.setLayout(gl_contentPane);
        
        //设置窗体居中
        this.setLocationRelativeTo(null);
    }

    private void pingAction(ActionEvent ae) {
        StringBuffer sb=new StringBuffer();
        String str=null;
        Runtime runtime=Runtime.getRuntime();
        Process process=null;
        String IP_Address=address_textField.getText();
        String command="ping "+IP_Address;
         try {
            process = runtime.exec(command);
            BufferedReader in=new BufferedReader(
                    new InputStreamReader(process.getInputStream()));
            while((str=in.readLine())!=null){
                sb.append(str+"\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
         textArea.setText(sb.toString());
    }
}


    三、总结

     本次实验使用的实现ping的方法是直接通过调用命令提示符窗口去执行ping命令,在gui界面显示ping后的结果有时间延时,之后会考虑换种方法实现ping功能。在所给出的压缩包中,附有可运行的jar包,可执行的exe文件,jre环境,动态的源码展示gif图片。运行之后的JFrame是一个香蕉图标的窗口。


原创粉丝点击