package com.softeem.work;
public class Config1 {
public static final String TITLE = "飞秋客户端V1.0";
public static final int WIDTH = 400;
public static final int HEIGHT = 400;
public static final boolean HIDE = false;
public static final boolean SHOW = true;
}
package com.softeem.work;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import com.softeem.gui.Config;
public class Task1 extends JFrame{
private JLabel title; //标题
private JLabel nickname; //提示昵称
private JLabel recieverIP; //提示IP
private JLabel text; //提示内容
private JTextField input_nickname; //昵称
private JTextField input_recieverIP; //接收人IP地址
private JTextArea area; //文本框
private JButton button; //按钮
public Task1() throws SocketException
{
setTitle(Config1.TITLE);
setSize(Config1.WIDTH,Config1.HEIGHT);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
//设置布局方式,为null则代表控件可以根据自身设置的位置任意摆放
setLayout(null);
initUI();
setVisible(Config.SHOW);
}
private void initUI() throws SocketException {
//JLabel标题创建
title = new JLabel("飞秋客户端V1.0");
title.setBounds(100, 15, 200, 30);
title.setForeground(Color.BLACK);
title.setFont(new Font("楷体", Font.BOLD, 25));
//JLabel提示信息创建
nickname = new JLabel("昵称");
nickname.setBounds(50, 65, 100, 20);
nickname.setForeground(Color.BLUE);
nickname.setFont(new Font("黑体", Font.BOLD, 18));
recieverIP = new JLabel("接收人IP");
recieverIP.setBounds(20, 105, 100, 20);
recieverIP.setForeground(Color.BLUE);
recieverIP.setFont(new Font("黑体", Font.BOLD, 18));
text = new JLabel("内容");
text.setBounds(50, 150, 100, 20);
text.setForeground(Color.BLUE);
text.setFont(new Font("黑体", Font.BOLD, 18));
//JTextArea文本框创建
area = new JTextArea();
area.setBounds(100, 150, 200, 150);
area.setBorder(new LineBorder(new Color(57, 173, 255)));
//JTextField创建
input_nickname = new JTextField("我是一只山驴逼");
input_nickname.setBounds(100, 65, 200, 30);
input_recieverIP = new JTextField();
input_recieverIP.setBounds(100, 105, 200, 30);
//JButton创建
button = new JButton("发送");
button.setBounds(300, 310, 80, 30);
add(title);
add(input_nickname);
add(nickname);
add(recieverIP);
add(input_recieverIP);
add(area);
add(text);
add(button);
//为按钮绑定事件
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// String nickname1 = input_nickname.getText().trim();
String recieverIP1 = input_recieverIP.getText().trim();
String text1 = area.getText();
//创建网络通道
text1 = BASE_FORMAT + text1;
//将数据打包成数据报包
try {
DatagramSocket ds = new DatagramSocket();
DatagramPacket dp = null;
dp = new DatagramPacket(
text1.getBytes(), //需要发送的数据(字节数组)
text1.getBytes().length, //数据的长度(字节数组长度)
InetAddress.getByName(recieverIP1), //接收方ip地址
2425 //飞秋的端口号
);
//发送包裹
ds.send(dp);
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
}
static final String BASE_FORMAT = "1:100:我是一只山驴逼::32:";
public static void main(String[] args) throws SocketException {
new Task1();
}
}