Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package andreas.christou.vertretungsplan.main;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.GridBagConstraints;
- import java.awt.GridBagLayout;
- import java.awt.Insets;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.Authenticator;
- import java.net.PasswordAuthentication;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.Scanner;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JPasswordField;
- import javax.swing.JRadioButton;
- import javax.swing.JTextField;
- import javax.swing.border.EmptyBorder;
- public class Vertretungsplan{
- static File file = new File(System.getProperty("user.home") + "\\VertretungsplanConfig.txt");
- private static JFrame frame;
- private static JPanel mainpanel;
- private static JPanel panel;
- public static JButton button;
- public static JRadioButton rem;
- private static JLabel user;
- private static JLabel pw;
- private static JLabel klass;
- public static JLabel er;
- private static JTextField userin;
- private static JPasswordField pwin;
- private static JTextField klassin;
- static int wotag;
- private static JPanel auswahl;
- public static JButton heute;
- public static JButton morgen;
- public static JButton mo;
- public static JButton di;
- public static JButton mi;
- public static JButton don;
- public static JButton fr;
- public static JButton myklass;
- public static void main(String[] args) {
- button = new JButton("Login");
- button.addActionListener(new Action());
- rem = new JRadioButton("Login speichern");
- gui();
- }
- static class Action implements ActionListener{
- public void actionPerformed(ActionEvent e){
- if(e.getSource().equals(button)){
- try {
- auth();
- saveData();
- } catch (Exception ex) {
- button.setText("Login");
- er.setText("Fehler bei der Anmeldung!");
- }
- }
- if(e.getSource().equals(heute)){
- try {
- } catch (Exception ex) {button.setText("Fehler");}}
- }
- private void saveData() {
- if(rem.isSelected()){
- SAVE();
- }
- }
- }
- public static void gui(){
- frame = new JFrame("Test");
- frame.setSize(800, 600);
- frame.setVisible(true);
- button.requestFocus();
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- mainpanel = new JPanel(new BorderLayout());
- panel = new JPanel(new GridBagLayout());
- auswahl = new JPanel(new GridBagLayout());
- user = new JLabel("Username");
- pw = new JLabel("Passwort");
- klass = new JLabel("Klasse (Optional)");
- er= new JLabel("");
- er.setForeground(Color.RED);
- userin = new JTextField("", 10);
- pwin = new JPasswordField("",10);
- klassin = new JTextField("",10);
- heute = new JButton("Heute");
- morgen = new JButton("Morgen");
- mo = new JButton("Montag");
- di = new JButton("Dienstag");
- mi = new JButton("Mittwoch");
- don = new JButton("Donnerstag");
- fr = new JButton("Freitag");
- myklass = new JButton("Nur für meine Klasse");
- myklass.setVisible(false);
- klass.setVisible(false);
- klassin.setVisible(false);
- GridBagConstraints c = new GridBagConstraints();
- c.insets = new Insets(15, 15, 15, 15);
- c.anchor = GridBagConstraints.NORTH;
- c.gridx = 0;
- c.gridy = 1;
- panel.add(user,c);
- c.gridx = 0;
- c.gridy = 2;
- panel.add(pw,c);
- c.gridx = 0;
- c.gridy = 3;
- panel.add(klass,c);
- c.gridx = 1;
- c.gridy = 1;
- panel.add(userin,c);
- c.gridx = 1;
- c.gridy = 2;
- panel.add(pwin,c);
- c.gridx = 1;
- c.gridy = 3;
- panel.add(klassin,c);
- c.gridx = 2;
- c.gridy = 4;
- panel.add(button, c);
- c.gridx = 3;
- c.gridy = 4;
- panel.add(er,c);
- c.gridx = 1;
- c.gridy = 4;
- panel.add(rem, c);
- mainpanel.setBorder(new EmptyBorder(10, 10, 10, 10));
- mainpanel.add(panel, BorderLayout.WEST);
- frame.add(mainpanel);
- er.setForeground(Color.RED);
- UPDATE();
- }
- private static void addToAuswahl() {
- GridBagConstraints c = new GridBagConstraints();
- c.insets = new Insets(15, 15, 15, 15);
- c.anchor = GridBagConstraints.WEST;
- c.gridx = 0;
- c.gridy = 0;
- auswahl.add(heute,c);
- c.gridx = 1;
- c.gridy = 0;
- auswahl.add(morgen,c);
- c.gridx = 0;
- c.gridy = 1;
- auswahl.add(mo,c);
- c.gridx = 1;
- c.gridy = 1;
- auswahl.add(di,c);
- c.gridx = 2;
- c.gridy = 1;
- auswahl.add(mi,c);
- c.gridx = 3;
- c.gridy = 1;
- auswahl.add(don,c);
- c.gridx = 4;
- c.gridy = 1;
- auswahl.add(fr, c);
- c.gridx = 4;
- c.gridy = 0;
- auswahl.add(myklass,c);
- mainpanel.add(auswahl, BorderLayout.NORTH);
- auswahl.setVisible(true);
- }
- public static void auth() throws Exception {
- Authenticator.setDefault(new Authenticator(){
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(userin.getText(),pwin.getPassword());
- }
- });
- URL url = new URL("LINK"+wotag);
- BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
- String line = null;
- while((line = br.readLine())!= null){
- System.out.println(line);
- }
- br.close();
- panel.setVisible(false);
- addToAuswahl();
- }
- @SuppressWarnings("deprecation")
- public void zeitsteuerung() {
- ArrayList<String> tagesdatei = new ArrayList<String>();
- tagesdatei.add("LINK_mo.htm");
- tagesdatei.add("LINK_di.htm");
- tagesdatei.add("LINK_mi.htm");
- tagesdatei.add("LINK_do.htm");
- tagesdatei.add("LINK_fr.htm");
- tagesdatei.add("LINK_mo.htm");
- tagesdatei.add("LINK_mo.htm");
- tagesdatei.add("LINK_fr.htm");
- Date heutiger_tag = new Date();
- wotag = heutiger_tag.getDay();
- }
- public static void SAVE(){
- try {
- if(!file.exists()) file.createNewFile();
- BufferedWriter bw = new BufferedWriter(new FileWriter(file.getAbsolutePath()));
- bw.write(userin.getText());
- bw.newLine();
- bw.write(pwin.getPassword());
- bw.close();
- } catch (IOException e) { e.printStackTrace(); }
- }
- public static void UPDATE(){
- try {
- if(file.exists()){
- Scanner scan = new Scanner(file);
- userin.setText(scan.nextLine());
- pwin.setText(scan.nextLine());
- rem.setSelected(true);
- scan.close();
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement