Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.util.Formatter;
  4.  
  5. import javax.swing.*;
  6. import javax.swing.border.Border;
  7. import javax.swing.BorderFactory;
  8.  
  9.  
  10. public class Gui extends JFrame implements ActionListener
  11. {  
  12.     JLabel statusBar;
  13.     String[] pws = new String[4];
  14.     Formatter format = null;
  15.     JTextField[] questions = {new JTextField("What's the name of your first pet?"),
  16.                                 new JTextField("What's your favorite movie?"),
  17.                                 new JTextField("Who's your favorite author?"),
  18.                                 new JTextField("What's your mother's maiden name?")
  19.                             };
  20.     JPasswordField[] answers = {new JPasswordField(), new JPasswordField(), new JPasswordField(), new JPasswordField()};
  21.     JButton save, cancel;
  22.    
  23.     public Gui()
  24.     {
  25.         super("Security Questions");
  26.         statusBar = new JLabel("  ");
  27.         add(statusBar, BorderLayout.SOUTH);
  28.         Container cPane = this.getContentPane();
  29.         Border border = BorderFactory.createLineBorder(Color.BLACK, 3);
  30.         JPanel panel = new JPanel();
  31.         panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
  32.        
  33.         for(int i=0; i<4; i++){
  34.             panel.add(questions[i]);
  35.             questions[i].setEditable(false);
  36.             panel.add(Box.createRigidArea(new Dimension(0, 2)));
  37.             panel.add(answers[i]);
  38.             panel.add(Box.createRigidArea(new Dimension(0, 2)));
  39.         }
  40.        
  41.         save = new JButton("Save");
  42.         cancel = new JButton("Cancel");
  43.        
  44.         panel.add(save);
  45.         panel.add(Box.createRigidArea(new Dimension(0, 2)));
  46.         panel.add(cancel);
  47.         panel.setBorder(border);
  48.        
  49.         cPane.add(panel);
  50.        
  51.         save.addActionListener(this);
  52.         cancel.addActionListener(this);
  53.        
  54.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  55.         setSize(400, 300);
  56.         setVisible(true);
  57.     }
  58.    
  59.     public void actionPerformed(ActionEvent event)
  60.     {
  61.         String clicked = event.getActionCommand();
  62.         if(clicked.equals("Cancel"))
  63.         {
  64.             int option = JOptionPane.showConfirmDialog(this, "Are you sure?");
  65.             if(option == 0)
  66.             {
  67.                 setVisible(false);
  68.                 this.dispose();
  69.             }
  70.         }
  71.         else if(clicked.equals("Save"))
  72.         {      
  73.             int i = 0;
  74.            
  75.             try{
  76.                 Formatter format = new Formatter("C:\\Users\\Michael\\Desktop\\XSecurityQuestions.txt");
  77.                 for(i=0; i<answers.length; i++){
  78.                     pws[i] = answers[i].getText();
  79.                     format.format("%s\r\n", pws[i]);
  80.                     }
  81.                    
  82.                 statusBar.setText("Save successful.");
  83.                 format.close();
  84.            
  85.             }catch(Exception error){};
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement