62,614
社区成员
发帖
与我相关
我的任务
分享
package send;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.FlowLayout;
public class send
{
public static int n = 0;//定义一个全局变量。
public static Frame f;
public static TextField rateText;
public static Label rateLabel;
public static Label ipListLabel;
public static TextArea ipText;
public static Button btStart;
public static Button btEnd;
public static TextArea debugText;
public static JTabbedPane jtp;
public static JPanel jp1;
public static JPanel jp2;
public static double rate;//发送速率
public static ArrayList<String> receiveIp = new ArrayList<String>();//接收者ip集合
public static void init(){
f = new Frame("send");
f.setLayout(new FlowLayout());
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
screenSize.setSize(screenWidth/2, screenHeight/2);
f.setPreferredSize(screenSize);
f.setBounds(600,100,400,200);
f.setLocationRelativeTo(null);
f.setLayout(null);
//UDP
jtp = new JTabbedPane();
jp1 = new JPanel();
jp2 = new JPanel();
jtp.add("UDP",jp1);
jtp.add("TCP",jp2);
f.add(jtp);
rateLabel = new Label("Send Rate(M/s)");
rateLabel.setLocation(50, 50);
rateLabel.setSize(200, 50);
rateLabel.setBackground(Color.YELLOW);
f.add(rateLabel);
ipListLabel = new Label("Receiver IP List");
ipListLabel.setLocation(300, 50);
ipListLabel.setSize(300,50);
ipListLabel.setBackground(Color.YELLOW);
f.add(ipListLabel);
btStart = new Button("Start");
btStart.setLocation(650, 50);
btStart.setSize(100, 50);
btStart.setBackground(Color.GREEN);
f.add(btStart);
btEnd = new Button("End");
btEnd.setLocation(800, 50);
btEnd.setSize(100, 50);
btEnd.setBackground(Color.RED);
f.add(btEnd);
rateText = new TextField();
rateText.setLocation(50, 120);
rateText.setSize(200, 50);
rateText.setBackground(Color.CYAN);
f.add(rateText);
ipText = new TextArea();
ipText.setLocation(300, 120);
ipText.setSize(300, 300);
ipText.setBackground(Color.CYAN);
f.add(ipText);
debugText = new TextArea();
debugText.setLocation(650, 120);
debugText.setSize(200, 300);
debugText.setBackground(Color.CYAN);
f.add(debugText);
f.pack();
f.setVisible(true);
}
public static void addEvents(){
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
//为按钮添加事件监听器.
btStart.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource() == btStart){
udpStart();
}
}
});
btEnd.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource() == btEnd){
udpEnd();
}
}
});
// 添加选项卡选中状态改变的监听器
jtp.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
System.out.println("当前选中的选项卡: " + jtp.getSelectedIndex());
}
});
}
protected static void udpStart() {
// TODO Auto-generated method stub
//获取参数
rate = Double.valueOf(rateText.getText());
String strs = ipText.getText();
String[] split = strs.split("\r\n");
receiveIp.clear();
for(int i=0;i<split.length;i++)
receiveIp.add(split[i]);
int i=0;
while(true)
{
rateText.setText("start.i="+i);
i++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
protected static void udpEnd() {
// TODO Auto-generated method stub
}
public static void main(String[] args)
{
init();
addEvents();
}
}