📄 qq.txt
字号:
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.awt.Font;
import java.awt.Color;
public class QQ extends Frame implements ActionListener,ItemListener{
Label l1=new Label("请输入您要发送的信息(限英文):");
Label l2=new Label("以下是你收到的消息");
Label l3=new Label("把以上消息发给以下IP地址");
Label l4=new Label("聊 天 记 录");
Label label=new Label();
Choice choice=new Choice();
TextArea input=new TextArea("",7,14,TextArea.SCROLLBARS_BOTH);
TextArea output=new TextArea("",8,14,TextArea.SCROLLBARS_BOTH);
TextField IPAdd=new TextField("请输入接收方的IP");
Button send=new Button("发送消息");
Button quit=new Button("关闭");
Button Clear=new Button("清空消息记录");
Choice c=new Choice();
Choice c1=new Choice();
Choice c2=new Choice();
Choice c3=new Choice();
Choice c4=new Choice();
GregorianCalendar time=new GregorianCalendar();
Font f1=new Font("宋体",Font.BOLD,12);
Font f2=new Font("楷体",Font.BOLD,12);
Font f3=new Font("隶书",Font.BOLD,12);
Font f4=new Font("黑体",Font.BOLD,12);
Font f5=new Font("楷体",Font.PLAIN,12);
Font f6=new Font("楷体",Font.BOLD,12);
Font f7=new Font("楷体",Font.ITALIC,12);
QQ(){
super("仿QQ");
setLayout(null);
setLocation(250,250);
setSize(600,450);
setResizable(false);
setVisible(true);
setBackground(new Color(130,180,230));
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);}});
l1.setBounds(10,30,216,16);
l2.setBounds(264,30,216,16);
l3.setBounds(10,172,160,16);
l4.setBounds(326,188,110,22);
input.setBounds(8,44,248,120);
output.setBounds(264,44,248,136);
IPAdd.setBounds(8,190,120,19);
send.setBounds(136,188,70,22);
quit.setBounds(216,188,70,22);
Clear.setBounds(430,188,90,22);
add(l1);
add(l2);
add(l3);
add(l4);
add(input);
add(output);
add(IPAdd);
add(send);
add(quit);
add(Clear);
send.addActionListener(this);
quit.addActionListener(this);
Clear.addActionListener(this);
c.addItem("12");
c.addItem("24");
c.addItem("36");
choice.addItem("黑");
choice.addItem("红");
choice.addItem("蓝");
choice.addItem("黄");
choice.addItem("绿");
c1.addItem("宋体");
c1.addItem("楷体");
c1.addItem("隶书");
c1.addItem("黑体");
c2.addItem("背景1:贵族紫");
c2.addItem("背景2:苹果绿");
c2.addItem("背景3:阳光红");
c2.addItem("原背景:天空蓝");
c3.addItem("普通");
c3.addItem("粗体");
c3.addItem("斜体");
c4.addItem("峰");
c4.addItem("勇");
c4.addItem("仁");
choice.setBounds(8,250,90,22);
add(choice);
choice.addItemListener(this);
choice.select(4);
c.setBounds(8,280,90,22);
add(c);
c.addItemListener(this);
c1.setBounds(100,250,90,22);
add(c1);
c1.addItemListener(this);
c2.setBounds(100,280,90,22);
add(c2);
c2.addItemListener(this);
c3.setBounds(192,250,90,22);
add(c3);
c3.addItemListener(this);
c4.setBounds(8,220,90,22);
add(c4);
c4.addItemListener(this);
waitForData();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==send)
sendData();
else if (e.getSource()==Clear)
output.setText("");
else if(e.getSource()==quit){
dispose();
System.exit(0);}
}
public static void main(String args[]){
new QQ();
}
public void itemStateChanged(ItemEvent e)
{ if(e.getSource()==choice){
if(choice.getSelectedItem()=="黑")
input.setForeground(Color.black);
if(choice.getSelectedItem()=="红")
input.setForeground(Color.red);
if(choice.getSelectedItem()=="蓝")
input.setForeground(Color.blue);
if(choice.getSelectedItem()=="黄")
input.setForeground(Color.yellow);
if(choice.getSelectedItem()=="绿")
input.setForeground(Color.green);
}
else if(e.getSource()==c){
if(c.getSelectedItem()=="12")
input.setFont(new Font("华文行楷",Font.BOLD,12));
if(c.getSelectedItem()=="24")
input.setFont(new Font("华文行楷",Font.BOLD,24));
if(c.getSelectedItem()=="36")
input.setFont(new Font("华文行楷",Font.BOLD,36));
}
else if(e.getSource()==c1){
if(c1.getSelectedItem()=="宋体")
input.setFont(f1);
if(c1.getSelectedItem()=="楷体")
input.setFont(f2);
if(c1.getSelectedItem()=="隶书")
input.setFont(f3);
if(c1.getSelectedItem()=="黑体")
input.setFont(f4);
}
else if(e.getSource()==c2){
if(c2.getSelectedItem()=="背景1:贵族紫")
{setBackground(Color.magenta);
l1.setBackground(Color.magenta);
l2.setBackground(Color.magenta);
l3.setBackground(Color.magenta);
l4.setBackground(Color.magenta);}
if(c2.getSelectedItem()=="背景2:苹果绿")
{ setBackground(Color.green);
l1.setBackground(Color.green);
l2.setBackground(Color.green);
l3.setBackground(Color.green);
l4.setBackground(Color.green);}
if(c2.getSelectedItem()=="背景3:阳光红")
{setBackground(Color.red);
l1.setBackground(Color.red);
l2.setBackground(Color.red);
l3.setBackground(Color.red);
l4.setBackground(Color.red);}
if(c2.getSelectedItem()=="原背景:天空蓝")
{setBackground(new Color(130,180,230));
l1.setBackground(new Color(130,180,230));
l2.setBackground(new Color(130,180,230));
l3.setBackground(new Color(130,180,230));
l4.setBackground(new Color(130,180,230));}
}
else if(e.getSource()==c3){
if(c3.getSelectedItem()=="普通")
input.setFont(f5);
if(c3.getSelectedItem()=="粗体")
input.setFont(f6);
if(c3.getSelectedItem()=="斜体")
input.setFont(f7);
}
else if(e.getSource()==c4){
if(c3.getSelectedItem()=="峰")
IPAdd.setText("192.168.105.*");
}
}
void sendData(){
try{
String msg=input.getText();
if(msg.equals(""))
return;
input.setText("");
String ad=IPAdd.getText();
InetAddress tea=InetAddress.getLocalHost();
String asd=tea.getHostAddress();//发送方的IP地址
output.append("["+asd+"]:("+time.get(GregorianCalendar.YEAR)
+"-"+time.get(GregorianCalendar.MONTH)+"-"
+time.get(GregorianCalendar.DATE)+" "
+time.get(GregorianCalendar.HOUR)+" "
+time.get(GregorianCalendar.MINUTE)+" "
+time.get(GregorianCalendar.SECOND)+")"+"\n"+ msg
+"\n");
msg="From["+asd+"]:("+time.get(GregorianCalendar.YEAR)
+"-"+time.get(GregorianCalendar.MONTH)+"-"
+time.get(GregorianCalendar.DATE)+" "
+time.get(GregorianCalendar.HOUR)+" "
+time.get(GregorianCalendar.MINUTE)+" "
+time.get(GregorianCalendar.SECOND)+")"+"\n"+ msg;
InetAddress address=InetAddress.getByName(ad);
int len=msg.length();
byte[] message=new byte[len];
msg.getBytes(0,len,message,0);
DatagramPacket packet=new DatagramPacket(message,len,address,9999);
DatagramSocket socket=new DatagramSocket();
socket.send(packet);
}catch(Exception e){
}
}
void waitForData(){
try{
byte[] buffer=new byte[1024];
DatagramPacket packet=new DatagramPacket(buffer,buffer.length);
DatagramSocket socket=new DatagramSocket(9999);
while(true){
socket.receive(packet);
String s=new String(buffer,0,0,packet.getLength());
output.append(s+"\n");
packet=new DatagramPacket(buffer,buffer.length);
}
}catch(Exception e){
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -