-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.java
More file actions
118 lines (104 loc) · 3.71 KB
/
chat.java
File metadata and controls
118 lines (104 loc) · 3.71 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import java.util.Scanner;
import javax.swing.*;
import java.io.*;
public class chat {
String username;
String password;
String messages;
String filePath = "Y:\\3BHIT\\test\\chat.txt";
JTextArea chat = new JTextArea();
private void readMessages() {
try {
File fp = new File(this.filePath);
if (fp.createNewFile())
System.out.println("File created: " + fp.getName());
Scanner reader = new Scanner(fp);
messages = "";
while (reader.hasNextLine()) {
String data = reader.nextLine();
messages += data + "\n";
}
chat.setText(messages);
chat.setCaretPosition(chat.getDocument().getLength());
reader.close();
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
private void writeMessages(String message) {
try {
FileWriter myWriter = new FileWriter(this.filePath, true);
myWriter.write(message);
myWriter.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
private void inputGUI() {
JFrame frame = new JFrame("Moritz Tools Java");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField userText = new JTextField(20);
JLabel userLabel = new JLabel("Username");
JPasswordField keyText = new JPasswordField(20);
JLabel keyLabel = new JLabel("Password");
keyText.setEchoChar('*');
keyText.addActionListener(e -> {
username = userText.getText();
password = new String(keyText.getPassword());
frame.dispose();
createAndShowGUI();
});
JButton button = new JButton("Starten");
button.addActionListener(e -> {
username = userText.getText();
password = new String(keyText.getPassword());
frame.dispose();
createAndShowGUI();
});
JPanel panel = new JPanel();
panel.add(userLabel);
panel.add(userText);
panel.add(keyLabel);
panel.add(keyText);
panel.add(button);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
private void createAndShowGUI() {
JFrame frame = new JFrame("Moritz Tools Java");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
chat.setLineWrap(true);
chat.setWrapStyleWord(true);
chat.setEditable(false);
frame.getContentPane().add(new JScrollPane(chat));
frame.setSize(800, 600);
frame.setVisible(true);
JTextField input = new JTextField();
input.addActionListener(e -> {
String text = input.getText();
chat.append(String.format("%s: %s%n", username, text));
input.setText("");
writeMessages(String.format("%s: %s%n", username, text));
chat.setCaretPosition(chat.getDocument().getLength());
readMessages();
});
frame.getContentPane().add(input, "South");
SwingUtilities.invokeLater(() -> input.requestFocusInWindow());
frame.setVisible(true);
Timer timer = new Timer(1000, e -> readMessages());
timer.start();
readMessages();
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
chat app = new chat();
app.inputGUI();
}
});
}
}