?? inputnametextarea.java~9~
字號(hào):
package com.soft.Team;
import java.awt.*;
import javax.swing.*;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import java.util.Hashtable;
import java.net.*;
import java.io.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class InputNameTextArea
extends JFrame implements Runnable{
JPanel contentPane;
String name=null;
XYLayout xYLayout1 = new XYLayout();
Label label1 = new Label();
CheckboxGroup group=null;
TextField nameFile = new TextField();
Checkbox male = new Checkbox();
Checkbox female = new Checkbox();
Button 進(jìn)入聊天室 = new Button();
Button 退出聊天室 = new Button();
Socket socket=null;
DataInputStream in=null;
DataOutputStream out=null;
boolean 能否聊天=false;
Hashtable ListTable;
Thread thread=null;
public InputNameTextArea(Hashtable listTable) {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit(ListTable);
}
catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit(Hashtable listTable) throws Exception {
this.ListTable=ListTable;
group=new CheckboxGroup();
contentPane = (JPanel) getContentPane();
contentPane.setLayout(xYLayout1);
setSize(new Dimension(400, 300));
setTitle("登入框架");
thread=new Thread(this);
label1.setText("用戶昵稱");
male.setForeground(Color.black);
male.setLabel("男");
female.setLabel("女");
進(jìn)入聊天室.setLabel("進(jìn)入聊天室");
進(jìn)入聊天室.addActionListener(new InputNameTextArea_進(jìn)入聊天室_actionAdapter(this));
退出聊天室.setLabel("退出聊天室");
退出聊天室.addActionListener(new InputNameTextArea_退出聊天室_actionAdapter(this));
contentPane.add(label1, new XYConstraints(34, 40, 102, 35));
contentPane.add(nameFile, new XYConstraints(148, 37, 171, 37));
contentPane.add(male, new XYConstraints(35, 101, 103, 29));
contentPane.add(female, new XYConstraints(151, 100, 118, 33));
contentPane.add(進(jìn)入聊天室, new XYConstraints(34, 166, 191, 35));
contentPane.add(退出聊天室, new XYConstraints(33, 218, 191, 36));
}
public void set能否聊天(boolean b)
{
能否聊天=b;
}
public boolean get能否聊天()
{
return 能否聊天;
}
public String getName()
{
return name;
}
public void setName(String s)
{
name=s;
}
public void setSocketConnection(Socket socket,DataInputStream in,DataOutputStream out)
{
this.socket=socket;
this.in=in;
this.out=out;
try{
thread.start();
}
catch(Exception e)
{
nameFile.setText(""+e);
}
}
public Socket getSocket()
{
return socket;
}
public void 進(jìn)入聊天室_actionPerformed(ActionEvent e) {
退出聊天室.setEnabled(true);
if(能否聊天==true)
{
nameFile.setText("您正在聊天:"+name);
}
else
{
this.setName(nameFile.getText());
String sex=group.getSelectedCheckbox().getLabel();
if(socket!=null&&name!=null)
{
try{
out.writeUTF("姓名:"+name+"性別:"+sex);
}
catch(IOException ee)
{
nameFile.setText("沒(méi)有連通服務(wù)器"+ee);
}
}
}
}
public void 退出聊天室_actionPerformed(ActionEvent e) {
try
{
out.writeUTF("用戶離開(kāi):");
}
catch(IOException ee)
{
}
}
public void run()
{
String message=null;
while(true)
{
if(in!=null)
{
try
{
message=in.readUTF();
}
catch(IOException e)
{
nameFile.setText("和服務(wù)器斷開(kāi)"+e);
}
}
if(message.startsWith("可以聊天:"))
{
能否聊天=true;
break;
}
else if(message.startsWith("聊天者:"))
{
String people=message.substring(message.indexOf(":")+1);
listTable.put(people,people);
}
else if(message.startsWith("不可以聊天:"))
{
能否聊天=false;
nameFile.setText("該昵稱已被占用");
}
}
}
}
class InputNameTextArea_退出聊天室_actionAdapter
implements ActionListener {
private InputNameTextArea adaptee;
InputNameTextArea_退出聊天室_actionAdapter(InputNameTextArea adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.退出聊天室_actionPerformed(e);
}
}
class InputNameTextArea_進(jìn)入聊天室_actionAdapter
implements ActionListener {
private InputNameTextArea adaptee;
InputNameTextArea_進(jìn)入聊天室_actionAdapter(InputNameTextArea adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.進(jìn)入聊天室_actionPerformed(e);
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -