Topic: 请教代码错误在哪里(Chatroom)

  Print this page

1.请教代码错误在哪里(Chatroom) Copy to clipboard
Posted by: applepingmei
Posted on: 2005-07-30 23:34

下面的代码报错为:ChatRoom.java,不知道怎么会事,请指教

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.Timer;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import test.socket.Client;

/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ChatRoom extends JFrame implements ActionListener, ListSelectionListener
{
private String host = "localhost";
private int port = 30000;
private String userName;
private JLabel lblChatRoom;
JTextArea txtMessages;
private JScrollPane jspTxtMsgPane;
private JTextField txtMsg;
private JScrollPane jspSendMsgPane;
private JButton msgSendBtn;
private JButton userLogoutBtn;
private JLabel lblChatToWho;
private JTextField nameChatToWho;

private JLabel lblUserList;
private JTextArea txtListUsers;
private JList userList;
private JScrollPane jspUserListPane;

private PrintWriter out = null;

private String chatMode = "public";

public ChatRoom(String userName) throws IOException
{
//t.start();
initRoom(userName);
ChatClient client = new ChatClient(this);
if (client.isConnected())
{
this.out = client.getOutputStream();
out.println("login;" + userName);
}
else
appendMessage("[] : ӷʧܣ");
}

private void initRoom(String userName)
{
this.userName = userName;
this.setTitle("FunChat : " + userName);//ôڱ
JPanel panel = new JPanel();//
panel.setLayout(new GridBagLayout());//
GridBagConstraints gbCons = new GridBagConstraints();//Լ
//ұǩpanel
gbCons.gridx = 0;
gbCons.gridy = 0;
lblChatRoom = new JLabel("Chat Room", SwingConstants.LEFT);
panel.add(lblChatRoom, gbCons);
//panel
gbCons.gridx = 0;
gbCons.gridy = 1;
gbCons.gridwidth = 1;
gbCons.gridheight = 1;
gbCons.weightx = 1.0;
gbCons.weighty = 1.0;
txtMessages = new JTextArea(25, 35);//һ25У35е
txtMessages.setEditable(false);
jspTxtMsgPane = new JScrollPane(txtMessages,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);//ӵJScrollPane
panel.add(jspTxtMsgPane, gbCons);
//panel
gbCons.gridx = 0;
gbCons.gridy = 3;
gbCons.gridwidth = 1;
gbCons.gridheight = 1;
gbCons.weightx = 1.0;
gbCons.weighty = 1.0;
txtMsg = new JTextField(35);
// jspSendMsgPane = new JScrollPane(txtMsg,
// JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
// JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// panel.add(jspSendMsgPane, gbCons);
panel.add(txtMsg, gbCons);
//ӷͰťpanel
gbCons.gridx = 1;
gbCons.gridy = 3;
gbCons.gridwidth = 1;
gbCons.gridheight = 1;
gbCons.weightx = 1.0;
gbCons.weighty = 1.0;
gbCons.anchor = GridBagConstraints.WEST;
msgSendBtn = new JButton("Send");
panel.add(msgSendBtn, gbCons);
msgSendBtn.addActionListener(this);//ΪͰťע

JPanel functionPanel = new JPanel();
functionPanel.setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
JCheckBox jChatMode = new JCheckBox("˽",false);
ItemListener iListener = new ItemListener()
{
public void itemStateChanged(ItemEvent evt)
{
System.out.println("Item event : " + evt.getStateChange());
if(evt.getStateChange() == 1)
{
chatMode = "private";
}
else
{
chatMode = "public";
}
System.out.println("Chat Mode : " + chatMode);
}

};
jChatMode.addItemListener(iListener);
functionPanel.add(jChatMode);
lblChatToWho = new JLabel("");
functionPanel.add(lblChatToWho);
nameChatToWho = new JTextField(10);
nameChatToWho.setText("");
functionPanel.add(nameChatToWho);
gbCons.gridx = 0;
gbCons.gridy = 2;
gbCons.gridwidth = 1;
gbCons.gridheight = 1;
gbCons.weightx = 1.0;
gbCons.weighty = 1.0;


panel.add(functionPanel, gbCons);

JPanel btnPanel = new JPanel();
userLogoutBtn = new JButton("Logout");
userLogoutBtn.addActionListener(this);
//add a listerener to the window
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e1)
{

}
});
btnPanel.add(userLogoutBtn);
gbCons.gridx = 0;
gbCons.gridy = 4;
gbCons.gridwidth = 1;
gbCons.gridheight = 1;
gbCons.weightx = 1.0;
gbCons.weighty = 1.0;
gbCons.anchor = GridBagConstraints.EAST;
gbCons.fill = GridBagConstraints.HORIZONTAL;
panel.add(btnPanel, gbCons);

