Hi, can someone help me? I have two Jframes: Frame and MainFrame.
My problem is they both show at the same time.
If the login is correct MainFrame opens and Frame closes.

import javax.swing.*;

public class Main extends JFrame{
	public static void main(String args[]) {

//Frame Window /Login Window
	Frame fram = new Frame();
	fram.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	fram.setSize(200,150);
	fram.setVisible(true);
	fram.setResizable(false);

//MainFrame Window
	MainFrame seal = new MainFrame();
	seal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	seal.setSize(200,150);
	seal.setVisible(true);
	seal.setResizable(true);

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

public class Frame extends JFrame{

private JButton login;
private JTextField user;
private JPasswordField pass;

	public Frame(){
	super("Login"); // title
	setLayout(new FlowLayout());

	user = new JTextField(10);
	add(user);
	pass = new JPasswordField(10);
	add(pass);
	login = new JButton("Login");
	add(login);
	maria louise = new maria();
	login.addActionListener(louise);
}

public class maria implements ActionListener{
	public void actionPerformed(ActionEvent e) {

	if(user.getText().trim().length()== 0 || pass.getText().length()==0)
	JOptionPane.showMessageDialog(null, "Please fill out both of the textbox");

	else if(user.getText().equals("12345") && pass.getText().equals("12345"))
	JOptionPane.showMessageDialog(null, " WELCOME");	

	else
	JOptionPane.showMessageDialog(null,"Wrong Username or Password");

}}

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

public class MainFrame extends JFrame{

	private JLabel item1;
	private JTextField item2, item3;

		public MainFrame(){
		super("Main Window");
		setLayout(new FlowLayout());

		item1 = new JLabel("Welcome");
		item1.setToolTipText("Hello");
		add(item1);
	
		item2 = new JTextField(15);
		add(item2);
	
		item3 = new JTextField(15);
		add(item3);

	}}

you declare two frame in the main method.For this reason these two open at a time.
Delete this....

//MainFrame Window
	MainFrame seal = new MainFrame();
	seal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	seal.setSize(200,150);
	seal.setVisible(true);
	seal.setResizable(true);

I think now you can solve the problem.....

Class Main lines 10, 17 you make them both visible. Don't make mainframe window visible until after logon.

How do I do it?

sealxlion
JamesCherrill's reply and my reply is same.
Delete code for mainframe from the main method that I have said before.

Unrelated to your question, but a general advice: do not do this

public class Frame extends JFrame{

JFrame itself extends java.awt.Frame . You're just asking for confusion and possible bugs by naming your class Frame.

Comments
Good post.
very good point

@Ezzaral very good point +1

@sealxlion don't mixing Java AWT Frame with Swing JFrame, that really road to the hell,

for todays GUI is required --> all Components must starts with Char J

then Frame fram = new Frame(); should be JFrame fram = new JFrame();

and as @Ezzaral suggested correctly, this is twice definitions

Edited 5 Years Ago by mKorbel: n/a

This article has been dead for over six months. Start a new discussion instead.