forked from biblelamp/JavaExercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHW4Lesson.java
More file actions
71 lines (65 loc) · 2.15 KB
/
HW4Lesson.java
File metadata and controls
71 lines (65 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* Java. Level 2. Lesson 4. Example of homework
*
* @author Sergey Iryupin
* @version 0.1.2 dated Jun 18, 2018
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
class HW4Lesson extends JFrame implements ActionListener {
final String TITLE_OF_PROGRAM = "Client for net.chat";
final int WINDOW_WIDTH = 350;
final int WINDOW_HEIGHT = 450;
final String BTN_ENTER = "Enter";
final String LOG_FILE_NAME = "logchat.txt";
JTextArea dialogue; // area for dialog
JTextField message; // field for entering messages
public static void main(String[] args) {
new HW4Lesson();
}
/**
* Constructor:
* Creating a window and all the necessary elements on it
*/
HW4Lesson() {
setTitle(TITLE_OF_PROGRAM);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setLocationRelativeTo(null); // to the center
// area for dialog
dialogue = new JTextArea();
dialogue.setEditable(false);
JScrollPane scrollBar = new JScrollPane(dialogue);
// panel for checkbox, message field and button
JPanel bp = new JPanel();
bp.setLayout(new BoxLayout(bp, BoxLayout.X_AXIS));
message = new JTextField();
message.addActionListener(this);
JButton enter = new JButton(BTN_ENTER);
enter.addActionListener(this);
// adding all elements to the window
bp.add(message);
bp.add(enter);
add(BorderLayout.CENTER, scrollBar);
add(BorderLayout.SOUTH, bp);
setVisible(true);
}
/**
* Listener of events from message field and enter button
*/
@Override
public void actionPerformed(ActionEvent event) {
if (message.getText().trim().length() > 0) {
dialogue.append(message.getText() + "\n");
try (PrintWriter pw =
new PrintWriter(new FileWriter(LOG_FILE_NAME, true))) {
pw.println(message.getText());
pw.flush();
} catch (IOException ex) { }
}
message.setText("");
message.requestFocusInWindow();
}
}