java课程设计报告-聊天室(2)

2019-01-07 16:37

课 程 设 计 报 告 书 专 用 纸

/**

* 程序初始化函数

*/

public void init(){ Container contentPane = getContentPane();

contentPane.setLayout(new BorderLayout());

//添加菜单栏

serviceMenu.add (portItem); serviceMenu.add (startItem); serviceMenu.addSeparator(); serviceMenu.add (stopItem); serviceMenu.addSeparator(); serviceMenu.add (exitItem); jMenuBar.add (serviceMenu); helpMenu.add (helpItem); jMenuBar.add (helpMenu); setJMenuBar (jMenuBar); //初始化按钮

portSet = new JButton(\端口设置\

startServer = new JButton(\启动服务\ stopServer = new JButton(\停止服务\ exitButton = new JButton(\退出\ //将按钮添加到工具栏

toolBar.add(portSet);

toolBar.addSeparator();//添加分隔栏 toolBar.add(startServer); toolBar.add(stopServer);

toolBar.addSeparator();//添加分隔栏 toolBar.add(exitButton);

contentPane.add(toolBar,BorderLayout.NORTH); //初始时,令停止服务按钮不可用 stopServer.setEnabled(false); stopItem .setEnabled(false); //为菜单栏添加事件监听

portItem.addActionListener(this); startItem.addActionListener(this); stopItem.addActionListener(this); exitItem.addActionListener(this); helpItem.addActionListener(this);

//添加按钮的事件侦听 第 5 页 共 18 页

课 程 设 计 报 告 书 专 用 纸

portSet.addActionListener(this); startServer.addActionListener(this); stopServer.addActionListener(this); exitButton.addActionListener(this); combobox = new JComboBox(); combobox.insertItemAt(\所有人\combobox.setSelectedIndex(0); messageShow = new JTextArea(); messageShow.setEditable(false); //添加滚动条

messageScrollPane = new JScrollPane(messageShow,

JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

messageScrollPane.setPreferredSize(new Dimension(400,400)); messageScrollPane.revalidate();

showStatus = new JTextField(35); showStatus.setEditable(false); sysMessage = new JTextField(24); sysMessage.setEnabled(false); sysMessageButton = new JButton(); sysMessageButton.setText(\发送\//添加系统消息的事件侦听

sysMessage.addActionListener(this);

sysMessageButton.addActionListener(this); sendToLabel = new JLabel(\发送至:\messageLabel = new JLabel(\发送消息:\downPanel = new JPanel();

girdBag = new GridBagLayout(); downPanel.setLayout(girdBag);

girdBagCon = new GridBagConstraints(); girdBagCon.gridx = 0; girdBagCon.gridy = 0;

girdBagCon.gridwidth = 3; girdBagCon.gridheight = 2; girdBagCon.ipadx = 5; girdBagCon.ipady = 5;

JLabel none = new JLabel(\

girdBag.setConstraints(none,girdBagCon); 第 6 页 共 18 页

课 程 设 计 报 告 书 专 用 纸

downPanel.add(none);

girdBagCon = new GridBagConstraints(); girdBagCon.gridx = 0; girdBagCon.gridy = 2;

girdBagCon.insets = new Insets(1,0,0,0); girdBagCon.ipadx = 5; girdBagCon.ipady = 5;

girdBag.setConstraints(sendToLabel,girdBagCon); downPanel.add(sendToLabel);

girdBagCon = new GridBagConstraints(); girdBagCon.gridx =1;

girdBagCon.gridy = 2;

girdBagCon.anchor = GridBagConstraints.LINE_START; girdBag.setConstraints(combobox,girdBagCon); downPanel.add(combobox); girdBagCon = new GridBagConstraints(); girdBagCon.gridx = 0;

girdBagCon.gridy = 3;

girdBag.setConstraints(messageLabel,girdBagCon); downPanel.add(messageLabel);

girdBagCon = new GridBagConstraints(); girdBagCon.gridx = 1; girdBagCon.gridy = 3;

girdBag.setConstraints(sysMessage,girdBagCon); downPanel.add(sysMessage);

girdBagCon = new GridBagConstraints(); girdBagCon.gridx = 2;

girdBagCon.gridy = 3;

girdBag.setConstraints(sysMessageButton,girdBagCon); downPanel.add(sysMessageButton); girdBagCon = new GridBagConstraints(); girdBagCon.gridx = 0; girdBagCon.gridy = 4;

girdBagCon.gridwidth = 3;

girdBag.setConstraints(showStatus,girdBagCon); downPanel.add(showStatus);

contentPane.add(messageScrollPane,BorderLayout.CENTER); contentPane.add(downPanel,BorderLayout.SOUTH); 第 7 页 共 18 页

课 程 设 计 报 告 书 专 用 纸

} /**

* 事件处理

*/

public void actionPerformed(ActionEvent e) { Object obj = e.getSource();

if (obj == startServer || obj == startItem) { //启动服务端

startService();

}

else if (obj == stopServer || obj == stopItem) { //停止服务端

int j=JOptionPane.showConfirmDialog( this,\真的停止服务吗?\停止服务\

JOptionPane.YES_OPTION,JOptionPane.QUESTION_MESSAGE);

//关闭程序时的操作

this.addWindowListener( new WindowAdapter(){ public void windowClosing(WindowEvent e){

}

stopService(); System.exit(0);

} );

if (j == JOptionPane.YES_OPTION){ }

stopService();

}

else if (obj == portSet || obj == portItem) { //端口设置 //调出端口设置的对话框 PortConf portConf = new PortConf(this);

portConf.show();

}

else if (obj == exitButton || obj == exitItem) { //退出程序 int j=JOptionPane.showConfirmDialog( this,\真的要退出吗?\退出\ }

JOptionPane.YES_OPTION,JOptionPane.QUESTION_MESSAGE);

if (j == JOptionPane.YES_OPTION){ stopService(); }

System.exit(0);

else if (obj == helpItem) { //菜单栏中的帮助 //调出帮助对话框

第 8 页 共 18 页

课 程 设 计 报 告 书 专 用 纸

} /**

* 启动服务端 */

public void startService(){

try{

serverSocket = new ServerSocket(port,10);

messageShow.append(\服务端已经启动,在\端口侦听...\\n\startServer.setEnabled(false); startItem.setEnabled(false); portSet.setEnabled(false); portItem.setEnabled(false); stopServer .setEnabled(true); stopItem .setEnabled(true); sysMessage.setEnabled(true);

Help helpDialog = new Help(this); helpDialog.show();

}

else if (obj == sysMessage || obj == sysMessageButton) { //发送系统消息 sendSystemMessage(); }

}

catch (Exception e){ }

//System.out.println(e);

userLinkList = new UserLinkList();

listenThread = new ServerListen(serverSocket,combobox, messageShow,showStatus,userLinkList); listenThread.start();

} /**

* 关闭服务端 */

public void stopService(){ try{

//向所有人发送服务器关闭的消息 sendStopToAll();

listenThread.isStop = true; serverSocket.close();

int count = userLinkList.getCount();

第 9 页 共 18 页


java课程设计报告-聊天室(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:高效课堂 - 语文教学的必由之路

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: