在线网络速度测试JAVA程序(二):GUI客户端


在jperf.java程序中,实现了控制台模式的服务端和客户端程序;本文中实现了GUI图形窗口的客户端,为接下来WEB中使用的applet打下基础。以前用JAVA都只是做B/S开发,所以这是第一次写图形界面的程序,因此,重温了《JAVA核心技术卷一》的第7-9章节。



jperfwin.java

import java.awt.EventQueue;
import javax.swing.JFrame;
public class jperfwin {
    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                appframe frame=new appframe();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
}


appframe.java

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.Border;
public  class appframe extends JFrame {
         
    public static final int DEFAULT_WIDTH=600;
    public static final int DEFAULT_HEIGHT=350;
    public framePanel frame;
         
    public appframe(){
        setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
        setTitle("网络应用带宽测试");
             
        initframe();
    }
    public void initframe(){
             
        frame=new framePanel();
        add(frame);
    }
    public class framePanel extends JPanel
    {
        public JTextField remoteAddrText;
        public JTextField remotePortText;
        public JTextField dataPortText;
        public JTextField messageNumberText;
        public JTextField messageSizeText;
        public JTextArea resultText;
        public JRadioButton upButton,downButton;
             
        public framePanel(){
             
            setLayout(null);           
            Border bset=BorderFactory.createEtchedBorder();
            Border bsettitle=BorderFactory.createTitledBorder(bset,"参数设置");        
            JPanel pset=new JPanel();
            pset.setBounds(10,10,550,120);
            pset.setLayout(null);
            pset.setBorder(bsettitle);
            String local_addr="";
             try {
                local_addr=InetAddress.getLocalHost().getHostAddress();
                     
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
                 
            JLabel remoteAddrLabel=new JLabel("服务器IP:");
            remoteAddrLabel.setBounds(10,30, 100, 20);
            pset.add(remoteAddrLabel);
            remoteAddrText=new JTextField(local_addr);
            remoteAddrText.setBounds(110, 30, 200, 20);
            pset.add(remoteAddrText);
                 
            JLabel remotePortLabel=new JLabel("端口:");
            remotePortLabel.setBounds(10,60,100,20);
            pset.add(remotePortLabel);
            remotePortText=new JTextField("5000");
            remotePortText.setBounds(110,60,100,20);
            pset.add(remotePortText);
                             
            JLabel messageNumberLabel=new JLabel("数据报文量:");
            messageNumberLabel.setBounds(10,90,100,20);
            pset.add(messageNumberLabel);
            messageNumberText=new JTextField("10000");
            messageNumberText.setBounds(110,90,100,20);
            pset.add(messageNumberText);
                 
            JLabel messageSizeLabel=new JLabel("报文大小(Byte):");
            messageSizeLabel.setBounds(210,90,100,20);
            pset.add(messageSizeLabel);
            messageSizeText=new JTextField("8192");
            messageSizeText.setBounds(310,90,100,20);
            pset.add(messageSizeText);
                 
            add(pset);     
                         
            Border btest=BorderFactory.createEtchedBorder();
            Border btesttitle=BorderFactory.createTitledBorder(btest,"测试结果");          
            JPanel ptest=new JPanel();
            ptest.setBounds(10,130,550,160);
            ptest.setLayout(null);
            ptest.setBorder(btesttitle);
                 
            resultText=new JTextArea(3,30);
            resultText.setLineWrap(true);
            resultText.setWrapStyleWord(true);         
            JScrollPane jsp=new JScrollPane(resultText);
            jsp.setBounds(10, 30, 520, 80);
            ptest.add(jsp);
                 
            JButton testButton=new JButton("Test");
            testButton.setBounds(10,120,100,30);
            testButton.addActionListener(new ButtonTestActionListener());
            ptest.add(testButton);
                 
            JLabel directionLabel=new JLabel("测试方向:");
            directionLabel.setBounds(150,120,100,30);
            ptest.add(directionLabel);
                 
            ButtonGroup group=new ButtonGroup();
            upButton=new JRadioButton("上行",false);
            upButton.setBounds(250,120,80,30);
            group.add(upButton);
            downButton=new JRadioButton("下行",true);
            downButton.setBounds(330,120,80,30);
            group.add(downButton);
                 
            ptest.add(upButton);
            ptest.add(downButton);
                 
            add(ptest);
        }
    }
         
             
    public class ButtonTestActionListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            try {
                     
                boolean direction=false;
                if(frame.upButton.isSelected()) direction=true;
                if(frame.downButton.isSelected()) direction=false;
                     
                jperf jtest=new jperf(frame.remoteAddrText.getText(),Integer.valueOf(frame.remotePortText.getText()),
                        Integer.valueOf(frame.messageNumberText.getText()),Integer.valueOf(frame.messageSizeText.getText()),direction);
                     
                String result=jtest.client();
                Date dateNow=new Date();
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                frame.resultText.append("\n\n"+sdf.format(dateNow)+"测试完成");
                frame.resultText.append(result);
                     
                int length=frame.resultText.getText().length();
                frame.resultText.setCaretPosition(length);
                     
            } catch (NumberFormatException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
                 
        }
             
    }  
}

编译代码:javac *.java

运行程序:java jperfwin,程序界面如图,实现的功能其实和控制台是一样的,都是调用jperf.java类执行测试。

wKioL1LZ6zmRxKeNAAGG9bpaSJ8040.jpg

你可能感兴趣的:(java程序,网速测试)