简体   繁体   中英

Outer class jframe is not hiding when calling the inner class jframe

When i am calling the inner jframe, it is called, but outer jframe is not hiding. instead it gets overlapped. so what will be the solution for this. Is there any way to get out of this. As i tried when i am calling the inner class frame, the outer class frame is also called, and it is not hidden.

package com.exp.example;

import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;


@SuppressWarnings("serial")
public class A extends JFrame implements ActionListener {
    JFrame rframe = new JFrame();
    JLabel CFirstName;
    JTextField Cfname;
    JButton jbsubmit;
    Container cp;

    public A() {

        rframe.setSize(500, 200);
        rframe.setLocationRelativeTo(null);
        cp = getContentPane();
        cp.setLayout(null);
        setSize(550, 300);
        rframe.setTitle("Outer Frame");
        cp.setBackground(new Color(140, 180, 180));

        CFirstName = new JLabel("First Name");
        Cfname = new JTextField(10);
        jbsubmit = new JButton("PREVIEW");

        CFirstName.setBounds(10, 20, 100, 35);
        Cfname.setBounds(150, 20, 150, 25);
        jbsubmit.setBounds(190, 110, 92, 25);
        cp.add(CFirstName);
        cp.add(Cfname);
        cp.add(jbsubmit);

        jbsubmit.addActionListener(this);

        rframe.add(cp);
        rframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        rframe.setVisible(true);
    }

    public void actionPerformed(ActionEvent ae) {
        String action = ae.getActionCommand();


        if (action == "PREVIEW") {
            /* Write the code here
             * When we click on preview button the frame of outer class(class A) gets
             * deactivated(closed) and inner frame, frame of inner class(class B) gets visible.
             * it should not be overlapped.  
             */
            /* My Code */
            new B();
            rframe.setVisible(false);

        }
    }

    public class B {
        JFrame frm = new JFrame();
        Container cp;

        public B() {
            frm.setSize(500, 200);
            frm.setLocationRelativeTo(null);
            cp = getContentPane();
            cp.setLayout(null);
            setSize(550, 300);
            frm.setTitle("Inner Frame");
            cp.setBackground(new Color(140, 180, 180));

            JLabel cpn = new JLabel("hello");
            cpn.setBounds(10, 20, 100, 35);
            cp.add(cpn);

            frm.add(cp);
            frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frm.setVisible(true);
        }
    }

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

First, Nice SSCCE, many people don't post one.

Second, i think your labels are overlapping, try:

if(action.equals("PREVIEW"))
    {
    CFirstName.setText("");
    new B();
    rframe.setVisible(false);

    }

Good Luck!

1, A extends JFrame . But the inner class B doesn't extend JFrame .

2, In both A and B 's constructors, you invoke getContentPane() to get a ContentPane object. Since B doesn't extends JFrame and it is an inner class of A . So actually A and B use the same ContentPane object to show something.

3, In A 's constructor, you have already added some components to it. And then in B 's constructor, you add a JLabel to the same ContentPane object. So all these components will be showed. It is not because the frame in A is not hiding. It is because the same ContentPane object is showed again.

Solution: you can make B extends JFrame .

In A 's constructor, line 26:

rframe.setSize(500, 200);
rframe.setLocationRelativeTo(null);
cp = getContentPane();
cp.setLayout(null);
setSize(550, 300);

In B 's constructor, line line 74:

frm.setSize(500, 200);
frm.setLocationRelativeTo(null);
cp = getContentPane();
cp.setLayout(null);
setSize(550, 300);

PS In your code, you will always create a new JFrame object and use it. There is no point for A or B to extends JFrame . Maybe it is better if you do the following changes.

1, In class A , do not create a new JFrame object, just use A , since it is also a JFrame .

2, In class B , use cp = frm.getContentPane(); to get ContentPane from the JFrame you are actually using.

you have mistake in you Container. In class A you have Container cp and also in class B. But your program always refer class A's Container cp. so before you create object for class B ( new B() ) you must remove the all component of Container cp. then you will not get overlapped.

public void actionPerformed(ActionEvent ae) {
    String action = ae.getActionCommand();


    if (action == "PREVIEW") {
        /* Write the code here
         * When we click on preview button the frame of outer class(class A) gets
         * deactivated(closed) and inner frame, frame of inner class(class B) gets visible.
         * it should not be overlapped.  
         */
        /* My Code */
        cp.removeAll();
        rframe.setVisible(false);
        new B();


    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM