Advertisement
Guest User

Untitled

a guest
Aug 6th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.40 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.io.*;
  5. import javax.swing.*;
  6.  
  7. public class Login
  8. {
  9. private static JFrame frame;
  10. private static JPanel northPanel;
  11. private static JPanel centerPanel;
  12. private static JPanel southPanel;
  13. private static JLabel headingLabel;
  14. private static JLabel usernameLabel;
  15. private static JLabel passwordLabel;
  16. private static JTextField usernameField;
  17. private static JPasswordField passwordField;
  18. private static JButton loginBtn;
  19.  
  20. public void start()
  21. {
  22. try
  23. {
  24. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  25. }
  26. catch (Exception e)
  27. {
  28.  
  29. }
  30.  
  31. new Login().buildLogin();
  32. }
  33.  
  34. private void buildLogin()
  35. {
  36. // Build Objects
  37. frame = new JFrame();
  38. northPanel = new JPanel();
  39. centerPanel = new JPanel();
  40. southPanel = new JPanel();
  41. headingLabel = new JLabel();
  42. usernameLabel = new JLabel();
  43. passwordLabel = new JLabel();
  44. usernameField = new JTextField();
  45. passwordField = new JPasswordField();
  46. loginBtn = new JButton();
  47.  
  48. // Labels
  49. headingLabel.setText("Login");
  50. headingLabel.setForeground(Color.white);
  51. headingLabel.setVisible(true);
  52.  
  53. usernameLabel.setText(" Username: ");
  54. usernameLabel.setForeground(Color.blue);
  55. usernameLabel.setVisible(true);
  56.  
  57. passwordLabel.setText(" Password: ");
  58. passwordLabel.setForeground(Color.blue);
  59. passwordLabel.setVisible(true);
  60.  
  61. // Buttons
  62. loginBtn.setText("Login");
  63. loginBtn.setVisible(true);
  64. loginBtn.addActionListener(new Validate1());
  65.  
  66. // Panels
  67. northPanel.add(headingLabel);
  68. northPanel.setBackground(Color.blue);
  69. centerPanel.add(usernameLabel);
  70. centerPanel.add(usernameField);
  71. centerPanel.add(passwordLabel);
  72. centerPanel.add(passwordField);
  73. centerPanel.setLayout(new GridLayout(2, 2));
  74. southPanel.add(loginBtn);
  75.  
  76. // JFrame
  77. frame.getContentPane().add(BorderLayout.NORTH, northPanel);
  78. frame.getContentPane().add(BorderLayout.CENTER, centerPanel);
  79. frame.getContentPane().add(BorderLayout.SOUTH, southPanel);
  80. frame.setSize(300, 150);
  81. frame.setResizable(false);
  82. frame.setVisible(true);
  83. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  84. frame.setLocationRelativeTo(null);
  85. }
  86.  
  87. // Validate1 Class
  88. static class Validate1 implements ActionListener
  89. {
  90. @SuppressWarnings("deprecation")
  91. public void actionPerformed(ActionEvent e)
  92. {
  93. String username = null;
  94. String password = null;
  95.  
  96. try
  97. {
  98. username = usernameField.getText().toUpperCase().trim();
  99. password = passwordField.getText().toUpperCase().trim();
  100.  
  101. if (username.equals("") && password.equals(""))
  102. {
  103. usernameLabel.setForeground(Color.red);
  104. passwordLabel.setForeground(Color.red);
  105. }
  106. else
  107. {
  108. if (username.equals(""))
  109. {
  110. usernameLabel.setForeground(Color.red);
  111. passwordLabel.setForeground(Color.blue);
  112. }
  113. else
  114. {
  115. if (password.equals(""))
  116. {
  117. usernameLabel.setForeground(Color.blue);
  118. passwordLabel.setForeground(Color.red);
  119. }
  120. else
  121. {
  122. usernameLabel.setForeground(Color.red);
  123. passwordLabel.setForeground(Color.red);
  124. new Login().validate2(username, password);
  125. }
  126. }
  127. }
  128. }
  129. catch (Exception validateError)
  130. {
  131. JOptionPane.showMessageDialog(null, "There was an error Class::Login Class Validate1", "Error", JOptionPane.ERROR_MESSAGE);
  132. }
  133. }
  134. }
  135.  
  136. //Validate2 Method
  137. private void validate2(String getUsername, String getPassword)
  138. {
  139. String username = null;
  140. String password = null;
  141.  
  142. try
  143. {
  144. username = getUsername.toUpperCase();
  145. password = getPassword.toUpperCase();
  146.  
  147. if (username.contains(" ") && password.contains(" "))
  148. {
  149. JOptionPane.showMessageDialog(null, "The username and password is invalid", "Error", JOptionPane.ERROR_MESSAGE);
  150. }
  151. else
  152. {
  153. if (username.contains(" "))
  154. {
  155. JOptionPane.showMessageDialog(null, "The username is invalid", "Error", JOptionPane.ERROR_MESSAGE);
  156. }
  157. else
  158. {
  159. if (password.contains(" "))
  160. {
  161. JOptionPane.showMessageDialog(null, "The password is invalid", "Error", JOptionPane.ERROR_MESSAGE);
  162. }
  163. else
  164. {
  165. usernameLabel.setForeground(Color.red);
  166. passwordLabel.setForeground(Color.red);
  167. System.out.println("All good!!");
  168. }
  169. }
  170. }
  171. }
  172. catch(Exception e)
  173. {
  174. JOptionPane.showMessageDialog(null, "Error in Class::Login Method:valsidate2","Error", JOptionPane.ERROR_MESSAGE);
  175.  
  176. }
  177. }
  178. }
  179.  
  180. public class Main
  181. {
  182.  
  183. public static void main(String[] args)
  184. {
  185. new Login().start();
  186.  
  187. }
  188.  
  189. }
  190.  
  191. import java.io.*;
  192.  
  193. public class FileData
  194. {
  195.  
  196. public static void main(String[] args) throws IOException
  197. {
  198. String file_name = "PATH TO THE TEXT FILE WOULD BE HERE";
  199.  
  200. try
  201. {
  202. ReadFile file = new ReadFile(file_name);
  203. String[] aryLines = file.OpenFile();
  204.  
  205. for(int i = 0; i < aryLines.length; i++)
  206. {
  207. System.out.println(aryLines[i]);
  208. }
  209. }
  210. catch (IOException e)
  211. {
  212. //System.out.println(e.getMessage());
  213. System.out.println("Sorry, dude - no can do!!!n" + e.getMessage());
  214. }
  215.  
  216. try
  217. {
  218. WriteFile data = new WriteFile(file_name , true );
  219. data.writeToFile("This is another line of text");
  220. }
  221. catch(IOException e)
  222. {
  223. System.out.println("Sorry, dude - no can do!!!n" + e.getMessage());
  224. }
  225.  
  226. System.out.println("Text File Written To");
  227. }
  228.  
  229. }
  230.  
  231. import java.io.*;
  232.  
  233. public class ReadFile
  234. {
  235. private String path;
  236.  
  237. public ReadFile(String file_path)
  238. {
  239. path = file_path;
  240. }
  241.  
  242. public String[] OpenFile() throws IOException
  243. {
  244. FileReader inFile = new FileReader(path);
  245. BufferedReader textReader = new BufferedReader(inFile);
  246.  
  247. int numOfLines = readLines();
  248. String[] textData = new String[numOfLines];
  249.  
  250. for(int i = 0; i < numOfLines; i++)
  251. {
  252. textData[i] = textReader.readLine();
  253. }
  254.  
  255. textReader.close();
  256. return textData;
  257. }
  258.  
  259. int readLines() throws IOException
  260. {
  261. FileReader file_to_read = new FileReader(path);
  262. BufferedReader bf = new BufferedReader(file_to_read);
  263.  
  264. String aLine;
  265. int numOfLines = 0;
  266.  
  267. while((aLine = bf.readLine()) != null)
  268. {
  269. numOfLines++;
  270. }
  271.  
  272. bf.close();
  273.  
  274. return numOfLines;
  275. }
  276. }
  277.  
  278. import java.io.*;
  279.  
  280. public class WriteFile
  281. {
  282. private String path;
  283. private boolean append_to_file = false;
  284.  
  285. public WriteFile(String file_path)
  286. {
  287. path = file_path;
  288. }
  289.  
  290. public WriteFile(String file_path, boolean append_value)
  291. {
  292. path = file_path;
  293. append_to_file = append_value;
  294. }
  295.  
  296. public void writeToFile(String textLine) throws IOException
  297. {
  298. FileWriter write = new FileWriter(path, append_to_file);
  299. PrintWriter print_line = new PrintWriter(write);
  300.  
  301. print_line.printf("%s" + "%n" , textLine);
  302.  
  303. print_line.close();
  304. }
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement