Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java GUI 设计  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 请教代码错误在哪里(Chatroom)
applepingmei





发贴: 2
积分: 0
于 2005-07-30 23:34 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
下面的代码报错为: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);
}
}


why edited on 2005-07-31 01:18


学习java的工具(IDE)有什么?

话题树型展开
人气 标题 作者 字数 发贴时间
2936 请教代码错误在哪里(Chatroom) applepingmei 12097 2005-07-30 23:34

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   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