Java 调用内部类jframe时,外部类jframe没有隐藏
当我调用内部jframe时,它被调用,但是外部jframe没有隐藏。相反,它是重叠的。那么,解决这个问题的办法是什么呢。 有没有办法摆脱这一切。正如我在调用内部类框架时尝试的那样,外部类框架也被调用,并且它不是隐藏的Java 调用内部类jframe时,外部类jframe没有隐藏,java,swing,jframe,multiple-instances,null-layout-manager,Java,Swing,Jframe,Multiple Instances,Null Layout Manager,当我调用内部jframe时,它被调用,但是外部jframe没有隐藏。相反,它是重叠的。那么,解决这个问题的办法是什么呢。 有没有办法摆脱这一切。正如我在调用内部类框架时尝试的那样,外部类框架也被调用,并且它不是隐藏的 package com.exp.example; import java.awt.Color; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionLi
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);
}
祝你好运 首先,很好,很多人都不发帖子 其次,我认为您的标签重叠,请尝试:
if(action.equals("PREVIEW"))
{
CFirstName.setText("");
new B();
rframe.setVisible(false);
}
祝你好运 1,
A
扩展了JFrame
。但是内部类B
没有扩展JFrame
2,在A
和B
的构造函数中,调用getContentPane()
以获取ContentPane
对象。因为B
不扩展JFrame
,它是A
的一个内部类。所以实际上A
和B
使用相同的ContentPane
对象来显示某些内容
3、在的构造函数中,您已经向其添加了一些组件。然后在B
的构造函数中,向同一ContentPane
对象添加JLabel
。因此,所有这些组件都将显示出来。这不是因为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);
另外,在代码中,您将始终创建一个新的JFrame
对象并使用它。A
或B
没有必要扩展JFrame
。如果您做以下更改,可能会更好
1,在类A
中,不要创建新的JFrame
对象,只需使用A
,因为它也是JFrame
2,在类B
中,使用cp=frm.getContentPane()
从实际使用的JFrame
获取ContentPane
。1,A
扩展JFrame
。但是内部类B
没有扩展JFrame
2,在A
和B
的构造函数中,调用getContentPane()
以获取ContentPane
对象。因为B
不扩展JFrame
,它是A
的一个内部类。所以实际上A
和B
使用相同的ContentPane
对象来显示某些内容
3、在的构造函数中,您已经向其添加了一些组件。然后在B
的构造函数中,向同一ContentPane
对象添加JLabel
。因此,所有这些组件都将显示出来。这不是因为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);
另外,在代码中,您将始终创建一个新的JFrame
对象并使用它。A
或B
没有必要扩展JFrame
。如果您做以下更改,可能会更好
1,在类A
中,不要创建新的JFrame
对象,只需使用A
,因为它也是JFrame
2,在类B
中,使用cp=frm.getContentPane()
从实际使用的JFrame
获取ContentPane
。您的容器中有错误。
在A类中有容器cp,在B类中也有容器cp。
但是你的程序总是引用类A的容器cp。
因此,在为类B(new B())创建对象之前,必须删除容器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();
}
}
你的容器有错误。
在A类中有容器cp,在B类中也有容器cp。
但是你的程序总是引用类A的容器cp。
因此,在为类B(new B())创建对象之前,必须删除容器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();
}
}
请尝试rframe.dispose()而不是rframe.setVisible(false),您能告诉我您的答案有什么问题吗?答案没有更改,您正在创建多个帧,您试图更改其可见性状态的帧不是屏幕上的帧…请尝试rframe.dispose()而不是rframe.setVisible(false)能否告诉我您的?答案没有改变,您正在创建多个帧,您试图更改其可见性状态的帧不是屏幕上的帧…谢谢subash,这是我得到的最好的answr。谢谢subash,这是我得到的最好的answr。不要使用“==”进行比较。相反,您应该使用equals(…)
方法。不要使用“==”进行比较。相反,您应该使用equals(…)
方法。