繁体   English   中英

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

[英]Outer class jframe is not hiding when calling the inner class jframe

当我调用内部jframe时,它被调用,但是外部jframe没有隐藏。 相反,它会重叠。 那么这将是解决方案。 有什么办法摆脱这种情况。 正如我在调用内部类框架时所尝试的那样,外部类框架也被称为,并且它不是隐藏的。

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();
    }
}

首先,尼斯SSCCE,很多人都没有发布。

其次,我认为您的标签重叠,请尝试:

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

    }

祝好运!

1, A扩展JFrame 但是内部类B不扩展JFrame

2,在AB的构造函数中,都调用getContentPane()以获取ContentPane对象。 由于B不扩展JFrame ,因此它是A的内部类。 因此,实际上AB使用相同的ContentPane对象显示某些内容。

3,在A的构造函数中,您已经添加了一些组件。 然后在B的构造函数中,将JLabel添加到同一ContentPane对象。 因此将显示所有这些组件。 这不是因为A的框架没有隐藏。 这是因为再次显示了相同的ContentPane对象。

解决方案:您可以使B扩展JFrame

A的构造函数中,第26行:

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

B的构造函数中,第74行:

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

PS在您的代码中,您将始终创建一个新的JFrame对象并使用它。 AB没有必要扩展JFrame 如果进行以下更改,可能会更好。

1,在类A ,不要创建新的JFrame对象,只需使用A ,因为它也是JFrame

2,在类B ,使用cp = frm.getContentPane(); 从实际使用的JFrame获取ContentPane

您的容器中有错误。 在A类中,您具有Container cp,在B类中也具有。但是您的程序始终引用A类的Container cp。 因此,在为类B( new B() )创建对象之前,必须删除Container cp的所有组件。 这样您就不会重叠。

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