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 ) ; } } }
}