📄 chat.java
字号:
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class Chat
{
/**
* Launch the application
* @param args
*/
public String yourip;
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell chatApplicationShell = new Shell(SWT.MIN);
chatApplicationShell.addShellListener(new ShellAdapter() {
public void shellClosed(final ShellEvent e) {System.exit(0);}
});
chatApplicationShell.setFont(new Font(display,"微软雅黑", 10, SWT.NONE));
chatApplicationShell.setSize(500, 430);
chatApplicationShell.setText("Chat Application");
//
chatApplicationShell.open();
final Label Label = new Label(chatApplicationShell, SWT.NONE);
final Text text_in = new Text(chatApplicationShell, SWT.WRAP | SWT.V_SCROLL | SWT.MULTI | SWT.BORDER);
final Text text_out = new Text(chatApplicationShell, SWT.WRAP | SWT.V_SCROLL | SWT.MULTI | SWT.BORDER);
Label.setFont(new Font(display,"微软雅黑", 9, SWT.NONE));
Label.setText("输入对方IP:");
Label.setBounds(33, 10, 71, 17);
final Text text_yourip = new Text(chatApplicationShell, SWT.BORDER);
text_yourip.setBounds(110, 9, 143, 18);
final Label label = new Label(chatApplicationShell, SWT.NONE);
label.setFont(new Font(display,"微软雅黑", 9, SWT.NONE));
label.setText("本机IP:");
label.setBounds(259, 10, 46, 17);
final Text text_myip = new Text(chatApplicationShell, SWT.BORDER);
text_myip.setBackground(new Color(display,255, 255, 255));
text_myip.setEditable(false);
text_myip.setBounds(311, 9, 143, 18);
InetAddress myip=null;
try{myip=InetAddress.getLocalHost();}
catch(Exception e) {}
text_myip.setText(myip.toString());
text_in.setFont(new Font(display,"微软雅黑", 10, SWT.NONE));
text_in.setBackground(new Color(display,255, 255, 255));
text_in.setEditable(false);
text_in.setBounds(10, 46, 472, 138);
final Composite composite = new Composite(chatApplicationShell, SWT.NONE);
composite.setBounds(10, 183, 472, 20);
final Button Button_textoutColor = new Button(composite, SWT.NONE);
Button_textoutColor.addMouseListener(new MouseAdapter() {
public void mouseDown(final MouseEvent e) {
ColorDialog cd = new ColorDialog(chatApplicationShell,SWT.NONE);
cd.setText("Sample Color Dialog");
RGB rgb = cd.open();
if(rgb!=null)
{
text_out.setBackground(new Color(display, rgb.red,rgb.green,rgb.blue));
}
}
});
Button_textoutColor.setText("C2");
Button_textoutColor.setBounds(100, 0, 24, 20);
final Button Button_fontColor = new Button(composite, SWT.NONE);
Button_fontColor.addMouseListener(new MouseAdapter() {
public void mouseDown(final MouseEvent e) {
FontDialog fd = new FontDialog(chatApplicationShell, SWT.NONE);
fd.setText("Sample Font Dialog");
FontData d = fd.open();
if(d!=null)
{
text_out.setFont(new Font(display,d));
text_in.setFont(new Font(display,d));
}
}
});
Button_fontColor.setText("A");
Button_fontColor.setBounds(40, 0, 24, 20);
final Button Button_textinColor = new Button(composite, SWT.NONE);
Button_textinColor.addMouseListener(new MouseAdapter() {
public void mouseDown(final MouseEvent e) {
ColorDialog cd = new ColorDialog(chatApplicationShell,SWT.NONE);
cd.setText("Sample Color Dialog");
RGB rgb = cd.open();
if(rgb!=null)
{
text_in.setBackground(new Color(display, rgb.red,rgb.green,rgb.blue));
}
}
});
Button_textinColor.setText("C1");
Button_textinColor.setBounds(70, 0, 24, 20);
final Button pingButton = new Button(composite, SWT.FLAT);
pingButton.addMouseListener(new MouseAdapter() {
public void mouseDown(final MouseEvent e) {
String regex="\\d{1,}\56\\d{1,}\56\\d{1,}\56\\d{1,}";
String yourip=text_yourip.getText().toString();
if(yourip.matches(regex))
{
try
{
Runtime ce=Runtime.getRuntime();
InputStream in=(InputStream) ce.exec("ping "+text_yourip.getText().toString()).getInputStream();
BufferedInputStream bin=new BufferedInputStream(in);
byte pingInfo[]=new byte[100];
int n;
while((n=bin.read(pingInfo,0,100))!=-1)
{
String s=null;
s=new String(pingInfo,0,n);
text_in.append(s);
}
text_in.append("Over!\n\n");
}
catch(Exception ee)
{
System.out.println(ee);
}
}
else
{
text_in.append("提示:你输入的对方ip不正确,请重新输入\n");
}
}
});
pingButton.setText("ping");
pingButton.setBounds(183, 0, 44, 20);
final Button backgroundButton = new Button(composite, SWT.NONE);
backgroundButton.addMouseListener(new MouseAdapter() {
public void mouseDown(final MouseEvent e) {
ColorDialog cd = new ColorDialog(chatApplicationShell,SWT.NONE);
cd.setText("Sample Color Dialog");
RGB rgb = cd.open();
if(rgb!=null)
{
chatApplicationShell.setBackground(new Color(display, rgb.red,rgb.green,rgb.blue));
}
}
});
backgroundButton.setText("B");
backgroundButton.setBounds(130, 0, 24, 20);
text_out.setFont(new Font(display,"微软雅黑", 10, SWT.NONE));
text_out.setBounds(10, 203, 472, 154);
final Button button_close = new Button(chatApplicationShell, SWT.NONE);
button_close.addMouseListener(new MouseAdapter() {
public void mouseUp(final MouseEvent e) {
System.exit(0);
}
});
button_close.setText("关闭");
button_close.setBounds(339, 363, 44, 23);
final Button button_send = new Button(chatApplicationShell, SWT.NONE);
button_send.addMouseListener(new MouseAdapter() {
public void mouseDown(final MouseEvent e) {
String regex="\\d{1,}\56\\d{1,}\56\\d{1,}\56\\d{1,}";
String yourip=text_yourip.getText().toString();
if(yourip.matches(regex))
{
byte buffer[]=text_out.getText().trim().getBytes();
try
{
InetAddress address=InetAddress.getByName(text_yourip.getText().toString());
DatagramPacket data_pack=new DatagramPacket(buffer,buffer.length,address,2009);
DatagramSocket mail_data=new DatagramSocket();
mail_data.send(data_pack);
text_in.append("我:"+text_out.getText()+"\n");
text_out.setText("");
}
catch(Exception ee) {}
}
else
{
text_in.append("提示:你输入的对方ip不正确,请重新输入\n");
}
}
});
button_send.setText("发送");
button_send.setBounds(406, 363, 44, 23);
chatApplicationShell.layout();
new Thread()//接收数据包线程
{
public void run()
{
DatagramPacket pack=null;
DatagramSocket mail_data=null;
byte data[]=new byte[8192];
try
{
pack=new DatagramPacket(data,data.length);
mail_data=new DatagramSocket(2008);
}
catch(Exception e) {System.out.println(""+e.toString());}
while(true)
{
if(mail_data==null) {break;}
else
{
try
{
mail_data.receive(pack);
int length=pack.getLength();
final String message=new String(pack.getData(),0,length);
display.asyncExec(new Runnable() {
public void run() {
text_in.append("对方:"+message+"\n");
}
});
}
catch(Exception ee) {System.out.println(""+ee.toString());}
}
}
}
}.start();
while (!chatApplicationShell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -