简体   繁体   English

调用内部类jframe时,外部类jframe没有隐藏

[英]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. 当我调用内部jframe时,它被调用,但是外部jframe没有隐藏。 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. 首先,尼斯SSCCE,很多人都没有发布。

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 . 1, A扩展JFrame But the inner class B doesn't extend JFrame . 但是内部类B不扩展JFrame

2, In both A and B 's constructors, you invoke getContentPane() to get a ContentPane object. 2,在AB的构造函数中,都调用getContentPane()以获取ContentPane对象。 Since B doesn't extends JFrame and it is an inner class of A . 由于B不扩展JFrame ,因此它是A的内部类。 So actually A and B use the same ContentPane object to show something. 因此,实际上AB使用相同的ContentPane对象显示某些内容。

3, In A 's constructor, you have already added some components to it. 3,在A的构造函数中,您已经添加了一些组件。 And then in B 's constructor, you add a JLabel to the same ContentPane object. 然后在B的构造函数中,将JLabel添加到同一ContentPane对象。 So all these components will be showed. 因此将显示所有这些组件。 It is not because the frame in A is not hiding. 这不是因为A的框架没有隐藏。 It is because the same ContentPane object is showed again. 这是因为再次显示了相同的ContentPane对象。

Solution: you can make B extends JFrame . 解决方案:您可以使B扩展JFrame

In A 's constructor, line 26: A的构造函数中,第26行:

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

In B 's constructor, line line 74: B的构造函数中,第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. PS在您的代码中,您将始终创建一个新的JFrame对象并使用它。 There is no point for A or B to extends JFrame . AB没有必要扩展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 . 1,在类A ,不要创建新的JFrame对象,只需使用A ,因为它也是JFrame

2, In class B , use cp = frm.getContentPane(); 2,在类B ,使用cp = frm.getContentPane(); to get ContentPane from the JFrame you are actually using. 从实际使用的JFrame获取ContentPane

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. 在A类中,您具有Container cp,在B类中也具有。但是您的程序始终引用A类的Container cp。 so before you create object for class B ( new B() ) you must remove the all component of Container cp. 因此,在为类B( new B() )创建对象之前,必须删除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();


    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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