-2

I have 2 JFrames f1 and f2 both having buttons b1 and b2 respectively.

The buttons b1 and b2 switch frames, i.e. if b1 is clicked it opens f2 and if b2 is clicked it opens f1.

I want my program to close previous JFrame when attempting to open a new JFrame, i.e. if b1 is clicked it should close/hide f1 and open f2 and vice versa.

I have tried setVisible(false) but it doesn't seem to work.

I'd appreciate any help or suggestions.

Here is my code:

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

public class m extends JFrame implements ActionListener
{
    static JFrame f1,f2;
    static JButton b1,b2;

    public m()
    {
        f1();
    }

    public void f1()
    {
        JFrame f1=new JFrame("frame 1");
        JButton b1=new JButton("frame 2");

        JLabel l1=new JLabel("FRAME 1");

        f1.setSize(600,600);
        b1.setBounds(300,300,100,100);
        l1.setBounds(300,150,100,100);
        b1.addActionListener(this);
        f1.add(b1);
        f1.add(l1);
        f1.setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public void f2()
    {
        JFrame f2=new JFrame("frame 2");
        JButton b2=new JButton("frame 1");
        JLabel l2=new JLabel("FRAME 2");
        f2.setSize(600,600);
        b2.setBounds(300,300,100,100);
        l2.setBounds(300,150,100,100);
        b2.addActionListener(this);
        f2.add(b2);
        f2.add(l2);
        f2.setVisible(true);

        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    public static void main(String args[])
    {
        new m();
    }

    public void actionPerformed(ActionEvent e)
    {
        String bt=String.valueOf(e.getActionCommand());
        if(bt=="frame 2")
        {
            f1.setVisible(false);
            f2();
        }
        else if(bt=="frame 1")
        {
            f2.setVisible(false);
            f1();
        }
    }
}
Frakcool
  • 10,915
  • 9
  • 50
  • 89
G k
  • 3
  • 1
  • 1
    Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Frakcool Dec 05 '16 at 19:23
  • @Mdlc when you approve suggested edits please be sure there's nothing else to improve, on the edit of Doron there was still things to improve such as the code format! – Frakcool Dec 05 '16 at 19:26
  • @DoronYakovlevGolani please fix code format too, when you suggest an edit improve everything you can in a single edit :) – Frakcool Dec 05 '16 at 19:27

1 Answers1

4

There are multiple errors in your code:

  1. Indenting, you should indent it correctly so it's easier to read

  2. You're setting bounds to the components, instead use a proper Layout manager, see: Null layout is evil and Why is it frowned upon to use null layout in swing?, while using null layout and setting bound might seem like the best and easiest way to create complex GUIs, it's not that way, you'll find yourself in troubles in the future due to this, because Swing has to work in different screen sizes, resolutions and PLAFs.

  3. You're extending JFrame and creating a JFrame object, and you're mixing them, leave one of them. I suggest the latter because otherwise when you're extending, you're saying your class is a JFrame, if you need to extend something, extend from JPanel instead.

  4. Don't use multiple JFrames, see: The use of multiple JFrames, good/bad practice? (BAD), you might want to try using JDialogs or Card Layout

  5. You're comparing Strings with == and not with .equals()! See: How do I compare Strings in Java?

  6. You have static members that you're never using and JFrame and JButton shouldn't be static actually!

  7. This line: setDefaultCloseOperation(DISPOSE_ON_CLOSE); and this one setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); are not working on the f2 and f1 frames, but on the frame that is generated by your extends

  8. You're not placing your program on the EDT

In just a simple program you have all those errors! So I suggest you to go back to the Java basics, before you go with GUIs which make the programs more complex and harder to learn the basics.

Now having said the above errors if you insist in having 2 JFrames:

//Create both frames

JFrame frame1 = new JFrame("Frame1");
JFrame frame2 = new JFrame("Frame2");

//Add your buttons using a proper layout manager as suggested above
...
//Later in your code in your actionPerformed:
if (e.getSource.equals(b1)) {
    //To close the frame w/o exiting application
    frame1.dispose();
    frame2 = new JFrame("Frame2"); //Or call the method that creates this JFrame

    //To toggle frame visibility and not closing it
    frame1.setVisible(false);
    frame2.setVisible(true);
} else if (e.getSource.equals(b2)) {
    //To close the frame w/o exiting application
    frame2.dispose();
    frame1 = new JFrame("Frame2"); //Or call the method that creates this JFrame

    //To toggle frame visibility and not closing it
    frame2.setVisible(false);
    frame1.setVisible(true);
}

That's the main idea, for reference, check this answer and the links and duplicate question there too

Community
  • 1
  • 1
Frakcool
  • 10,915
  • 9
  • 50
  • 89