当我调用内部 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();
}
}