Created
January 16, 2013 17:25
-
-
Save yogendra/4548967 to your computer and use it in GitHub Desktop.
Java sample code - sujith
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.util.ArrayList; | |
import java.util.List; | |
import javax.swing.JButton; | |
import javax.swing.JFrame; | |
import javax.swing.JLabel; | |
import javax.swing.JOptionPane; | |
import javax.swing.JPasswordField; | |
import javax.swing.JTextField; | |
class sujith implements ActionListener { | |
JTextField text1; | |
JPasswordField text2; | |
JFrame f = new JFrame(); | |
JButton bn, bn1, bn2; | |
JLabel l1, l2; | |
// String [] name = new String[100]; | |
List<String> name = new ArrayList<String>(); | |
// String[] password = new String[100]; | |
List<String> password = new ArrayList<String>(); | |
int count = 0, i; | |
public sujith() { | |
text1 = new JTextField(8); | |
text2 = new JPasswordField(8); | |
bn2 = new JButton("Save"); | |
bn = new JButton("Login"); | |
bn1 = new JButton("Bye"); | |
l1 = new JLabel("Username"); | |
l2 = new JLabel("Password"); | |
f.add(l1); | |
f.add(text1); | |
f.add(l2); | |
f.add(text2); | |
f.add(bn); | |
f.add(bn1); | |
f.add(bn2); | |
bn1.addActionListener(this); | |
bn.addActionListener(this); | |
bn2.addActionListener(this); | |
f.setSize(300, 400); | |
f.setVisible(true); | |
launch(); | |
} | |
public void launch() { | |
f.setLayout(null); | |
l1.setBounds(20, 40, 100, 50); | |
text1.setBounds(100, 55, 150, 20); | |
l2.setBounds(20, 60, 70, 50); | |
text2.setBounds(100, 75, 150, 20); | |
bn.setBounds(130, 100, 85, 30); | |
bn1.setBounds(130, 140, 85, 30); | |
bn2.setBounds(130, 180, 85, 30); | |
} | |
public void actionPerformed(ActionEvent e) { | |
if (e.getActionCommand().equals("Save")) { | |
// name[count] = text1.getText(); | |
name.add(text1.getText()); | |
// password[count] = text2.getText(); | |
password.add(new String(text2.getPassword())); | |
text1.setText(""); | |
text2.setText(""); | |
count++; | |
} | |
if (e.getActionCommand().equals("Bye")) { | |
System.exit(0); | |
} | |
if (e.getActionCommand().equals("Login")) { | |
for (i = 0; i < name.size(); i++) { | |
// if (name[i].equals(text1.getText()) | |
// && password[i].equals(text2.getText())) { | |
if (name.get(i).equals(text1.getText()) | |
&& password.get(i).equals( | |
new String(text2.getPassword()))) { | |
JOptionPane.showMessageDialog(null, "Login Success"); | |
} else { | |
JOptionPane.showMessageDialog(null, | |
"Incorrect username and password"); | |
} | |
} | |
} | |
} | |
public static void main(String[] args) { | |
sujith obj = new sujith(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment