Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

where to put Loop/IfElse in my code

$
0
0
Hi there, i have an assignment which is to create a login system. if it enters correctly it will prompt that they logged in successfully. If wrongly, they would be denied(no databases required).

Then, exit the system when password for payment is entered wrongly for 3 times.

I have tried placing For loop around but it does not work, hence i came to ask for some advice.

Here is my code,

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Payment extends JFrame implements ActionListener
{  
  private JLabel jusername = new JLabel("Username: ");
  private JTextField jusername1 = new JTextField(10);
  private JLabel jpassword = new JLabel("Password: ");
  private JTextField jpassword1 = new JTextField(10);
  private JButton jbtEnter = new JButton("Enter");
  private JButton jbtExit = new JButton("Exit");
  
  String a = "boss";
  String b = "happy";
  
  
  public Payment()
  {
  
  BorderLayout b = new BorderLayout();
  
  setLayout(B)/>;
  

  
  JPanel p1 = new JPanel();  
  JPanel p2 = new JPanel();
  JPanel p3 = new JPanel();
  

  
    p1.add(jbtEnter);
    p1.add(jbtExit);
    p2.add(jusername);
    p3.add(jpassword);
    p2.add(jusername1);
    p3.add(jpassword1);

    
  
    jbtEnter.addActionListener(this);
    jbtExit.addActionListener(this);
    
 
  add(p2, BorderLayout.NORTH);
  add(p3, BorderLayout.CENTER);
  add(p1, BorderLayout.SOUTH);

  }

  
  public static void main(String[] args)
  {
    Payment frame = new Payment();
    frame.setTitle("Payment");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300,150);
    
    frame.setVisible(true);
  }


public void actionPerformed(ActionEvent e) {	
	String user = jusername1.getText();
	String pass = jpassword1.getText();
	
	
	if (e.getSource()== jbtEnter)
	{
		if(user.equals(a)&& pass.equals(B)/>)
		{
			JOptionPane.showMessageDialog(null,"Login Successful! ","Login Success",JOptionPane.PLAIN_MESSAGE);
			JOptionPane.showMessageDialog(null,"Items will be devlivered in 2 working days");
		}
		else
			
		{
			JOptionPane.showMessageDialog(null,"Access Denied!","Login Incorrect!",JOptionPane.PLAIN_MESSAGE);
		}
	}
	
	
	if (e.getSource() == jbtExit)
	{
		System.exit(0);
	}
	
}
  
}



the error is that it will loop 3 times when I have just pressed the enter button once.

Thanks in advance. :D

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>