📄 mypanel.java
字号:
/**
* Email: taorundong@126.com
*
* @author taorundong
* @version 1.00 07/02/04
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class MyPanel extends JPanel implements ActionListener{
private //private type is automatically synchronized
JTextField message = null;
JTextArea text = null;
JComboBox comboBox = null;
JScrollPane scrollPane = null;
String item[] = {"Chose one person to check!"};
JTextArea combo = null;
Hashtable clientHash = null;
String []mode = {"all of the people","one person"};
JComboBox getMode = null;
MyPanel(){
super();
this.setLayout(new BorderLayout());
comboBox = new JComboBox(item);
comboBox.addActionListener(this);
combo = new JTextArea();
text = new JTextArea();
message = new JTextField();
message.addActionListener(this);
getMode = new JComboBox(mode);
showMessage();
showComboBox();
showTextArea();
showLabel();
validate();
}
public void setClientHash(Hashtable temp){
clientHash = temp;
}
public void showMessage(){
JPanel temp1 = new JPanel(new GridLayout(1,2));
JLabel left = new JLabel();
left.setHorizontalAlignment(JLabel.LEFT);
left.setText("Welcome to the chating world! ");
temp1.add(left);
getMode.setForeground(Color.blue);
temp1.add(getMode);
JPanel temp = new JPanel(new GridLayout(1,2));
temp.add(temp1);
message.setToolTipText("Input the message here");
temp.add(message);
temp.validate();
this.add(temp,"South");
this.validate();
}
public void showComboBox(){
combo.setBackground(Color.pink);
combo.setEditable(false);
JPanel temp = new JPanel(new BorderLayout());
JLabel down = new JLabel();
down.setIcon(new ImageIcon("picture\\3.jpg"));
temp.add(comboBox,"North");
temp.add(combo,"Center");
temp.add(down,"South");
temp.validate();
this.add(temp,"West");
this.validate();
}
public String getMessageContent(){
return message.getText();
}
public void setMessageContent(String s){
text.append("\n"+s);
}
public JTextArea getJTextArea(){
return text;
}
public JComboBox getJComboBox(){
return comboBox;
}
public void showTextArea(){
text.setBackground(Color.GREEN);
text.setWrapStyleWord(false);
text.setLineWrap(true);
text.setAlignmentX(JTextArea.LEFT_ALIGNMENT);
text.setEditable(false);
text.validate();
scrollPane = new JScrollPane(text);
this.add(scrollPane,"Center");
this.validate();
}
public void showLabel(){
JPanel slip = new JPanel();
slip.setBackground(Color.yellow);
slip.setLayout(new GridLayout(2,1));
JLabel down = new JLabel();
JLabel top = new JLabel();
down.setIcon(new ImageIcon("picture\\1.jpg"));
top.setIcon(new ImageIcon("picture\\2.jpg"));
slip.add(top);
slip.add(down);
slip.validate();
this.add(slip,"East");
this.validate();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==comboBox){
new MusicThread("music\\press.wav");
if(((String)comboBox.getSelectedItem())=="Chose one person to check!"){
}
else{
combo.setText(integrateClientInfo());
}
}
if(e.getSource()==message){
new MusicThread("music\\send.wav");
text.append(message.getText()+"\n");
if((String)getMode.getSelectedItem()=="all of the people"){
outPutToEveryClient(message.getText());
}
else{
outPutToOnePerson(message.getText());
//send message to one person
}
message.setText("");//new output thread
}
}
public void outPutToOnePerson(String message){
Enumeration Enum = clientHash.elements();
while(Enum.hasMoreElements()){
People client = (People)Enum.nextElement();
if(client.getName()==(String)comboBox.getSelectedItem()){
try{
client.getOutputStream().writeUTF(message+" From Server");
}
catch(Exception e){
e.printStackTrace();
}
}
else{
return;
}
}
}
public void outPutToEveryClient(String message){
Enumeration Enum = clientHash.elements();
while(Enum.hasMoreElements()){
People client = (People)Enum.nextElement();
try{
client.getOutputStream().writeUTF(message+" From Server");
}
catch(Exception e){
e.printStackTrace();
}
}
}
public String integrateClientInfo(){
Object temp = comboBox.getSelectedItem();
String name = (String)temp;
// System.out.println(name);
String a = "用户名为: "+name+"\n";
// System.out.println((People)clientHash.get(name));
String b = "IP是: "+((People)clientHash.get(name)).getClientSocket().getInetAddress().getHostAddress()+"\n";
// System.out.println(b);
String c = "主机名为: "+((People)clientHash.get(name)).getClientSocket().getInetAddress().getHostName()+"\n";
String d = "端口号为: "+((People)clientHash.get(name)).getClientSocket().getPort();
return a+b+c+d;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -