Guest User

Untitled

a guest
Mar 31st, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. package Khan2ndSemAPL;
  2.  
  3. import java.awt.Container;
  4. import java.awt.GridLayout;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.io.FileNotFoundException;
  8. import java.io.FileOutputStream;
  9. import java.io.IOException;
  10.  
  11. import javax.swing.*;
  12.  
  13. public class Activity_32_Writing_toText_FileOutputStream extends JFrame implements ActionListener
  14. {
  15. FileOutputStream fswriter;
  16. public JLabel l1, l2;
  17. public JTextField t1, t2;
  18. public JButton b1, b2;
  19. Activity_32_Writing_toText_FileOutputStream()
  20. {
  21. Container c = getContentPane();
  22. c.setLayout(new GridLayout(3, 2));
  23. l1 = new JLabel("Customer Login Name: ");
  24. l2 = new JLabel("Password: ");
  25. t1 = new JTextField();
  26. t2 = new JPasswordField();
  27. b1 = new JButton("Login");
  28. b2 = new JButton("Clear");
  29.  
  30. c.add(l1);
  31. c.add(l2);
  32. c.add(t1);
  33. c.add(t2);
  34. c.add(b1);
  35. c.add(b2);
  36.  
  37. b1.addActionListener(this);
  38. b2.addActionListener(this);
  39.  
  40. pack();
  41. setVisible(true);
  42. this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  43. this.setLocationRelativeTo(null);
  44. }
  45.  
  46. public void actionPerformed(ActionEvent ae)
  47. {
  48. if(ae.getSource() == b1)
  49. {
  50. if(t1.getText().isEmpty() || t1.getText().trim().equals("")
  51. || t2.getText().isEmpty() || t2.getText().trim().equals(""))
  52. {
  53. System.err.println("details are incomplete");
  54. JOptionPane.showMessageDialog(null, "Details are incomplete");
  55. }
  56. else
  57. {
  58. try
  59. {
  60. fswriter = new FileOutputStream("C:\\Users\\10-0235C\\Documents\\migs\\login.text", false);
  61. String temp = "PASSWORD: " + t1.getText() + " USERNAME: " + t2.getText();
  62. //String temp = t1.getText() + t2.getText();
  63. //System.out.println(temp.getBytes());
  64. //System.out.println(temp.getBytes());
  65. fswriter.write(temp.getBytes());
  66. System.out.println("finished writing details to file");
  67. JOptionPane.showMessageDialog(null, "finished writing details to file");
  68. fswriter.close();
  69. }
  70. catch(FileNotFoundException e)
  71. {
  72. System.out.println("cannot find login.txt file " + e.toString());
  73. e.printStackTrace();
  74. }
  75. catch(IOException e)
  76. {
  77. System.out.println("Error writing to file " + e.toString());
  78. e.printStackTrace();
  79. }
  80.  
  81. }
  82. }
  83. if(ae.getSource() == b2)
  84. {
  85. t1.setText(null);
  86. t2.setText(null);
  87. }
  88.  
  89. }
  90.  
  91. public static void main(String args[])
  92. {
  93. Activity_32_Writing_toText_FileOutputStream e = new Activity_32_Writing_toText_FileOutputStream();
  94. }
  95.  
  96.  
  97. }
Add Comment
Please, Sign In to add comment