Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import java.util.Formatter;
- import javax.swing.*;
- import javax.swing.border.Border;
- import javax.swing.BorderFactory;
- public class Gui extends JFrame implements ActionListener
- {
- JLabel statusBar;
- String[] pws = new String[4];
- Formatter format = null;
- JTextField[] questions = {new JTextField("What's the name of your first pet?"),
- new JTextField("What's your favorite movie?"),
- new JTextField("Who's your favorite author?"),
- new JTextField("What's your mother's maiden name?")
- };
- JPasswordField[] answers = {new JPasswordField(), new JPasswordField(), new JPasswordField(), new JPasswordField()};
- JButton save, cancel;
- public Gui()
- {
- super("Security Questions");
- statusBar = new JLabel(" ");
- add(statusBar, BorderLayout.SOUTH);
- Container cPane = this.getContentPane();
- Border border = BorderFactory.createLineBorder(Color.BLACK, 3);
- JPanel panel = new JPanel();
- panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
- for(int i=0; i<4; i++){
- panel.add(questions[i]);
- questions[i].setEditable(false);
- panel.add(Box.createRigidArea(new Dimension(0, 2)));
- panel.add(answers[i]);
- panel.add(Box.createRigidArea(new Dimension(0, 2)));
- }
- save = new JButton("Save");
- cancel = new JButton("Cancel");
- panel.add(save);
- panel.add(Box.createRigidArea(new Dimension(0, 2)));
- panel.add(cancel);
- panel.setBorder(border);
- cPane.add(panel);
- save.addActionListener(this);
- cancel.addActionListener(this);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setSize(400, 300);
- setVisible(true);
- }
- public void actionPerformed(ActionEvent event)
- {
- String clicked = event.getActionCommand();
- if(clicked.equals("Cancel"))
- {
- int option = JOptionPane.showConfirmDialog(this, "Are you sure?");
- if(option == 0)
- {
- setVisible(false);
- this.dispose();
- }
- }
- else if(clicked.equals("Save"))
- {
- int i = 0;
- try{
- Formatter format = new Formatter("C:\\Users\\Michael\\Desktop\\XSecurityQuestions.txt");
- for(i=0; i<answers.length; i++){
- pws[i] = answers[i].getText();
- format.format("%s\r\n", pws[i]);
- }
- statusBar.setText("Save successful.");
- format.close();
- }catch(Exception error){};
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement