hi, I was wondering how can i make a new frame after the password is entered(if it's correct).
Like if the user enters the correct password than another brower/frame will open.


This is the program:

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

public class password { 
private static String password = "pass";
JFrame frame; 
JPanel contentPane; 
JLabel label; 
JButton button; 
JTextField name; 

 
    public static void main(String[] args) { 
    JFrame frame = new JFrame("Password"); 
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(100, 100); 
    
    JLabel label = new JLabel("Enter password"); 
    JPanel panel = new JPanel(); 
    frame.add(panel); 
    JPasswordField pass = new JPasswordField(10); 
    pass.setEchoChar('*'); 
    pass.addActionListener(new AL()); 
  panel.add(label, BorderLayout.WEST); 
  panel.add(pass, BorderLayout.EAST); 
  } 
  
  static class AL implements ActionListener{ 
    public void actionPerformed(ActionEvent e) { 
      JPasswordField input = (JPasswordField) e.getSource(); 
      char[] passy = input.getPassword(); 
      String p = new String(passy); 
      
      if (p.equals(password)) { 
        JOptionPane.showMessageDialog(null, "Correct"); 
        password frame = new password(); 
      } 
      else 
        JOptionPane.showMessageDialog(null, "Incorrect"); 
    } 
  } 
}

Recommended Answers

All 5 Replies

All you need to do is create another JFrame and set it visible

see thread post by you named...
GUI - creating buttons with action

take that example..and try it your way..

if it is fine then please make it as resolved...

Yea I saw his post, and i looked at the codes but it was kind of a "different" way I did mine. But I figured out how to make another frame.. but then how do i make the previous frame exit by itself?

itself mean what...

without force to dispose..hoe van the current frame know this to dispose itself..

you can close frame with this

frame.dispose();

its better that you locate

frame.setVisible(true);

in last line, such az this in your code

public static void main(String[] args) {

JFrame frame = new JFrame("Password");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(100, 100);

frame.setVisible(true);
 
}

and locate this in constructor

JLabel label = new JLabel("Enter password");

JPanel panel = new JPanel();

frame.add(panel);

JPasswordField pass = new JPasswordField(10);

pass.setEchoChar('*');

pass.addActionListener(new AL());

panel.add(label, BorderLayout.WEST);

panel.add(pass, BorderLayout.EAST);
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.