Advertisement
Guest User

Untitled

a guest
Apr 18th, 2016
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.07 KB | None | 0 0
  1. package andreas.christou.vertretungsplan.main;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.GridBagConstraints;
  6. import java.awt.GridBagLayout;
  7. import java.awt.Insets;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.io.BufferedReader;
  11. import java.io.BufferedWriter;
  12. import java.io.File;
  13. import java.io.FileNotFoundException;
  14. import java.io.FileWriter;
  15. import java.io.IOException;
  16. import java.io.InputStreamReader;
  17. import java.net.Authenticator;
  18. import java.net.PasswordAuthentication;
  19. import java.net.URL;
  20. import java.util.ArrayList;
  21. import java.util.Date;
  22. import java.util.Scanner;
  23.  
  24. import javax.swing.JButton;
  25. import javax.swing.JFrame;
  26. import javax.swing.JLabel;
  27. import javax.swing.JPanel;
  28. import javax.swing.JPasswordField;
  29. import javax.swing.JRadioButton;
  30. import javax.swing.JTextField;
  31. import javax.swing.border.EmptyBorder;
  32.  
  33.  
  34. public class Vertretungsplan{
  35. static File file = new File(System.getProperty("user.home") + "\\VertretungsplanConfig.txt");
  36. private static JFrame frame;
  37. private static JPanel mainpanel;
  38. private static JPanel panel;
  39. public static JButton button;
  40. public static JRadioButton rem;
  41. private static JLabel user;
  42. private static JLabel pw;
  43. private static JLabel klass;
  44. public static JLabel er;
  45. private static JTextField userin;
  46. private static JPasswordField pwin;
  47. private static JTextField klassin;
  48. static int wotag;
  49.  
  50. private static JPanel auswahl;
  51. public static JButton heute;
  52. public static JButton morgen;
  53. public static JButton mo;
  54. public static JButton di;
  55. public static JButton mi;
  56. public static JButton don;
  57. public static JButton fr;
  58. public static JButton myklass;
  59.  
  60.  
  61.  
  62. public static void main(String[] args) {
  63. button = new JButton("Login");
  64. button.addActionListener(new Action());
  65. rem = new JRadioButton("Login speichern");
  66.  
  67. gui();
  68.  
  69.  
  70. }
  71. static class Action implements ActionListener{
  72.  
  73. public void actionPerformed(ActionEvent e){
  74.  
  75. if(e.getSource().equals(button)){
  76.  
  77. try {
  78. auth();
  79. saveData();
  80. } catch (Exception ex) {
  81. button.setText("Login");
  82. er.setText("Fehler bei der Anmeldung!");
  83. }
  84. }
  85.  
  86.  
  87.  
  88.  
  89. if(e.getSource().equals(heute)){
  90. try {
  91.  
  92. } catch (Exception ex) {button.setText("Fehler");}}
  93.  
  94.  
  95. }
  96.  
  97. private void saveData() {
  98. if(rem.isSelected()){
  99. SAVE();
  100. }
  101.  
  102. }
  103. }
  104.  
  105. public static void gui(){
  106.  
  107. frame = new JFrame("Test");
  108. frame.setSize(800, 600);
  109. frame.setVisible(true);
  110. button.requestFocus();
  111. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  112. mainpanel = new JPanel(new BorderLayout());
  113. panel = new JPanel(new GridBagLayout());
  114. auswahl = new JPanel(new GridBagLayout());
  115.  
  116. user = new JLabel("Username");
  117. pw = new JLabel("Passwort");
  118. klass = new JLabel("Klasse (Optional)");
  119. er= new JLabel("");
  120. er.setForeground(Color.RED);
  121. userin = new JTextField("", 10);
  122. pwin = new JPasswordField("",10);
  123. klassin = new JTextField("",10);
  124.  
  125. heute = new JButton("Heute");
  126. morgen = new JButton("Morgen");
  127. mo = new JButton("Montag");
  128. di = new JButton("Dienstag");
  129. mi = new JButton("Mittwoch");
  130. don = new JButton("Donnerstag");
  131. fr = new JButton("Freitag");
  132. myklass = new JButton("Nur für meine Klasse");
  133.  
  134.  
  135. myklass.setVisible(false);
  136. klass.setVisible(false);
  137. klassin.setVisible(false);
  138.  
  139.  
  140. GridBagConstraints c = new GridBagConstraints();
  141. c.insets = new Insets(15, 15, 15, 15);
  142. c.anchor = GridBagConstraints.NORTH;
  143.  
  144. c.gridx = 0;
  145. c.gridy = 1;
  146. panel.add(user,c);
  147.  
  148. c.gridx = 0;
  149. c.gridy = 2;
  150. panel.add(pw,c);
  151.  
  152. c.gridx = 0;
  153. c.gridy = 3;
  154. panel.add(klass,c);
  155.  
  156. c.gridx = 1;
  157. c.gridy = 1;
  158. panel.add(userin,c);
  159.  
  160. c.gridx = 1;
  161. c.gridy = 2;
  162. panel.add(pwin,c);
  163.  
  164. c.gridx = 1;
  165. c.gridy = 3;
  166. panel.add(klassin,c);
  167.  
  168. c.gridx = 2;
  169. c.gridy = 4;
  170. panel.add(button, c);
  171.  
  172. c.gridx = 3;
  173. c.gridy = 4;
  174. panel.add(er,c);
  175.  
  176. c.gridx = 1;
  177. c.gridy = 4;
  178. panel.add(rem, c);
  179. mainpanel.setBorder(new EmptyBorder(10, 10, 10, 10));
  180. mainpanel.add(panel, BorderLayout.WEST);
  181.  
  182. frame.add(mainpanel);
  183.  
  184. er.setForeground(Color.RED);
  185. UPDATE();
  186. }
  187.  
  188.  
  189. private static void addToAuswahl() {
  190. GridBagConstraints c = new GridBagConstraints();
  191. c.insets = new Insets(15, 15, 15, 15);
  192. c.anchor = GridBagConstraints.WEST;
  193.  
  194. c.gridx = 0;
  195. c.gridy = 0;
  196. auswahl.add(heute,c);
  197.  
  198. c.gridx = 1;
  199. c.gridy = 0;
  200. auswahl.add(morgen,c);
  201.  
  202. c.gridx = 0;
  203. c.gridy = 1;
  204. auswahl.add(mo,c);
  205.  
  206. c.gridx = 1;
  207. c.gridy = 1;
  208. auswahl.add(di,c);
  209.  
  210. c.gridx = 2;
  211. c.gridy = 1;
  212. auswahl.add(mi,c);
  213.  
  214. c.gridx = 3;
  215. c.gridy = 1;
  216. auswahl.add(don,c);
  217.  
  218. c.gridx = 4;
  219. c.gridy = 1;
  220. auswahl.add(fr, c);
  221.  
  222. c.gridx = 4;
  223. c.gridy = 0;
  224. auswahl.add(myklass,c);
  225. mainpanel.add(auswahl, BorderLayout.NORTH);
  226. auswahl.setVisible(true);
  227.  
  228. }
  229.  
  230.  
  231. public static void auth() throws Exception {
  232.  
  233. Authenticator.setDefault(new Authenticator(){
  234. protected PasswordAuthentication getPasswordAuthentication() {
  235. return new PasswordAuthentication(userin.getText(),pwin.getPassword());
  236. }
  237. });
  238. URL url = new URL("LINK"+wotag);
  239. BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
  240. String line = null;
  241. while((line = br.readLine())!= null){
  242. System.out.println(line);
  243. }
  244. br.close();
  245. panel.setVisible(false);
  246. addToAuswahl();
  247.  
  248. }
  249. @SuppressWarnings("deprecation")
  250. public void zeitsteuerung() {
  251.  
  252. ArrayList<String> tagesdatei = new ArrayList<String>();
  253. tagesdatei.add("LINK_mo.htm");
  254. tagesdatei.add("LINK_di.htm");
  255. tagesdatei.add("LINK_mi.htm");
  256. tagesdatei.add("LINK_do.htm");
  257. tagesdatei.add("LINK_fr.htm");
  258. tagesdatei.add("LINK_mo.htm");
  259. tagesdatei.add("LINK_mo.htm");
  260. tagesdatei.add("LINK_fr.htm");
  261.  
  262. Date heutiger_tag = new Date();
  263. wotag = heutiger_tag.getDay();
  264.  
  265. }
  266.  
  267.  
  268.  
  269.  
  270. public static void SAVE(){
  271.  
  272.  
  273.  
  274. try {
  275. if(!file.exists()) file.createNewFile();
  276.  
  277. BufferedWriter bw = new BufferedWriter(new FileWriter(file.getAbsolutePath()));
  278. bw.write(userin.getText());
  279. bw.newLine();
  280. bw.write(pwin.getPassword());
  281. bw.close();
  282.  
  283. } catch (IOException e) { e.printStackTrace(); }
  284.  
  285. }
  286.  
  287.  
  288.  
  289.  
  290. public static void UPDATE(){
  291.  
  292. try {
  293. if(file.exists()){
  294.  
  295. Scanner scan = new Scanner(file);
  296.  
  297. userin.setText(scan.nextLine());
  298. pwin.setText(scan.nextLine());
  299. rem.setSelected(true);
  300. scan.close();
  301. }
  302.  
  303. } catch (FileNotFoundException e) {
  304. e.printStackTrace();
  305. }
  306.  
  307. }
  308.  
  309.  
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement