Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class TestFrame extends JFrame {
- private PassWordDialog passDialog;
- public TestFrame() {
- passDialog = new PassWordDialog(this, true);
- passDialog.setVisible(true);
- }
- public static void main(String[] args) {
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- JFrame frame = new TestFrame();
- frame.getContentPane().setBackground(Color.BLACK);
- frame.setTitle("Logged In");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setLocationRelativeTo(null);
- frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
- }
- });
- }
- }
- class PassWordDialog extends JDialog {
- private final JLabel jlblUsername = new JLabel("Username");
- private final JLabel jlblPassword = new JLabel("Password");
- private final JTextField jtfUsername = new JTextField(15);
- private final JPasswordField jpfPassword = new JPasswordField();
- private final JButton jbtOk = new JButton("Login");
- private final JButton jbtCancel = new JButton("Cancel");
- private final JLabel jlblStatus = new JLabel(" ");
- public PassWordDialog() {
- this(null, true);
- }
- public PassWordDialog(final JFrame parent, boolean modal) {
- super(parent, modal);
- JPanel p3 = new JPanel(new GridLayout(2, 1));
- p3.add(jlblUsername);
- p3.add(jlblPassword);
- JPanel p4 = new JPanel(new GridLayout(2, 1));
- p4.add(jtfUsername);
- p4.add(jpfPassword);
- JPanel p1 = new JPanel();
- p1.add(p3);
- p1.add(p4);
- JPanel p2 = new JPanel();
- p2.add(jbtOk);
- p2.add(jbtCancel);
- JPanel p5 = new JPanel(new BorderLayout());
- p5.add(p2, BorderLayout.CENTER);
- p5.add(jlblStatus, BorderLayout.NORTH);
- jlblStatus.setForeground(Color.RED);
- jlblStatus.setHorizontalAlignment(SwingConstants.CENTER);
- setLayout(new BorderLayout());
- add(p1, BorderLayout.CENTER);
- add(p5, BorderLayout.SOUTH);
- pack();
- setLocationRelativeTo(null);
- setDefaultCloseOperation(DISPOSE_ON_CLOSE);
- addWindowListener(new WindowAdapter() {
- @Override
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- });
- jbtOk.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if ("stackoverflow".equals(String.valueOf(jpfPassword.getPassword()))
- && "stackoverflow".equals(jtfUsername.getText())) {
- parent.setVisible(true);
- setVisible(false);
- } else {
- jlblStatus.setText("Invalid username or password");
- }
- }
- });
- jbtCancel.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- setVisible(false);
- parent.dispose();
- System.exit(0);
- }
- });
- }
- }
- public class MyApp {
- private static boolean isAuthenticated;
- public static void main( String args[] ) {
- isAuthenticated = false;
- while( !isAuthenicated ) {
- authenticateUser();
- try{ Thread.sleep( 200 ); } catch( InterruptedException ie ){ }
- }
- new JFrame();
- // finish rest of GUI code.
- }
- private static void authenticateUser(){
- dialog = new MyAuthenticationDialog();
- dialog.show();
- if( isValid( dialog.getUsername(), dialog.getPassword() )
- isAuthenticated = true;
- else
- isAuthenticated = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement