Need help on setting the values of username and password for my log-in menu:

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


class Profile extends JFrame
{
	private JPanel p,p1,p2,p3;
	private JTextField tf;
	private JPasswordField pf;
	private JLabel l1,l2;
	private JButton b;
	
	Profile()
	{
		p = new JPanel();
		p1 = new JPanel();
		p2 = new JPanel();
		p3 = new JPanel();
		tf = new JTextField(10);
		pf = new JPasswordField(10);
		l1 = new JLabel("Enter username: ");
		l2 = new JLabel("Enter password: ");
		b = new JButton("Log-in");
		
		getContentPane().setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));
		add(p);
		p.add(p1,BorderLayout.CENTER);
		p.add(p2,BorderLayout.CENTER);
		p.add(p3,BorderLayout.CENTER);
		p1.add(l1);
		p1.add(tf);
		p2.add(l2);
		p2.add(pf);
		p3.add(b);
		
		
	}
}


public class TestProject {
    
    public static void main(String[] args) {
   	Profile p = new Profile();
   	p.setBounds(200,200,270,165);
   	p.setVisible(true);
   	p.setResizable(false);
   	p.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	
    
    
    }
}

and... is it possible that when I clicked the log-in button this will appear:

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

class Profile extends JFrame
{
	private JPanel p,p1,p2,p3,p4,p5;
	private JLabel l1,l2,l3,l4;
	private JTextField tf1,tf2,tf3,tf4;
	private JButton b1,b2,b3;
	private JTextArea ta;
	
	Profile()
	{
		p = new JPanel();
		p.setLayout(new BoxLayout(p,BoxLayout.Y_AXIS));
		p1 = new JPanel();
		p1.setLayout(new GridLayout(2,2));
		p2 = new JPanel();
		p2.setLayout(new BorderLayout());
		p3 = new JPanel();
		p4 = new JPanel();
		p4.setLayout(new BorderLayout());
		p5 = new JPanel();
		
		l1 = new JLabel("Name: ");
		l2 = new JLabel("CWA: ");
		l3 = new JLabel("Course/Year: ");
		l4 = new JLabel("ID number: ");
		
		tf1 = new JTextField(17);
		tf2 = new JTextField(17);
		tf3 = new JTextField(17);
		tf4 = new JTextField(17);
		
		b1 = new JButton("       OK       ");
		b2 = new JButton("     ADD     ");
		b3 = new JButton("  DELETE  ");
		
		String x = "";
		ta = new JTextArea(x,30,72);
		ta.setEditable(false);
		
		add(p);
		p.add(p1);
		p.add(p2);
		p.add(p3);
		p.add(p4);
		p1.add(l1);
		p1.add(tf1);
		p1.add(l2);
		p1.add(tf2);
		p1.add(l3);
		p1.add(tf3);
		p1.add(l4);
		p1.add(tf4);
		p2.add(b1,BorderLayout.EAST);
		p3.add(ta);
		p4.add(p5,BorderLayout.EAST);
		p5.add(b2);
		p5.add(b3);
		
		
	}
	
}

public class TestProject1 {
    
    public static void main(String[] args) {
    Profile p = new Profile();
    p.setBounds(270,100,800,600);
    p.setVisible(true);
    p.setResizable(false);
    p.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

I'm just new to java and only know how to code for the GUI :)

Thanks in advance :)

Recommended Answers

All 10 Replies

Here a few hints:
You have two classes, both called Profile, so the first thing is to rename one of them - eg change the first one to Logon.
Move all the code (except the first line) in the main methods into the appropriate constructors so that you can just call new Profile() or new Logon()and that's enough to make the complete window appear.
Add an ActionListener (Google it) to the first screen's button - that's a method which is called when the button is clicked. In that method call new Profile() to get the second screen to appear.
Finally, you;ll want some way to pass the user name from the logon screen to the profile screen, eg add it as a parameter to the profile screen's constructor.

Sir, what do you mean on moving all the codes (except the first line) in the main methods?

p.setBounds(270,100,800,600);
p.setVisible(true);
p.setResizable(false);
p.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

all belong in the constructor, so the main method just says

public static void main(String[] args) {
  Profile p = new Profile();
}

Then any other class can also just call that constructor to get the window to appear.

Can you give me an example on what action listener to use that when I click the log-in button on the first frame will show the second frame.

I'm clueless.

If my button of the login frame is "b" then how can i code it so it will show my next frame.

Let's just say that when I click the button it will show directly the second frame regardless of the username and password.

All I know is just for the text, like: text.append(x + "\n");

and for the button that will show new Frame... I really have no idea. :(

Did you move the code like I said?
Did you study the tutorial whose link I gave you?

The way DaniWeb works is that we will help you to learn what you need to know to solve your problem. If all you want is the answer without putting in any work yourself, then this is the wrong place for you.

Read the tutorial. All the code you need is either in the tutorial or already in your program. You can do this.

An idea just pop out in my mid :D

I'll set the visible of the second frame to false, but when I click the button on my first frame it makes the visible value to true.

Is that possible? if it is, how can i do that?

Yes, its possible to do it that way if you prefer.
"How can I do that?" - I've already told you: ActionListener. I'm not going to answer any more questions until you have studied the tutorial link I gave you.

Thanks, I'm currently reading it now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.