1 27 package olstore.client; 28 29 import java.awt.Dimension; 30 import java.awt.GridLayout; 31 import java.awt.Toolkit; 32 import java.awt.event.ActionEvent; 33 import java.awt.event.ActionListener; 34 35 import javax.swing.JButton; 36 import javax.swing.JDialog; 37 import javax.swing.JFrame; 38 import javax.swing.JLabel; 39 import javax.swing.JPasswordField; 40 import javax.swing.JTextField; 41 42 45 public class LoginDialog extends JDialog implements ActionListener { 46 47 48 private JTextField username; 49 50 51 private JPasswordField password; 52 53 54 private JButton bLogin; 55 56 57 private JButton bCancel; 58 59 60 private static final int FIELD_LENGTH = 15; 61 62 63 private boolean loginPress = false; 64 65 71 public LoginDialog(JFrame parent) { 72 super(parent, "Enter username and password..."); 73 username = new JTextField(FIELD_LENGTH); 74 password = new JPasswordField(FIELD_LENGTH); 75 76 setBackground(OlstoreSwingClient.itemColor1); 77 78 bLogin = new JButton("Login"); 79 bLogin.addActionListener(this); 80 bLogin.setBackground(OlstoreSwingClient.buttonColor); 81 82 bCancel = new JButton("Cancel"); 83 bCancel.addActionListener(this); 84 bCancel.setBackground(OlstoreSwingClient.buttonColor); 85 86 getContentPane().setLayout(new GridLayout(0, 2, 5, 5)); 87 88 JLabel userLabel = new JLabel("Username:"); 89 userLabel.setBackground(OlstoreSwingClient.itemColor1); 90 JLabel passwordLabel = new JLabel("Password:"); 91 passwordLabel.setBackground(OlstoreSwingClient.itemColor1); 92 getContentPane().add(userLabel); 93 getContentPane().add(username); 94 getContentPane().add(passwordLabel); 95 getContentPane().add(password); 96 getContentPane().add(bLogin); 97 getContentPane().add(bCancel); 98 99 setModal(true); 100 101 pack(); 102 Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); 103 setLocation((int) d.getWidth() / 2 - getWidth(), (int) d.getHeight() 104 / 2 - getHeight()); 105 } 106 107 111 public void actionPerformed(ActionEvent e) { 112 if (e.getSource().equals(bLogin)) { 113 loginPress = true; 114 } 115 setModal(false); 116 hide(); 117 } 118 119 123 public boolean login() { 124 return loginPress; 125 } 126 127 131 public String[] getLogin() { 132 String[] result = new String[2]; 133 134 result[0] = username.getText(); 135 result[1] = new String(password.getPassword()); 136 137 return result; 138 139 } 140 } 141 | Popular Tags |