private static JLabel[] totalu; private static int num; private String username; private String servername; private int width,height; private String title; JFrame top; JFrame bottom; JFrame menu; JFrame user; Container topc; Container menuc; Container bottomc; Container c; Container u;
JTextArea messagearea; JTextArea userarea;
JScrollPane textAreaScrollPane; JLabel tishi; JTextField mess; JButton send; JButton ying; JButton cancle; JButton login; JButton reg; Client client;
MultiJabberServer server;
public MainBord(int width,int height,String title,String name,String servername,int port) {
totalu = new JLabel[100]; num = 0;
username = name;
this.servername = servername; this.width = width; this.height = height; this.title = title; top = new JFrame(); bottom = new JFrame(); menu = new JFrame(); user =new JFrame();
topc = top.getContentPane();
bottomc = bottom.getContentPane(); menuc = menu.getContentPane(); c = this.getContentPane();
u = user.getContentPane();
topc.setLayout(new BorderLayout()); bottomc.setLayout(new BorderLayout()); menuc.setLayout(new GridLayout()); c.setLayout(new BorderLayout()); u.setLayout(new BorderLayout()); tishi = new JLabel(\消息:\ mess = new JTextField(); send = new JButton(\发送\ messagearea = new JTextArea(); messagearea.setEditable(false);
textAreaScrollPane = new JScrollPane(messagearea); ying = new JButton(\隐身\ cancle = new JButton(\退出\ login = new JButton(\登陆\ reg = new JButton(\注册\
userarea = new JTextArea(10,10);
userarea.setBackground(new Color(180,180,180)); userarea.setForeground(new Color(150,180,150)); userarea.setLayout(new GridLayout(20,1)); client = new Client(username,servername,port); menuc.add(ying); menuc.add(cancle); menuc.add(login); menuc.add(reg);
topc.add(textAreaScrollPane,BorderLayout.CENTER); topc.add(menuc,BorderLayout.NORTH); bottomc.add(tishi,BorderLayout.WEST); bottomc.add(mess,BorderLayout.CENTER); bottomc.add(send,BorderLayout.EAST);
u.add(new JButton(\当前在线用户\ u.add(userarea,BorderLayout.CENTER); c.add(topc,BorderLayout.CENTER); c.add(bottomc,BorderLayout.SOUTH); //c.add(menuc,BorderLayout.NORTH); c.add(u,BorderLayout.EAST); setSize(width,height); this.setTitle(title);
mess.addActionListener(this); send.addActionListener(this); cancle.addActionListener(this);
this.setDefaultCloseOperation(EXIT_ON_CLOSE); }
public void actionPerformed(ActionEvent e)
{
if((e.getSource()==mess)||(e.getSource()==send)) {
String message=mess.getText(); mess.setText(\ try {
client.sendMessage(message); }
catch(Throwable ex) {
JOptionPane.showMessageDialog(this,\信息发送出现错误:\聊天错误\ } } else if(e.getSource()==cancle) { JOptionPane.showMessageDialog(this,\客户端将关闭!\信息提示]\JOptionPane.CLOSED_OPTION); this.dispose(); } }
public void run() {
String mess; while(true) { try {
mess = client.receiveMessage(); System.out.println(mess);
if (mess.equals(\ {
refreshUser(client.receiveMessage(),totalu,num); num++; } else
messagearea.append(mess+\ }catch(Throwable te){
JOptionPane.showMessageDialog(this,\服务器已关闭!\聊天错误\JOptionPane.ERROR_MESSAGE); break; }
textAreaScrollPane.getVerticalScrollBar().setValue(textAreaScrollPane.getVerticalScrollBar().get
Maximum()); } }
public static void main(String [] args) { }
public void refreshUser(String username, JLabel[] j,int k) {
// for(int i = 0;i // if (username.equals(j[i].getText())) // { // continue; // } JLabel adduser = new JLabel(username); adduser.setForeground(new Color(100, 100, 200)); userarea.add(adduser); } } //============================程序结束=======================// 4.4 程序测试 4.4.1 JBuilder debug JBuilder debug也就是我们在前面学过的JBuilder中的程序调试,下面我们就来调试一下我们的这个聊天室程序。 (1)在JBuilder中打开我们要调试的聊天室程序,点击 就是对程序的调试运行,要是 程序中有错误的话就会给出提示,我们可以根据系统给的提示很方便的找到错误的所在,如图4-7所示: 图4-7调试程序 (2)根据图4-7所给出的错误,我们可以大致的知道错在那里,我们双击相应的错误也可以直接找到在程序中出错的代码行,如图4-8所示: 图4-8在程序中的相应错误 (3)红色的字体就是出错处相对应的代码行,这样我们就可以很方便的来进行修改。当然也有的程序在语法和书写上是没有错误的,可能在调试中发现不了,这时我们还可以用设置断点的方法来找到错误。如图4-9所示: 图4-9设置断点 (4)图4-9中的程序前方出现的红色圆点就是我们设置的断点,也就是程序在运行到这一行代码的时候要中断运行,由手到控制程序的继续运行。 这是JBuilder中的debug调试运行程序,当我们的程序中没有错误的时候我们就会看到没有报错的情况下程序顺利运行。 4.4.2 测试报告 本软件系统的测试阶段可分为:单元测试、集成测试、确认测试、系统综合测试和系统验收。每个阶段都要经历测试需求分析、测试过程设计、测试实现、测试实施、测试评价、测试维护六个步骤。如表4-1所示: