Results 1 to 2 of 2

Thread: Log in stress

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2003
    Posts
    67

    Exclamation Log in stress

    I am fairly new to java and I dont really know what I am doing so if someone could help me it would b great.

    I am coding a login screen, where the user enters a name and password and when they press "Ok" I want a welcome message to appear. I tried this code below but keep getting 1 error.
    Please help me!!

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

    public class PassWordInterface extends JFrame implements ActionListener{



    private JLabel nameLabel, passWordLabel, EMCLabel;
    private JTextField nameTextField, passwordTextField;
    private JButton okButton, CancelButton;
    private FlowLayout layout;



    public PassWordInterface(){

    super("Enter Password");
    setBounds(200, 200, 350, 200);
    GridBagLayout layout = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();
    JPanel pane = new JPanel();
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pane.setLayout(layout);
    ImageIcon EMC = new ImageIcon("EMC.gif");

    createConstraints(constraints, 0, 0, 4, 1, 0, 0);
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.anchor = GridBagConstraints.CENTER;
    JLabel EMCLabel = new JLabel(EMC);
    layout.setConstraints(EMCLabel, constraints);
    pane.add(EMCLabel);

    createConstraints(constraints, 0, 1, 1, 1, 50, 20);
    constraints.fill = GridBagConstraints.NONE;
    constraints.anchor = GridBagConstraints.EAST;
    JLabel nameLabel = new JLabel ("Name:", JLabel.LEFT);
    layout.setConstraints(nameLabel, constraints);
    pane.add(nameLabel);


    createConstraints(constraints, 1, 1, 2, 1, 50, 20);
    constraints.fill = GridBagConstraints.HORIZONTAL;
    JTextField nameTextField = new JTextField();
    layout.setConstraints(nameTextField, constraints);
    pane.add(nameTextField);

    createConstraints(constraints, 0, 2, 1, 1, 50, 20);
    constraints.fill = GridBagConstraints.NONE;
    constraints.anchor = GridBagConstraints.EAST;
    JLabel passWordLabel = new JLabel ("Password:", JLabel.LEFT);
    layout.setConstraints(passWordLabel, constraints);
    pane.add(passWordLabel);


    createConstraints(constraints, 1, 2, 2, 1, 50, 20);
    constraints.fill = GridBagConstraints.HORIZONTAL;
    JPasswordField passwordTextField = new JPasswordField();
    passwordTextField.setEchoChar('*');
    layout.setConstraints(passwordTextField, constraints);
    pane.add(passwordTextField);


    createConstraints(constraints, 0, 3, 1, 1, 30, 40);
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.anchor = GridBagConstraints.CENTER;
    JButton okButton = new JButton ("OK");
    layout.setConstraints(okButton, constraints);
    pane.add(okButton);
    okButton.addActionListener(this);

    createConstraints(constraints, 2, 3, 1, 4, 40, 40);
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.anchor = GridBagConstraints.CENTER;
    JButton CancelButton = new JButton ("Cancel");
    layout.setConstraints(CancelButton, constraints);
    pane.add(CancelButton);
    CancelButton.addActionListener(this);


    setContentPane(pane);
    }


    private void createConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy){

    gbc.gridx = gx;
    gbc.gridy = gy;
    gbc.gridwidth = gw;
    gbc.gridheight = gh;
    gbc.weightx = wx;
    gbc.weighty = wy;

    }

    public static void main (String args[]){

    PassWordInterface test = new PassWordInterface();
    test.show();
    }


    public void ActionPerformed(ActionEvent e)
    {
    if (e.getSource()==okButton)
    {
    JOptionPane.showMessageDialog(null, "Welcome "+ nameTextField.getText());

    }

    if (e.getSource()==CancelButton)
    {
    nameTextField.setText("");
    passwordTextField.setText("");
    }
    }

    }

  2. #2
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724
    While I do appreciate you're new to Java, I think you should have a look at the coding conventions on java's web-site (no offense intended).

    The corrections have been commented .
    Code:
    import java.awt.*; 
    import java.awt.event.*; 
    import javax.swing.*; 
    
    public class PassWordInterface extends JFrame implements ActionListener { 
    	private JLabel nameLabel, passWordLabel, EMCLabel; 
    	private JTextField nameTextField;
    	
    	// 1 - you had this declared as a JTextField.
    	private JPasswordField passwordTextField;
    	private JButton okButton, CancelButton; 
    	private FlowLayout layout; 
    
    	public PassWordInterface() { 
    		super("Enter Password"); 
    		setBounds(200, 200, 350, 200); 
    		GridBagLayout layout = new GridBagLayout(); 
    		GridBagConstraints constraints = new GridBagConstraints(); 
    		
    		JPanel pane = new JPanel(); 
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    		pane.setLayout(layout); 
    
    		ImageIcon EMC = new ImageIcon("EMC.gif"); 
    		createConstraints(constraints, 0, 0, 4, 1, 0, 0); 
    		constraints.fill = GridBagConstraints.HORIZONTAL; 
    		constraints.anchor = GridBagConstraints.CENTER; 
    
    		JLabel EMCLabel = new JLabel(EMC); 
    		layout.setConstraints(EMCLabel, constraints); 
    		pane.add(EMCLabel); 
    		createConstraints(constraints, 0, 1, 1, 1, 50, 20); 
    		constraints.fill = GridBagConstraints.NONE; 
    		constraints.anchor = GridBagConstraints.EAST; 
    		
    		/* 
    			2 - You're redeclared this, and as it's in a different
    			scope, you're modular variable isn't initialised.
    		*/
    		nameLabel = new JLabel ("Name:", JLabel.LEFT); 
    		layout.setConstraints(nameLabel, constraints); 
    		pane.add(nameLabel); 
    		createConstraints(constraints, 1, 1, 2, 1, 50, 20); 
    		constraints.fill = GridBagConstraints.HORIZONTAL; 
    		
    		// 3 - Ditto (2).
    		nameTextField = new JTextField(); 
    		layout.setConstraints(nameTextField, constraints); 
    		pane.add(nameTextField); 
    		createConstraints(constraints, 0, 2, 1, 1, 50, 20); 
    		constraints.fill = GridBagConstraints.NONE; 
    		constraints.anchor = GridBagConstraints.EAST; 
    		
    		// 4 - Ditto (2).
    		passWordLabel = new JLabel ("Password:", JLabel.LEFT); 
    		layout.setConstraints(passWordLabel, constraints); 
    		pane.add(passWordLabel); 
    		createConstraints(constraints, 1, 2, 2, 1, 50, 20); 
    		constraints.fill = GridBagConstraints.HORIZONTAL; 
    
    		// 5 - Ditto (2).
    		passwordTextField = new JPasswordField(); 
    		passwordTextField.setEchoChar('*'); 
    		layout.setConstraints(passwordTextField, constraints); 
    		pane.add(passwordTextField); 
    		createConstraints(constraints, 0, 3, 1, 1, 30, 40); 
    		constraints.fill = GridBagConstraints.HORIZONTAL; 
    		constraints.anchor = GridBagConstraints.CENTER; 
    		
    		// 6 - Ditto (2).
    		okButton = new JButton ("OK"); 
    		layout.setConstraints(okButton, constraints); 
    		pane.add(okButton); 
    		okButton.addActionListener(this); 
    		createConstraints(constraints, 2, 3, 1, 4, 40, 40); 
    		constraints.fill = GridBagConstraints.HORIZONTAL; 
    		constraints.anchor = GridBagConstraints.CENTER; 
    		
    		// 7 - Ditto (2).
    		CancelButton = new JButton ("Cancel"); 
    		layout.setConstraints(CancelButton, constraints); 
    		pane.add(CancelButton); 
    		CancelButton.addActionListener(this); 
    		setContentPane(pane); 
    	} 
    	
    	private void createConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy) { 
    		gbc.gridx = gx; 
    		gbc.gridy = gy; 
    		gbc.gridwidth = gw; 
    		gbc.gridheight = gh; 
    		gbc.weightx = wx; 
    		gbc.weighty = wy; 
    	} 
    	
    	public static void main (String args[]) { 
    		PassWordInterface test = new PassWordInterface(); 
    		test.show(); 
    	} 
    	
    	// 8 - You had ActionPerformed (case sensitive).
    	public void actionPerformed(ActionEvent e) { 
    		// 9 - Rather use the equals() method - a tad cleaner.
    		if (e.getSource().equals(okButton))
    		{ 
    			JOptionPane.showMessageDialog(null, "Welcome "+ nameTextField.getText()); 
    		} 
    		// Better to use Else If & ditto (9).
    		else if (e.getSource().equals(CancelButton))
    		{ 
    			nameTextField.setText(""); 
    			passwordTextField.setText(""); 
    		} 
    	} 
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width