gbCons.gridx = 1;
gbCons.gridy = 0;
lblUserList = new JLabel("Online Users", SwingConstants.LEFT);
panel.add(lblUserList, gbCons);

gbCons.gridx = 1;
gbCons.gridy = 1;
gbCons.gridwidth = 1;
gbCons.gridheight = 1;
gbCons.weightx = 1.0;
gbCons.weighty = 1.0;
userList = new JList(new Vector());
userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
userList.setVisibleRowCount(28);
//userList.setFixedCellHeight(56);//õԪĸ߶
userList.setFixedCellWidth(15);//õԪĿ
userList.addListSelectionListener(this);
JScrollPane userListScrollPane = new JScrollPane(userList);

// txtListUsers = new JTextArea(25, 10);
// txtListUsers.setEditable(false);
// jspUserListPane = new JScrollPane(txtListUsers,
// JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
// JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
panel.add(userListScrollPane, gbCons);

this.getContentPane().add(panel);//panelӵݴ(עֱӵFrameϵWink
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.pack();
//this.setSize(546, 567);//ôʾߴ
this.setVisible(true);//ôǷɼ
txtMsg.requestFocus(true);//ý
}

/*
* (non-Javadoc)
*
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent evt)
{
// TODO Auto-generated method stub
JButton button = (JButton) evt.getSource();
if (button.equals(msgSendBtn))
{
String outMessage = txtMsg.getText();
String who = nameChatToWho.getText();
if(who.equalsIgnoreCase(""))
{
who = "roomall";
}
out.println("chat;" + this.chatMode + " " + who + ";" + outMessage);
txtMsg.setText("");
txtMsg.requestFocus(true);
}
else
{
System.exit(0);
}
}

public void appendMessage(String message)
{
//System.out.println("Call appendMessage.");
txtMessages.setText(txtMessages.getText() + message);
}

public void refreshUserList(String names)
{
String toWho = this.nameChatToWho.getText();
//System.out.println("Call refreshUserList.");
// int pos = names.indexOf("return_list>");
// String strTemp = names.substring(pos + 12);
// pos = strTemp.indexOf(":");
// strTemp = strTemp.substring(pos + 1);
//this.txtListUsers.setText("king");
Vector v = new Vector();
v.addElement("king");
v.addElement("Ryan");
for(int i=0; i < 8; i++)
{
v.addElement("user" + i);
}
this.userList.setListData(v);
this.nameChatToWho.setText(toWho);
}

public static void main(String[] args)
{
try
{
ChatRoom room = new ChatRoom("1;king");
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}

//Timer t = new Timer(5000, new TimerAction());

class TimerAction implements ActionListener
{
Socket toServer;
ObjectInputStream streamFromServer;
BufferedReader in;
PrintStream streamToServer;

public void actionPerformed(ActionEvent e2)
{
try
{
toServer = new Socket("127.0.0.1", 30000);
in = new BufferedReader(new InputStreamReader(toServer.getInputStream()));
streamToServer = new PrintStream(toServer.getOutputStream());
streamToServer.println("list;1");
//streamFromServer = new ObjectInputStream(toServer
// .getInputStream());
while(true)
{System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
String inStr = in.readLine();
System.out.println(inStr);
if(inStr.startsWith("return_list>"))
{
int pos = inStr.indexOf("return_list>");
String strTemp = inStr.substring(pos + 12);
pos = strTemp.indexOf(":");
strTemp = strTemp.substring(pos + 1);
txtListUsers.setText("");
txtListUsers.append(strTemp);
txtListUsers.append("\n");
break;
}

//send a message to the server

//show the online users

}
}//end of try
catch (Exception e)
{
System.out.println("Exception " + e);
}
}//end of actionPerformed
}//end of TimerListener class

/* (non-Javadoc)
* @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
*/
public void valueChanged(ListSelectionEvent event)
{
// TODO Auto-generated method stub
JList source = (JList)event.getSource();
String value = (String)source.getSelectedValue();
nameChatToWho.setText(value);
}
}


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923