ChatClient.java 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package eu.tankernn.chat.client;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.util.Properties;
  8. import java.util.Scanner;
  9. import javax.swing.JComponent;
  10. import javax.swing.JLabel;
  11. import javax.swing.JOptionPane;
  12. import javax.swing.JTextField;
  13. public class ChatClient {
  14. static Properties prop = new Properties();
  15. static File confFile = new File("client.properties");
  16. public static void main(String[] arg) {
  17. try {
  18. prop.load(new FileInputStream(confFile));
  19. } catch (FileNotFoundException e) {
  20. prop.setProperty("host", "tankernn.eu");
  21. prop.setProperty("port", "25566");
  22. prop.setProperty("username", "Username");
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. }
  26. JTextField hostBox = new JTextField(prop.getProperty("host"));
  27. JTextField portBox = new JTextField(prop.getProperty("port"));
  28. JTextField userBox = new JTextField(prop.getProperty("username"));
  29. final JComponent[] inputs = new JComponent[] {
  30. new JLabel("Host:"), hostBox,
  31. new JLabel("Port:"), portBox,
  32. new JLabel("Username:"), userBox
  33. };
  34. String host, username, portString;
  35. JOptionPane.showMessageDialog(null, inputs, "Chat settings", JOptionPane.PLAIN_MESSAGE);
  36. host = hostBox.getText();
  37. prop.setProperty("host", host);
  38. username = userBox.getText();
  39. prop.setProperty("username", username);
  40. portString = portBox.getText();
  41. Scanner sc = new Scanner(portString);
  42. int port = sc.nextInt();
  43. sc.close();
  44. prop.setProperty("port", portString);
  45. writeConfFile();
  46. new ChatWindow(host, port, username);
  47. }
  48. static void writeConfFile() {
  49. try {
  50. if (!confFile.exists())
  51. confFile.createNewFile();
  52. prop.store(new FileOutputStream(confFile), "Configuration for chat client");
  53. } catch (IOException e) {
  54. e.printStackTrace();
  55. }
  56. }
  57. }