📄 chatclient.java
字号:
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Dialog.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.JComponent.*;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JScrollPane;
import java.util.StringTokenizer;
//import com.ms.com.Dispatch;
public class ChatClient extends JApplet implements Runnable, ActionListener {
//protected Socket s = null;
protected ObjectInputStream i;
protected ObjectOutputStream o;
protected DataObject dO;
//protected JTextArea listclient;
protected JTextArea output;
protected JList listclient;
protected JTextField input;
protected JButton b,bd;
protected JButton send; //new added
protected Thread listener;
protected boolean first = true, kill=false;
protected String name = "";
protected JLabel initLabel, nameLabel;
BufferedReader in = null;
String serverResponse;
TextField tf;
public static Boolean fst = true;
public static String usrnm = "";
public ChatClient(){
}
public void init(){
Socket s = null;
String hostName = "afs34.njit.edu";
// String hostName = "localhost";
int portno = 2585;
setForeground(java.awt.Color.black);
setSize(200,200);
JLabel initLabel = new JLabel(" All Messages Are Here ");
JLabel initLabel1 = new JLabel(" Online USERS ");
//0.98f, 0.97f, 0.85f
initLabel1.setBackground(new Color(0.98f, 0.97f, 0.85f));//new added
initLabel.setForeground(Color.black);
initLabel1.setForeground(Color.blue);
setLayout (new BorderLayout ());
listclient=new JList();
listclient.setForeground(Color.red);
listclient.setOpaque(true);
listclient.setBorder(BorderFactory.createRaisedBevelBorder());
listclient.setBackground(new Color(0.98f, 0.97f, 0.85f)); ///new added
JScrollPane listScrollPane = new JScrollPane(listclient);
Dimension preferredSize1 = new Dimension(200, 75);
listScrollPane.setPreferredSize(preferredSize1);
listScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
output = new JTextArea ();
output.setEditable (false);
output.setForeground(Color.blue);
output.setBorder(BorderFactory.createRaisedBevelBorder());
output.setBackground(new Color(0.90f, 0.950f, 0.90f));///new added
JScrollPane topScrollPane = new JScrollPane(output);
Dimension preferredSize = new Dimension(200, 75);
topScrollPane.setPreferredSize(preferredSize);
topScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
input = new JTextField(30);
input.setEditable (false);
input.addActionListener(this);
input.setForeground(java.awt.Color.BLUE);
input.setBorder(BorderFactory.createLoweredBevelBorder());
//ImageIcon ButtonIcon = new ImageIcon("icon.jpeg");
b = new JButton("CONNECT");
b.setVerticalTextPosition(AbstractButton.CENTER);
b.setHorizontalTextPosition(AbstractButton.LEADING);
b.setMnemonic(KeyEvent.VK_D);
b.addActionListener(this);
//new added.....
send = new JButton("SEND");
send.setVerticalTextPosition(AbstractButton.CENTER);
send.setHorizontalTextPosition(AbstractButton.LEADING);
send.addActionListener(this);
//new added.....
bd = new JButton("DISCONNECT");
bd.setVerticalTextPosition(AbstractButton.CENTER);
bd.setHorizontalTextPosition(AbstractButton.LEADING);
bd.addActionListener(this);
bd.setVisible(false);
//new added
JPanel bottompanel = new JPanel();
bottompanel.setLayout(new BorderLayout());
bottompanel.add("West",input);
bottompanel.add("East",send);
//new added
JPanel leftpanel = new JPanel();
leftpanel.setLayout(new BorderLayout());
leftpanel.add("North",initLabel);
leftpanel.add("Center",topScrollPane);
leftpanel.add("South",bottompanel);
leftpanel.setBorder(BorderFactory.createBevelBorder(10, Color.blue, Color.gray));
listclient.setBackground(new Color(0.98f, 0.97f, 0.85f));
JPanel rightpanel = new JPanel();
rightpanel.setLayout(new BorderLayout());
rightpanel.add("North",initLabel1);
rightpanel.add("Center",listScrollPane);
rightpanel.add("South",b);
rightpanel.setBorder(BorderFactory.createBevelBorder(2, Color.blue, Color.gray));
JPanel gridpanel = new JPanel();
gridpanel.setLayout(new BorderLayout());
add(rightpanel,BorderLayout.CENTER);
add(leftpanel, BorderLayout.WEST);
gridpanel.setBorder(BorderFactory.createEtchedBorder(Color.black,Color.gray));
b.requestFocus ();
dO=new DataObject();
try{
s = new Socket(hostName, portno);
o = new ObjectOutputStream(s.getOutputStream());
i = new ObjectInputStream(s.getInputStream());
}catch(Exception e){}
//if(!fst){
dO.setName("New User");
dO.setMessage("Connecting..");
try{
o.writeObject(dO);
}catch(IOException e){};
input.requestFocus ();
listener = new Thread (this);
listener.start ();
System.out.println(initLabel.getText());
}
//}
public synchronized void changeLabel(String newLabel){
initLabel.setText(newLabel);
}
public void stop(){
kill=true;
listener = null;
}
public void run () {
if(kill){
return;
}
try {
while (true) {
dO=(DataObject)i.readObject();
if(dO.getMessage().startsWith("@")){
listclient.clearSelection();
String s1 = dO.getMessage();
String tmp[] = new String[20];
int n = s1.length();
s1 = s1.substring(1, n);
StringTokenizer st = new StringTokenizer(s1);
int i = 0;
while (st.hasMoreTokens()) {
tmp[i] = (st.nextToken());
i++;
}
listclient.setListData(tmp);
}
else{
String line = dO.getName()+": "+dO.getMessage();
output.append(line + "\n");
}
}
} catch (IOException ex) {
} catch(ClassNotFoundException e){
} finally {
input.setVisible(false);
validate ();
try {
o.close ();
} catch (IOException ex) {
}
}
}
public void actionPerformed(ActionEvent e) {
try {
if (e.getSource() == input) {
dO.setName(name);
dO.setMessage(input.getText());
}
//new added
if (e.getSource() == send) {
dO.setName(name);
dO.setMessage(input.getText());
}
//new added
if(e.getSource() == b){
if(ChatClient.fst){
String s = (String)JOptionPane.showInputDialog("Please Enter User Name");
usrnm = s;
while(s == null || s.length() == 0){
JOptionPane.showMessageDialog(null,"You Have To Connect, To enter in Chat System");
return;
}
dO.setName(s);
name=s;
dO.setMessage("@ HAS ENTERED");
input.setEditable (true);
input.requestFocus ();
ChatClient.fst=false;
}
else{
//dO.setName(usrnm);
//dO.setMessage("#Disconnecting");
/*input.setEditable(false);
output.setEdiatbe(false);
b.setVisible(false);
listclient.setVisible(false);
send.setVisible(false);//new added
*/
//kill=true;
}
}
o.writeObject(dO);
o.flush ();
if(kill){
}
} catch (IOException ex) {
kill=true;
}
input.setText ("");
//b.setLabel("DisConnect");
b.setEnabled(false);
return;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -