Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can we properly provide alignment to these forms..
check this and please tell...




Java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class MainFrame extends JPanel  {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public MainFrame() {
	      super(true);

	      final JFrame frame = new JFrame("Password Encrypter & Saver");
	      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	      JMenuBar menubar = new JMenuBar();
		  JMenu loginmenu = new JMenu("Login");
		  loginmenu.add(new JSeparator());
		  JMenu registermenu = new JMenu("Registration");
		  registermenu.add(new JSeparator());
		  JMenu manualmenu = new JMenu("Help");
		  manualmenu.add(new JSeparator());
		  
          JMenuItem loginItem1 = new JMenuItem("Login to Project");
                    
		  JMenuItem registerItem1 = new JMenuItem("Register to Project");
		 
		  JMenuItem manualItem1 = new JMenuItem("Manual to use Project");
		  
		  loginmenu.add(loginItem1);
		  registermenu.add(registerItem1);
		  manualmenu.add(manualItem1);
		  
		  menubar.add(loginmenu);
		  menubar.add(registermenu);
		  menubar.add(manualmenu);
		  frame.setJMenuBar(menubar);
		  frame.setSize(500,500);
		  frame.setVisible(true);
		  
		  loginItem1.addActionListener(	new ActionListener() {
		       public void actionPerformed(ActionEvent e) {
		       setVisible(false);
		       new Login();
		      }
		  });
		  
		  registerItem1.addActionListener(	new ActionListener() {
		       public void actionPerformed(ActionEvent e) {
		       setVisible(false);
		       new Registration();
		      }
		  });
		  
		  manualItem1.addActionListener(	new ActionListener() {
		       public void actionPerformed(ActionEvent e) {
		       setVisible(false);
		       new MainFrame();
		      }
		  });
		  
		  
	}  
	
		public static void main(String[] args) {
		new MainFrame();
		/*new Login();
		new Registration();
		new Userframe();
		new Savingframe();
		new Retrievingframe();
		new Showpasswordframe();*/
	}
}


Java
class Login extends JFrame
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	final JButton loginbtn;
	final JPanel lgnpanel;
	final JLabel usernameLabel, passwordLabel;
	final JTextField  usernameField;
	final JPasswordField passwordField;
	
	Login()
	{
		super("Login form");
		
		usernameLabel = new JLabel("Username:");
		usernameField = new JTextField(15);

		passwordLabel = new JLabel("Password:");
		passwordField = new JPasswordField(15);

		loginbtn = new JButton("Login");
		
		lgnpanel = new JPanel(new GridLayout(3,2));
		lgnpanel.add(usernameLabel);
		lgnpanel.add(usernameField);
		lgnpanel.add(passwordLabel);
		lgnpanel.add(passwordField);
		lgnpanel.add(loginbtn);
		// to fill the GridLayout
		add(lgnpanel,BorderLayout.CENTER);
		setTitle("LOGIN FRAME");
		setSize(400,200);
		setVisible(true);
		loginbtn.addActionListener(
	    new ActionListener() {
	        public void actionPerformed(ActionEvent e) {
	        // do something for valid login
	        JOptionPane.showMessageDialog(lgnpanel,"Login successful");
	        setVisible(false);
	        new Userframe();
	        }
	    });
		/*loginbtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			// do something 
			JOptionPane.showMessageDialog(panel,"Sorry Invalid Username or Password");
			new Login();
			}
	   });*/
	}
}



Java
class Registration extends JFrame
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	final JButton submitBtn;
	final JPanel panel;
	final JLabel usernameLabel, passwordLabel,retypepasswordLabel,answerlabel;
	final JTextField  usernameField,combofield,answerfield;
	final JPasswordField passwordField,retypepasswordField;
	final JComboBox combo;
	  
	Registration()
	{
		super("Registration Form");
		
		String course[] = {"Select Security Question","your first crush?","your favourite sportperson?","Your first vehicle number?","Your lucky number?"};
		
		usernameLabel = new JLabel("Choose Username:");
		usernameField = new JTextField(15);

		passwordLabel = new JLabel("Choose Password:");
		passwordField = new JPasswordField(15);
		
		retypepasswordLabel = new JLabel("Retype Password:");
		retypepasswordField = new JPasswordField(15);
		
		combo = new JComboBox(course);
		combo.setBackground(Color.white);
		combo.setForeground(Color.blue);
		
		answerlabel = new JLabel("Answer of Question");
		answerfield = new JTextField(20);

		combofield = new JTextField(15);
		
		submitBtn = new JButton("Submit");
		
		panel = new JPanel(new GridLayout(6,6));
		panel.add(usernameLabel);
		panel.add(usernameField);
		panel.add(passwordLabel);
		panel.add(passwordField);
		panel.add(retypepasswordLabel);
		panel.add(retypepasswordField);
		panel.add(combo);
		panel.add(combofield);
		panel.add(answerlabel);
		panel.add(answerfield);
		panel.add(submitBtn);
		// to fill the GridLayout
		panel.add(new JLabel(""));
		add(panel,BorderLayout.CENTER);
		
		setTitle("REGISTRATION FRAME");
		setSize(400,300);
		setVisible(true);
		
		combo.addItemListener(new ItemListener(){
		public void itemStateChanged(ItemEvent ie){
		String str = (String)combo.getSelectedItem();
		combofield.setText(str);
		  }
		});
		
		submitBtn.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			// do something for login invalid
			JOptionPane.showMessageDialog(panel,"Registration Successful");
			setVisible(false);
			new Login();
			}
	   });

		/*submitBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			// do something 
			JOptionPane.showMessageDialog(panel,"Username already exists or password doesnot match");
			new Registration();
			}
		});*/
		
		
	}
}


Java
class Userframe extends JFrame
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	Userframe()
	{
		final JFrame frame = new JFrame("WELCOME username ");
	    
		//lgnpanel.setVisible(false);
		JMenuBar menubar = new JMenuBar();
		JMenu savemenu = new JMenu("Save");
		JMenu retrievemenu = new JMenu("Retrieve");
		JMenu logoutmenu = new JMenu("LogOut");
		
		JMenuItem saveItem1 = new JMenuItem("Save your Passwords");
        
		JMenuItem retrieveItem1 = new JMenuItem("Retrieve your Passwords");
		 
	    JMenuItem logoutItem1 = new JMenuItem("LogOut of Project");
		  
		savemenu.add(saveItem1);
		retrievemenu.add(retrieveItem1);
		logoutmenu.add(logoutItem1);
		  
		menubar.add(savemenu);
		menubar.add(retrievemenu);
		menubar.add(logoutmenu);
		frame.setJMenuBar(menubar);
		frame.setSize(400,300);
		frame.setVisible(true);
		
		saveItem1.addActionListener(new ActionListener() {
		       public void actionPerformed(ActionEvent e) {
		       setVisible(false);
		       new Savingframe();
		      }
		  });
		
		retrieveItem1.addActionListener(new ActionListener() {
		       public void actionPerformed(ActionEvent e) {
		       setVisible(false);
		       new Retrievingframe();
		      }
		  });
		
		logoutItem1.addActionListener(new ActionListener() {
		       public void actionPerformed(ActionEvent e) {
		       setVisible(false);
		       new MainFrame();
		      }
		  });
	}
}



Java
class Savingframe extends JFrame
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	final JButton saveitbtn,addmorebtn;
	final JPanel panel;
	final JLabel usernameLabel,websiteLabel,passwordLabel,creditfieldlabel;
	final JTextField  websiteField,usernameField,creditfield;
	final JPasswordField passwordField;
	
	Savingframe()
	{
		super("Saving Password Frame");
		
		websiteLabel = new JLabel("Website:");
		websiteField = new JTextField(15);

		usernameLabel = new JLabel("Username:");
		usernameField = new JPasswordField(15);
		
		creditfieldlabel = new JLabel("Credit Card Number");
		creditfield = new JTextField(20);
		
		passwordLabel = new JLabel("Password:");
		passwordField = new JPasswordField(15);
		
		saveitbtn = new JButton("Save It");
		addmorebtn = new JButton("Add More");
		
		panel = new JPanel(new GridLayout(6,6));
		panel.add(websiteLabel);
		panel.add(websiteField);
		panel.add(usernameLabel);
		panel.add(usernameField);
		panel.add(passwordLabel);
		panel.add(passwordField);
		panel.add(addmorebtn);
		panel.add(saveitbtn);
		// to fill the GridLayout
		add(panel,BorderLayout.CENTER);

		setTitle("SAVING PASSWORD");
		setSize(400,350);
		setVisible(true);
		
		saveitbtn.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			// do something 
			JOptionPane.showMessageDialog(panel,"Saved Successfully");
			setVisible(false);
			new Userframe();
			}
		});
		
		/*saveitbtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			// do something 
			JOptionPane.showMessageDialog(panel,"Website or email-id already exists");
			new Savingframe();
			}
		});*/
	}
}


Java
class Retrievingframe extends JFrame
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	final JButton givepasswordbtn;
	final JPanel panel;
	final JLabel usernameLabel,websiteLabel;
	final JTextField  websiteField,usernameField;
	
	Retrievingframe()
	{
		super("Retreving Password Frame");
		
		websiteLabel = new JLabel("Website:");
		websiteField = new JTextField(15);

		usernameLabel = new JLabel("Username:");
		usernameField = new JPasswordField(15);
		
		givepasswordbtn = new JButton("Give Password");
						
		panel = new JPanel(new GridLayout(3,3));
		panel.add(websiteLabel);
		panel.add(websiteField);
		panel.add(usernameLabel);
		panel.add(usernameField);
		panel.add(givepasswordbtn);
		// to fill the GridLayout
		
		add(panel,BorderLayout.CENTER);
		
		setTitle("RETRIEVING PASSWORD");
		setSize(400,150);
		setVisible(true);
		
		givepasswordbtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			// do something 
			JOptionPane.showMessageDialog(panel,"Such details does not exists");
			setVisible(false);
			new Retrievingframe();
			}
		});
		
		/*givepasswordbtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			// do something 
			new Showpasswordframe();
			}
		});*/
	}
}


Java
class Showpasswordframe extends JFrame
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	final JButton continuebtn;
	final JPanel panel;
	final JLabel usernameLabel,websiteLabel,passwordLabel;
	final JTextField  websiteField,usernameField,passwordField;
	
	Showpasswordframe()
	{
		super("Show Password Frame");
		
		websiteLabel = new JLabel("Website:");
		websiteField = new JTextField(15);

		usernameLabel = new JLabel("Username:");
		usernameField = new JPasswordField(15);
		
		passwordLabel = new JLabel("Password:");
		passwordField = new JPasswordField(15);
		
		continuebtn = new JButton("Continue");
				
		panel = new JPanel(new GridLayout(4,3));
		panel.add(websiteLabel);
		panel.add(websiteField);
		panel.add(usernameLabel);
		panel.add(usernameField);
		panel.add(passwordLabel);
		panel.add(passwordField);
		panel.add(continuebtn);
		// to fill the GridLayout
		panel.add(new JLabel(""));
		add(panel,BorderLayout.CENTER);
		
		setTitle("SHOWING YOUR PASSWORD");
		setSize(400,200);
		setVisible(true);
		
		continuebtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			// do something
			setVisible(false); 
			new Userframe();
			}
		});
	}
}
Posted
Updated 25-Jan-12 0:01am
v2

1 solution

- customize the components
figure out what you need on all components (ID, text to display) and make it a basic implementation.

- set up worker classes beside the GUI
delegate the Action execution to the worker and clean the GUI to make it a only-graphical representation. When you need to tweak the GUI, you'll not have to touch the logical parts.

- set up interfaces for public access
limit the access

- set up interfaces for static String representations
use just those public available Strings to make sure you are using the right reference.

- thing about properties
add properties support to your application. Thing about making them (partly) available for the user. You can also add properties only for you to make certain settings (like showing or hiding GUI functions, colors, language, ....).


EDIT:
so let's try it again:

When I figured your half sentence explanation correct, then you are searching for a solution to have a common look on those JFrames codes.

You have extended the object JFrame a couple of times. Also don't you code OOP, this is procedural coding.
So break that up and extend an own abstract version of JFrame:

Java
public abstract class BasicFrame extends JFrame {
  //common variables
  // common constructor (with argument ID and Title String of login form)
  public BasicFrame(String strID, String strTitle){
    super(strTitle);
   // ...

  }
  
  public void createForm(/*arguments*/){ // common layout
     createName(/*args*/);
     createPassword(/*args*/);
     create ButtonBar(/**/);
  }

  public void createName(/*args*/){

  }
  public void createPassword(/*args*/){

  }
  public void create ButtonBar(/**/){

  }
}


to make sure, that every Frame implements the same methods that are needed, you could set up an interface that is referencing those methods. Also can you directly insert the ActionListener support, which makes it even more comfortable:

Java
public interface ILoginFrame extends ActionListener{

 public void createForm(/*args*/);
 public String getPassword();
 // more funny methods


}


then your implementations can base on that:

Java
class Showpasswordframe extends BasicFrame implements ILoginFrame{

  public Showpasswordframe(){
      super("ShowPassword", "Show Password Frame"); // (ID, title)
    createForm(/*args*/);
  }

  @override
  public createForm(/*args*/){
  // common look of LoginFrames
  }

  @override
  public String getPassword(){
    return null;
  }

// optional  - if needed you can also modify parts of the GUI by changing the basic implementation of the part:
  @ override
  public void create ButtonBar(/*args*/){
    // different Button bar
  }

  // and of course the actions:
  @override
  public void actionPerformed(ActionEvent oEvent) {
  if(null != getPassword()){
    // party!
  }
  //.... the other methods 
}


I hope this is what you were searching for. Otherwise we'll need 2-3 sentences in which you describe your problem more.
 
Share this answer
 
v4
Comments
Akshayy Tikekarr 25-Jan-12 11:15am    
Sorry but I have not understand any of what you told. I am just asking how can we code a frame to look like proper login or registration form as it looks in drag and drop IDE.
TorstenH. 26-Jan-12 1:44am    
you are not even writing complete sentences - so how am I supposed to know abut your GUI coding problem?

EDIT: I have updated the answer - I hope it fits now.
Akshayy Tikekarr 26-Jan-12 4:55am    
As you can see what I did is I made a Frame which has menu bar and then after you click on Login item another frame opens which is Login form but it do not give proper look as any Login form gives when it is develop using drag and drop applications (website login forms). I mean that as you extend frame the size of textfield increases but size of text does not. Similarly two labels and textfields are there but I want login button below it and forgot password button even below it..
Hope you got what I wanted..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900