0

I am a beginner to java and stuck with the below issue. The purpose is to display the login window when log out button is pressed.

First JFrame window displayed is "Plain" with 2 fields username and password(I will be adding the log in functionality later)

When I press the submit button the JFrame "NEw Window" is displayed with the button "LOGOUT"

What I would like to do is that when the "LOGOUT" is pressed the "NEw Window" should close and the "Plain" window should open.

Present issue: When "LOGOUT" button is pressed the "NEw Window" is opening up.

Please correct the code so that I get the desired functionality

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

class Test implements ActionListener{
    JButton submit;
    JFrame j;
    JFrame jf;

    public Test()
    {
    j = new JFrame("PLAIN");
    j.setBounds(500,150,300,400);
    j.setVisible(true);
    j.setDefaultCloseOperation(j.EXIT_ON_CLOSE);
    JPanel panel = new JPanel();
    j.add(panel);
    panel.setSize(50, 50);
    panel.setLayout(null);

    JLabel label = new JLabel("User Name");
    label.setSize(10,10);
    label.setBounds(100, 30, 400, 30);
    panel.add(label);

    JTextField username = new JTextField(10);
    username.setSize(10,10);
    username.setBounds(300, 30, 400, 30);
    panel.add(username);

    JLabel password= new JLabel("Password");
    password.setBounds(100, 90, 400, 30);
    panel.add(password);

    JPasswordField pass = new JPasswordField(10);
    pass.setBounds(300, 90, 400, 30);
    panel.add(pass);

    submit = new JButton("Submit");
    submit.setSize(10, 10);
    submit.setBounds(300, 160, 200, 40);
    panel.add(submit);

    submit.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

        j.setVisible(false);
        jf = new JFrame("NEw Window");
        jf.setVisible(true);
        jf.setBounds(500,150,300,400);
        JPanel panel2 = new JPanel();
        panel2.setLayout(null);
        jf.add(panel2);

        JButton logout = new JButton("LOGOUT");
        logout.setBounds(100, 30, 400, 30);
        panel2.add(logout);
        logout.addActionListener(this);
        jf.setDefaultCloseOperation(j.EXIT_ON_CLOSE);

    }

    public void actionPerformed1(ActionEvent e1) {

        jf.dispose();
        j.setVisible(true);


    }

public static void main(String args[])
{

    new Test();

}

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Memory bot
  • 45
  • 2
  • 11
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Jun 09 '14 at 00:00
  • Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead [use layout managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html), or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Jun 09 '14 at 00:01

2 Answers2

1

Some Points:

  1. Call JFrame#setVisible() in the end after adding all the components.

  2. Never use null layout at all instead use proper Layout Manager.

    Read more A Visual Guide to Layout Managers where all the layout manger are described in detail along with sample code.

  3. Use SwingUtilities.invokeLater() to make sure that EDT is initialized properly.

    Read more

Try with WindowConstants.DISPOSE_ON_CLOSE instead of JFrame.EXIT_ON_CLOSE.

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
0

Just put j.setVisible(true) from Test() at the end, after adding all the components.

If you want to make a new form, don't do it in the same class where you already have one because it isn`t right. Read the book named Clean code. You also have some errors in your code and useless.

  • As I told above that i am beginner to Java and not an expert. So please don't call my code as useless. Remember The Expert in anything was once a beginner. – Memory bot Jun 20 '14 at 05:05
  • Hei, I`m sorry if I offended you, that was not the idea. I said that you wrote some useless code, it`s just an observation. You have to learn from this kind of observations. Obviously, nobody is an expert in the beginning. – Chirila Marc-Mihai Jun 20 '14 at 09:20