CODE2
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import java.awt.Rectangle;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Password extends JFrame{
public int tryTimes = 0;
public Password() {
super("Password Game");
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
setSize(400,400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void jbInit() throws Exception {
this.getContentPane().setLayout(null);
orginalPassword.setBounds(new Rectangle(17, 39, 124, 28));
jLabel1.setText("Enter your orginal Password:");
jLabel1.setBounds(new Rectangle(17, 11, 309, 22));
jLabel2.setText("Try to guess my password:");
jLabel2.setBounds(new Rectangle(17, 122, 273, 19));
tryPassword.setEnabled(false);
tryPassword.setBounds(new Rectangle(17, 162, 202, 27));
tryButton.setBounds(new Rectangle(253, 161, 99, 27));
tryButton.setEnabled(false);
tryButton.setText("Try");
tryButton.addActionListener(new Password_tryButton_actionAdapter(this));
jLabel3.setText("Try Numbers:");
jLabel3.setBounds(new Rectangle(17, 215, 127, 24));
times.setText("0");
times.setBounds(new Rectangle(205, 214, 87, 25));
submitButton.addActionListener(new Password_submitButton_actionAdapter(this));
this.getContentPane().add(jLabel1);
submitButton.setBounds(new Rectangle(192, 39, 106, 26));
submitButton.setText("Ok");
this.getContentPane().add(orginalPassword);
this.getContentPane().add(submitButton);
this.getContentPane().add(jLabel2);
this.getContentPane().add(tryPassword);
this.getContentPane().add(tryButton);
this.getContentPane().add(jLabel3);
this.getContentPane().add(times);
}
JPasswordField orginalPassword = new JPasswordField(5);
JLabel jLabel1 = new JLabel();
JButton submitButton = new JButton();
JLabel jLabel2 = new JLabel();
JTextField tryPassword = new JTextField();
JButton tryButton = new JButton();
JLabel jLabel3 = new JLabel();
JLabel times = new JLabel();
public void submitButton_actionPerformed(ActionEvent e) {
if(orginalPassword.getPassword().length == 0 || orginalPassword.getPassword().length > 5){
JOptionPane.showMessageDialog(this,
"Please enter Password from 5 letter at most");
return;
}
for(int i=0;i<orginalPassword.getPassword().length; i++){
if (orginalPassword.getPassword()[i] > '0' &&
orginalPassword.getPassword()[i] <= '9')
continue;
if (orginalPassword.getPassword()[i] == 'o' ||
orginalPassword.getPassword()[i] == 'm' ||
orginalPassword.getPassword()[i] == 'z')
continue;
JOptionPane.showMessageDialog(this, "Please Password must contain just number from 1 to 9\n" +
"letters 'o', 'm' or 'z'");
return;
}
submitButton.setEnabled(false);
orginalPassword.setEnabled(false);
tryButton.setEnabled(true);
tryPassword.setEnabled(true);
}
public static void main(String [] arg){
Password pass = new Password();
}
public void checkTimes(){
if(tryTimes >= 10){
JOptionPane.showMessageDialog(this,
"You failed to guess the password from 10 try" +
"\nPlay again");
submitButton.setEnabled(true);
orginalPassword.setEnabled(true);
tryButton.setEnabled(false);
tryPassword.setEnabled(false);
tryTimes = 0;
}
times.setText("" + tryTimes);
}
public void tryButton_actionPerformed(ActionEvent e) {
if(tryPassword.getText().length() == 0 || tryPassword.getText().length() > 5){
JOptionPane.showMessageDialog(this,
"Password from 5 letter don't lose your try");
tryTimes++;
checkTimes();
return;
}
for (int i = 0; i < tryPassword.getText().length(); i++) {
if (tryPassword.getText().charAt(i) > '0' &&
tryPassword.getText().charAt(i) <= '9')
continue;
if (tryPassword.getText().charAt(i) == 'o' ||
tryPassword.getText().charAt(i) == 'm' ||
tryPassword.getText().charAt(i) == 'z')
continue;
JOptionPane.showMessageDialog(this,
"Password contain just number from 1 to 9\n" +
"letters 'o', 'm' or 'z'" +
"you lose try");
tryTimes++;
checkTimes();
return;
}
for (int i = 0; i < tryPassword.getText().length(); i++) {
if( i > orginalPassword.getPassword().length){
JOptionPane.showMessageDialog(this,
"another failed try");
tryTimes++;
checkTimes();
return;
}
if (tryPassword.getText().charAt(i) != orginalPassword.getPassword()[i]){
JOptionPane.showMessageDialog(this,
"another failed try");
tryTimes++;
checkTimes();
return;
}
}
JOptionPane.showMessageDialog(this,
"congratulation!!! you have success try and the password is:" +
"\n" + tryPassword.getText());
JOptionPane.showMessageDialog(this,
"Play again");
submitButton.setEnabled(true);
orginalPassword.setEnabled(true);
tryButton.setEnabled(false);
tryPassword.setEnabled(false);
tryTimes = 0;
times.setText("" + tryTimes);
}
}
class Password_tryButton_actionAdapter implements ActionListener {
private Password adaptee;
Password_tryButton_actionAdapter(Password adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.tryButton_actionPerformed(e);
}
}
class Password_submitButton_actionAdapter implements ActionListener {
private Password adaptee;
Password_submitButton_actionAdapter(Password adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.submitButton_actionPerformed(e);
}
}
السلام عليكم
يا شباب حدا يحكم هل هذا الكود يحتوي على خلفيات
أو أي إضافات عن المطلوب من قبل الأخ حياة كلبي