📄 talkprogram.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
/*--------------------------------------定义常用变量接口---------------------------------------*/
interface MyValue {
int WEIGHT1 = 20;
int WEIGHT2 = 10;
int WEIGHT3 = 30;
int HEIGHT1 = 30;
int LOCATION = 200;
int ININWEIGHT = 200;
String s1 = "发送";
String s2 = "消息:";
String s3 = "Wayne";
String s4 = "文件";
String s5 = "保存聊天信息";
String s6 = "读取聊天信息";
String s7 = "对方IP";
String s8 = "帮助";
String s9 = "帮助文档";
String s10 = "编辑";
String s11 = "删除记录";
String s12 = "查找记录";
String s13 = "传文件";
String s14 = "关闭";
String s15 = "Connect";
String user = "对方";
String s16 = "Port";
String s17 = "共可输入20个字符";
String conncetIp = "127.0.0.1";
int INITHEIGHT = 200;
}
/* ========================================核心程序================================================ */
public class TalkProgram extends Frame implements MyValue {
private Socket client = null;
private Button b1 = null;
private Button b2 = null;
private Button b3 = null;
private Button b4 = null;
private TextField t1 = null;
private TextField t2 = null;
private TextField t3 = null;
private TextArea ta1 = null;
private Panel p1 = null;
private Panel p2 = null;
private Label l1 = null;
private Label l2 = null;
private Label l3 = null;
private Label l4 = null;
public DatagramSocket socket = null;// 声明datagramsocket套接字,随机绑定一个有效的端口;
int SERVERPORT = 8099;
public static String serverIP = "127.0.0.1";// 默认为本机
public static String localIp = null;
public static int PORT;// 对话框输入对方的服务器的端口号
public String port1;// 自己方服务器的端口
public TalkProgram() {// 构造函数
super(s3);
/*------------------------ 初始化所有需要的部件------------------------------------*/
t1 = new TextField("", WEIGHT1);
t2 = new TextField("", WEIGHT3);
t1.addKeyListener(new stringlLeft());
t3 = new TextField("", WEIGHT2 / 2);
ta1 = new TextArea(WEIGHT2, HEIGHT1);
b1 = new Button(s1);
b2 = new Button(s13);
b2.addActionListener(new send());
b3 = new Button(s14);
b4 = new Button(s15);
b4.addActionListener(new connect());
p1 = new Panel();
p2 = new Panel();
l1 = new Label(s2);
l2 = new Label(s7);
l3 = new Label(s16);
l4 = new Label(s17);
p1.setLayout(new FlowLayout());
p1.add(l1);
p1.add(t1);
p1.add(l4);
p1.add(b1);
b1.addActionListener(new SendMessage());
p1.add(b2);
p1.add(b3);
b3.addActionListener(new closeConnect());
p2.setLayout(new FlowLayout());
p2.add(l3);
p2.add(t3);
p2.add(l2);
p2.add(t2);
p2.add(b4);
/*------------------------------下面是关于菜单部分-------------------------------------*/
MenuBar mb = new MenuBar();
Menu m1 = new Menu(s4);
Menu m2 = new Menu(s10);
Menu m3 = new Menu(s8);
MenuItem mi1 = new MenuItem(s5, new MenuShortcut(KeyEvent.VK_S));
MenuItem mi2 = new MenuItem(s6, new MenuShortcut(KeyEvent.VK_O));
MenuItem mi3 = new MenuItem(s11, new MenuShortcut(KeyEvent.VK_X));
MenuItem mi4 = new MenuItem(s9, new MenuShortcut(KeyEvent.VK_H));
MenuItem mi5 = new MenuItem(s12, new MenuShortcut(KeyEvent.VK_F));
m1.add(mi1);
m1.addSeparator();
m1.add(mi2);
m2.add(mi3);
m2.addSeparator();
m2.add(mi5);
m3.add(mi4);
mi1.addActionListener(new SaveMenuItem());
mi2.addActionListener(new ReadMenuItem());
mi3.addActionListener(new deleteContent());
mi4.addActionListener(new showHelp());
mb.add(m1);
mb.add(m2);
mb.add(m3);
/*port1 = JOptionPane.showInputDialog(null, "服务器端口", "端口号:" + "",
JOptionPane.QUESTION_MESSAGE);
System.out.println("server port is " + port1);
SERVERPORT = getPort(port1);
System.out.println("server port is " + SERVERPORT);*/
this.setMenuBar(mb);
/* -------------------------组合各个部件和相互的关系---------------------------- */
this.setLayout(new BorderLayout(5, 5));
this.add(p2, BorderLayout.NORTH);
this.add(ta1, BorderLayout.CENTER);
this.add(p1, BorderLayout.SOUTH);
this.pack();// 简单整理一下部件布局
this.setLocation(LOCATION, LOCATION);
this.setVisible(true);
/*----------------------------关闭窗口------------------------------------------------*/
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
/*
* -------------------------------------------启动server监听端口是否有人连接-----------
* -----------------------------------------
*/
InetAddress addr = null;
try {
addr = InetAddress.getLocalHost();
} catch (UnknownHostException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
localIp=addr.getHostAddress().toString();
receiveMessage waitMessage = new receiveMessage();// 启动server线程监听是否有人连接
Thread td1 = new Thread(waitMessage);
td1.start();
receiveFile waitFile = new receiveFile();
Thread td2 = new Thread(waitFile);
td2.start();
}// 构造函数结束
/* public String getIP() {
String ip = t2.getText();
return ip;
}
public int getPort(String s) {
if (s != "") {
int s1 = Integer.parseInt(s);
return s1;
} else
return SERVERPORT;
}
*/
/*-----------------------------delete chat content-----------------------------------------------------*/
class deleteContent implements ActionListener {
public void actionPerformed(ActionEvent e) {
ta1.setText(null);
}
}
/*--------------------------------------显示帮助文档--------------------------------------------------*/
class showHelp implements ActionListener {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,
"该作品制作人:王宁\n学号:05301052\n\n感谢大家对本产品的支持!");
}
}
/* -----------------save chat content----------------------------- */
class SaveMenuItem implements ActionListener {// save chat message
public void actionPerformed(ActionEvent e)// 保存信息
{
FileOutputStream out;
PrintStream p; // 声明一个Print Stream流
try {
out = new FileOutputStream("chatlog.txt", true);
p = new PrintStream(out);
p.println(ta1.getText());// 方法println(),它将一个字符串发送给输出流。
p.close();
} catch (Exception e1) {
System.err.println("Error writing to file");
}
}
}
/* -------------------read chat content-------------------------- */
class ReadMenuItem implements ActionListener {// read chat message
public void actionPerformed(ActionEvent e) {// 读取信息
// 定义一个byte数组用于接收从文件中读出的字节,注意它的长度为1024
byte[] buff = new byte[1024];
int n;
FileInputStream fis = null;
// 生成对象infile 准备读取文件
try {
File f = new File("chatlog.txt");
fis = new FileInputStream(f);
// 从文件读取数据
while ((n = fis.read(buff, 0, 1024)) != -1) {
String s = new String(buff, 0, n);
ta1.append(s);
fis.close();
}
} catch (FileNotFoundException e1) {
System.out.println("没有找到文件");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -