[英]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,在A
和B
的構造函數中,都調用getContentPane()
以獲取ContentPane
對象。 由於B
不擴展JFrame
,因此它是A
的內部類。 因此,實際上A
和B
使用相同的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
對象並使用它。 A
或B
沒有必要擴展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.