package com.timer.demo.newsocket;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import com.swtdesigner.SWTResourceManager;
public class ServerGUI {
private Text listServer;
private Text chartServer;
private Label otherIP;
private Label myIP;
private Display display = Display.getDefault();
private Shell qqShell = new Shell();
private Server server=new Server(this);
/**
* 开启服务按钮
*/
Button startServer ;
/**
* 停止服务按钮
*/
Button endServer ;
/**
* 构造函数
*/
public ServerGUI() {
this.init();
this.createContent();
this.start();
}
public void init() {
qqShell.setImage(SWTResourceManager.getImage(ServerGUI.class,
"/icons/user_business_boss.png"));
qqShell.setSize(599, 407);
qqShell.setText("服务端");
}
public void createContent() {
final Group group = new Group(qqShell, SWT.NONE);
group.setText("显示信息");
group.setBounds(10, 0, 401, 241);
listServer = new Text(group, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
listServer.setBounds(10, 22, 381, 209);
final Group group_1 = new Group(qqShell, SWT.NONE);
group_1.setText("操作");
group_1.setBounds(10, 247, 401, 105);
/**
* Text控件的风格: 主要可以指定为以下几种: SWT.BORDER:指定边框 SWT.SINGLE:单行Text SWT.MULTI
* :多行显示 SWT.V_SCROLL:显示垂直滚动条 SWT.H_SCROLL:显示水平滚动条
*
*/
chartServer = new Text(group_1, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
// chartServer.setBackgroundImage(SWTResourceManager.getImage(ServerGUI.class, "/icons/user.png"));
chartServer.setBounds(10, 29, 309, 65);
chartServer.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
if (e.keyCode == 13) {
final String str=chartServer.getText();
new Thread(){
public void run(){
server.converSation(str);
}
}.start();
}
}
});
final Button button = new Button(group_1, SWT.NONE);
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
final String str=chartServer.getText();
new Thread(){
public void run(){
server.converSation(str);
}
}.start();
}
});
button.setBounds(343, 72, 48, 22);
button.setText("发送");
final Group group_2 = new Group(qqShell, SWT.NONE);
group_2.setText("对方信息");
group_2.setBounds(433, 76, 148, 135);
final Label ipLabel = new Label(group_2, SWT.NONE);
ipLabel.setText("IP:");
ipLabel.setBounds(10, 28, 30, 12);
otherIP = new Label(group_2, SWT.NONE);
otherIP.setText("0.0.0.0");
otherIP.setBounds(46, 28, 70, 12);
final Group group_3 = new Group(qqShell, SWT.NONE);
group_3.setText("我的信息");
group_3.setBounds(433, 217, 148, 135);
final Label ipLabel_1 = new Label(group_3, SWT.NONE);
ipLabel_1.setText("IP:");
ipLabel_1.setBounds(10, 44, 30, 12);
myIP = new Label(group_3, SWT.NONE);
myIP.setText("0.0.0.0");
myIP.setBounds(46, 44, 72, 12);
startServer = new Button(qqShell, SWT.NONE);
startServer.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
listServer.append("服务端---后台服务开始启动...\n");
setButtonState(false);
new Thread(){
public void run(){
server.start();
}
}.start();
}
});
startServer.setText("开启服务");
startServer.setBounds(433, 23, 60, 22);
endServer= new Button(qqShell, SWT.NONE);
endServer.setEnabled(false);
endServer.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
server.stop();
setButtonState(true);
}
});
endServer.setText("停止服务");
endServer.setBounds(521, 23, 60, 22);
}
public void setButtonState(boolean bFlag){
startServer.setEnabled(bFlag);
endServer.setEnabled(!bFlag);
if(bFlag){
startServer.setText("开启服务");
endServer.setText("已停止");
}else{
startServer.setText("已开启");
endServer.setText("停止服务");
}
}
public void start() {
qqShell.open();
qqShell.layout();
while (!qqShell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
// display.dispose();
}
/**
* Launch the application
*
* @param args
*/
public static void main(String[] args) {
new ServerGUI();
}
/**
* @return the listServer
*/
public Text getListServer() {
return listServer;
}
/**
* @return the chartServer
*/
public Text getChartServer() {
return chartServer;
}
/**
* @return the otherIP
*/
public Label getOtherIP() {
return otherIP;
}
/**
* @return the myIP
*/
public Label getMyIP() {
return myIP;
}
/**
* @return the qqShell
*/
public Shell getQqShell() {
return qqShell;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
SWT+SOCKET通信
共4个文件
java:4个
5星 · 超过95%的资源 需积分: 10 78 下载量 91 浏览量
2010-01-28
09:10:28
上传
评论
收藏 6KB RAR 举报
温馨提示
swt与socket的网络通信。一个服务端,一个客户端。并且UI组件和SOCKET是分开的。里面用到了关于SWT的线程处理。很不错! 运行: 先开启服务器。然后点客户端的连接即可。 需要说明的是里面有些图片。我没有上传。你可以把用到的地方注释了。不影响程序的运行。如有疑问请发邮件到 cuiran2001@163.com 谢谢大家!
资源推荐
资源详情
资源评论
收起资源包目录
newsocket.rar (4个子文件)
newsocket
Server.java 4KB
ClientGUI.java 5KB
ServerGUI.java 5KB
Client.java 4KB
共 4 条
- 1
cuiran
- 粉丝: 1182
- 资源: 30
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页