1. 面向对象编程
- 类与对象:代码中定义了
Chat
类,它是整个程序的核心,封装了与聊天窗口相关的属性和方法。在 main
方法中创建了 Chat
类的对象,并调用其方法来完成相应的功能。
- 继承与多态:
ButtonClickListener
类实现了 ActionListener
接口,这是一种多态的体现。通过实现接口,ButtonClickListener
类可以作为 ActionListener
类型来使用,使得按钮能够注册该监听器以处理点击事件。
- 内部类:
ButtonClickListener
是 Chat
类的内部类,它可以直接访问外部类 Chat
的成员变量和方法,这有助于代码的组织和封装,同时也能方便地处理与外部类相关的事件。
2. 图形用户界面(GUI)编程
- Swing 库:代码使用了 Java 的 Swing 库来创建图形用户界面。
JFrame
用于创建窗口,JButton
用于创建按钮,JTextArea
用于创建文本区域。这些组件都是 Swing 库提供的,通过组合它们可以构建出丰富的 GUI 界面。
- 布局管理器:使用
FlowLayout
作为窗口的布局管理器,它会按照组件添加的顺序从左到右、从上到下排列组件,这是一种简单且常用的布局方式。
- 事件处理:通过实现
ActionListener
接口,为按钮添加点击事件监听器。当用户点击按钮时,会触发 actionPerformed
方法,在该方法中可以编写相应的处理逻辑,如发送消息、保存文件和关闭窗口等。
3. 文件操作
- 文件类:使用
File
类来表示文件和目录,通过 File
对象可以对文件进行创建、删除、重命名等操作。在代码中,使用 File
类创建了一个表示 job\\out.txt
文件的对象。
- 文件输出流:
FileOutputStream
用于将数据写入文件。代码中使用 FileOutputStream
将聊天记录文本内容写入到指定的文件中,涉及到字节流的操作,需要将字符串转换为字节数组后再写入文件。
4. 异常处理
try-catch
块:在代码中使用了 try-catch
块来捕获和处理可能出现的异常。例如,在 initChatWindow
方法中捕获 IOException
,以处理文件操作可能出现的输入输出异常;在 saveTextToFile
方法中也捕获了 IOException
,确保在文件写入过程中出现异常时程序不会崩溃,并打印异常信息方便调试。
5. 基本数据类型和字符串处理
- 字符串操作:在处理聊天记录和输入内容时,使用了字符串类型。例如,通过
getText
方法从 JTextArea
中获取文本内容,使用 append
方法将文本追加到 JTextArea
中,还将字符串转换为字节数组以便写入文件。
6. 静态方法和入口点
main
方法:main
方法是 Java 程序的入口点,程序从这里开始执行。它是一个静态方法,不需要创建对象就可以直接调用,通常用于初始化程序和调用其他方法。
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class Chat {
public static void main(String[] args) {
try {
new Chat().initChatWindow();
} catch (IOException e) {
e.printStackTrace();
}
}
JFrame chatWindow = new JFrame("QQ");
JButton sendButton = new JButton("发送");
JButton closeButton = new JButton("关闭");
JTextArea chatHistoryTextArea = new JTextArea(10, 35);
JTextArea inputTextArea = new JTextArea(5, 35);
public void initChatWindow() throws IOException {
chatWindow.setSize(400, 480);
chatWindow.setLayout(new FlowLayout());
chatHistoryTextArea.setBackground(Color.GREEN);
inputTextArea.setBackground(Color.lightGray);
sendButton.addActionListener(new ButtonClickListener());
closeButton.addActionListener(new ButtonClickListener());
chatWindow.add(chatHistoryTextArea);
chatWindow.add(inputTextArea);
chatWindow.add(sendButton);
chatWindow.add(closeButton);
chatWindow.setResizable(false);
chatWindow.setVisible(true);
}
public void saveTextToFile(String textToSave) {
try {
File outputFile = new File("job\\out.txt");
FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
fileOutputStream.write(textToSave.getBytes());
fileOutputStream.flush();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
class ButtonClickListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("hdj");
if (e.getSource() == sendButton) {
chatHistoryTextArea.append(inputTextArea.getText());
inputTextArea.setText("");
}
if (e.getSource() == closeButton) {
saveTextToFile(chatHistoryTextArea.getText());
System.exit(0);
}
}
}
}