如何从我的 JFrame 中删除此 JPanel?

How do I remove this JPanel from my JFrame?

我会将其简化为我的程序中与问题实际相关的内容。

在一个 class 我有这个:

private void frame1(){

JFrame Introframe = new JFrame();
Introframe.add(new IntroText());

Introframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Introframe.setLocation(600,100);
Introframe.pack();
Introframe.setVisible(true);

}

另一个class我有这个

public class IntroText extends JPanel {

IntroText(){

setPreferredSize(new Dimension(480, 800));

}

所以我将 class IntroText 中的 JPanel 添加到我的 JFrame 中。但是我该如何删除它?!??!我可以使用 removeAll 删除所有组件,但如果我不想删除所有组件怎么办?感谢您的帮助。

尝试从其父项中删除 JPanel

Introframe.remove(introtext) // where introtext is an instance of IntroText

虽然您不会在添加后立即删除它,但这里有一个示例:

private void frame1(){

JFrame Introframe = new JFrame();
IntroText introtext = new IntroText(); // your panel
Introframe.add(introtext); // add panel

Introframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Introframe.setLocation(600,100);
Introframe.pack();
Introframe.setVisible(true);

Introframe.remove(introtext); // remove it now

}