📄 inputnametextfield.java
字号:
import java.awt.*;
import java.net.*;
import java.awt.event.*;
import java.io.*;
import java.util.Hashtable;
import javax.swing.JOptionPane;
public class InputNameTextField extends Panel implements ActionListener,
Runnable
{
TextField nameField = null;
String name = null;
TextField nickNameField = null;
String nickName = "";
Button enter = null, leave = null;
Choice colorChoice = null;
Socket userSocket = null;
DataInputStream in = null;
DataOutputStream out = null;
Thread chatThread = null;
boolean okToChat = false;
Hashtable listTable;
Hashtable bakgrdTable;
String[] bakColor = { "black", "blue", "cyan", "darkgray", "gray", "green",
"light gray", "magenta", "orange", "pink", "red", "white", "yellow" };
Color[] color = { Color.BLACK, Color.BLUE, Color.CYAN, Color.DARK_GRAY,
Color.GRAY, Color.GREEN, Color.LIGHT_GRAY, Color.MAGENTA,
Color.ORANGE, Color.PINK, Color.RED, Color.WHITE, Color.YELLOW };
public InputNameTextField(Hashtable listTable)
{
this.listTable = listTable;
nameField = new TextField(10);
nickNameField = new TextField(10);
enter = new Button("登录");
enter.setEnabled(true);
leave = new Button("退出");
enter.addActionListener(this);
leave.addActionListener(this);
bakgrdTable = new Hashtable();
colorChoice = new Choice();
colorChoice.setSize(60, 30);
for (int i = 0; i < bakColor.length; i++)
{
colorChoice.addItem(bakColor[i]);
bakgrdTable.put(bakColor[i], color[i]);
}
colorChoice.select(0);
colorChoice.addItemListener(new java.awt.event.ItemListener()
{
public void itemStateChanged(java.awt.event.ItemEvent e)
{
Color color = (Color) bakgrdTable.get(colorChoice
.getSelectedItem());
ClientChat.chatArea.pubArea.setBackground(color);
}
});
chatThread = new Thread(this);
add(new Label("姓名:"));
add(nameField);
add(new Label("昵称:"));
add(nickNameField);
add(enter);
add(leave);
add(new Label("聊天区背景色:"));
add(colorChoice);
leave.setEnabled(false);
}
public void setOkToChat(boolean b)
{
okToChat = b;
}
public boolean getOkToChat()
{
return okToChat;
}
public String getName()
{
return name;
}
public void setName(String s)
{
name = s;
}
public String getNickName()
{
return nickName;
}
public void setNickName(String s)
{
nickName = s;
}
public Socket getSocket()
{
return userSocket;
}
public void setSocketConnection(Socket socket, DataInputStream in,
DataOutputStream out)
{
this.userSocket = socket;
this.in = in;
this.out = out;
try
{
chatThread.start();
} catch (Exception e)
{
nameField.setText("" + e);
}
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == enter)
{
leave.setEnabled(true);
if (okToChat == true)
{
// ClientChat.remindLabel.setText("您正在聊天:" + name);
// JOptionPane.showConfirmDialog(null, "您正在聊天");
// nameField.setText(null);
} else
{
this.setName(nameField.getText());
this.setNickName(nickNameField.getText());
enter.setEnabled(false);
if (userSocket != null && name != null)
{
try
{
out.writeUTF("姓名:" + name + "昵称:" + nickName);
} catch (IOException e1)
{
// TODO 自动生成 catch 块
// nameField.setText("没有连通服务器" + e1);
e1.printStackTrace();
}
okToChat = true;
}
}
}
if (e.getSource() == leave)
{
try
{
out.writeUTF("用户离开:");
} catch (IOException e1)
{
// TODO 自动生成 catch 块
e1.printStackTrace();
}
okToChat = false;
nameField.setText(null);
nickNameField.setText("");
enter.setEnabled(true);
}
}
public void run()
{
String msg = null;
while (true)
{
if (in != null)
{
try
{
msg = in.readUTF();
} catch (IOException e)
{
nameField.setText("和服务器断开" + e);
}
}
if (msg.startsWith("可以聊天:"))
{
okToChat = true;
break;
} else if (msg.startsWith("聊天者:"))
{
String people = msg.substring(msg.indexOf(":") + 1);
listTable.put(people, people);
} else if (msg.startsWith("不可以聊天:"))
{
okToChat = false;
JOptionPane.showConfirmDialog(null, "该姓名已被占用", "登录错误",
JOptionPane.ERROR_MESSAGE);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -