📄 frame1.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class Frame1 extends JFrame implements ActionListener
{
String str;
JButton b1,b2,b3,dlgb1,dlgb2;
JTextField tf,tf2;
JPanel pane1,pane2,pane3;
JLabel l1,l2,l3,dlgl1;
List list1,list2,list3;
JRadioButton rb;
Dialog dlg;
Container con=this.getContentPane();
Mm m2;
ImageIcon icon=new ImageIcon("image\\login.gif");
DatagramSocket ds2,ds3;
public Frame1()
{
super("Title");
setSize(800,600);
l1=new JLabel("");
l2=new JLabel("你对:");
dlgl1=new JLabel(" 真的要退出吗? ");
dlgl1.setFont(new Font("宋体", Font.BOLD, 15));
rb=new JRadioButton("悄悄说:");
rb.setBackground(new Color(237,240,241));
b1=new JButton("发送");
b1.setBackground(new Color(220,218,248));
b2=new JButton("退出");
b2.setBackground(new Color(220,218,248));
b3=new JButton("清屏");
b3.setBackground(new Color(220,218,248));
dlgb1=new JButton("确定");
dlgb1.setBackground(new Color(254,227,224));
dlgb2=new JButton("取消");
dlgb2.setBackground(new Color(254,227,224));
tf=new JTextField(30);
tf2=new JTextField("所有人",6);
list1= new List();
list2= new List();
list3= new List();
dlg=new Dialog(this,"退出",true);
pane1=new JPanel();
pane2=new JPanel();
pane3=new JPanel();
pane1.setSize(5,5);
pane2.setSize(5,5);
pane3.setSize(5,5);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
dlgb1.addActionListener(this);
dlgb2.addActionListener(this);
list3.addActionListener(this);
tf.addActionListener(this);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
dlg.show();
}
});
//pane1.add(l1);
dlg.setSize(200,100);
dlg.add(dlgl1);
dlg.add(dlgb1);
dlg.add(dlgb2);
dlg.setLayout(new FlowLayout());
dlg.setBackground(new Color(237,240,241));
pane1.add(l2);
pane1.add(tf2);
pane1.add(rb);
pane1.add(tf);
pane1.add(b1);
pane1.add(b2);
pane1.add(b3);
pane1.setLayout(new FlowLayout());
pane1.setBackground(new Color(237,240,241));
list1.setBackground(new Color(179,225,253));
list1.setFont(new Font("黑体", Font.BOLD, 15));
JScrollPane scr1 = new JScrollPane(list1);
scr1.setPreferredSize(new Dimension(645,300));
list1.setForeground(new Color(123,46,4));
pane2.add(scr1);
list2.setForeground(new Color(123,46,4));
list2.setBackground(new Color(179,225,253));
list2.setFont(new Font("黑体", Font.BOLD, 15));
JScrollPane scr2 = new JScrollPane(list2);
scr2.setPreferredSize(new Dimension(645,100));
pane2.add(scr2);
pane2.setLayout(new GridLayout(2,1));
list3.createImage(10,10);
list3.setBackground(new Color(179,225,253));
list3.setFont(new Font("黑体", Font.BOLD, 15));
list3.setForeground(new Color(97,24,103));
list3.add("所有人");//////////////////////////
JScrollPane scr3 = new JScrollPane(list3);
scr3.setPreferredSize(new Dimension(1,1));
pane3.add(scr3);
pane3.setLayout(new GridLayout(1,1));
con.add(pane1,BorderLayout.SOUTH);
con.add(pane2,BorderLayout.WEST);
con.add(pane3,BorderLayout.CENTER);
m2=new Mm();
setResizable(false);
setLocation(100,50);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
show();
///////////////////////////////////////////////////////接收信息
try
{
ds2=new DatagramSocket(8001);
ds3=new DatagramSocket(9501);
}
catch(Exception e)
{
System.out.println(e);
}
new Thread(new Runnable()
{
public void run()
{
byte buf[]=new byte[1024];
DatagramPacket dp=new DatagramPacket(buf,1024);
while(true)
{
synchronized("a")
{
try
{
ds2.receive(dp);
list1.add(new String(buf,0,dp.getLength()),0);
}
catch(Exception e2)
{
System.out.println(e2);
}
}
}
}
}).start();
////////////////////////////////////////////////////////////接收完毕
////////////////////////////////////////////////////////////更新列表
new Thread(new Runnable()
{
public void run()
{
byte buf[]=new byte[1024];
DatagramPacket dp=new DatagramPacket(buf,1024);
while(true)
{
synchronized("b")
{
try
{
ds3.receive(dp);
list3.clear();
str=new String(buf,0,dp.getLength());
for(int i=0;i<30;i++)
{
if (str.length()==1)
{
break;
}
else
{
int k=str.indexOf(' ');
list3.add(str.substring(0,k));
str=str.substring(k+1);
}
}
}
catch(Exception e2)
{
e2.printStackTrace();
}
}
}
}
}).start();
////////////////////////////////////////////////////////////更新完毕
}
public static void main(String[] args)
{
new Frame1();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
String stringsend=tf2.getText()+" "+tf.getText();
m2.strsend(stringsend);
list2.add("你对"+tf2.getText()+"说:"+tf.getText(),0);
tf.setText("");
}
if(e.getSource()==b2)
{
dlg.setLocation(this.getLocationOnScreen());
dlg.show();
}
if(e.getSource()==b3)
{
list1.clear();
list2.clear();
}
if(e.getSource()==dlgb1)
{
m2.updata("EXIT");
m2.exit();
}
if(e.getSource()==dlgb2)
{
dlg.dispose();
}
if(e.getSource()==list3)
{
tf2.setText(list3.getSelectedItem());
}
if(e.getSource()==tf)
{
String stringsend=tf2.getText()+" "+tf.getText();
m2.strsend(stringsend);
list2.add("你对"+tf2.getText()+"说:"+tf.getText(),0);
tf.setText("");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -