gpt4 book ai didi

java - 如何从 JInternalFrame 父级中处理子级?

转载 作者:行者123 更新时间:2023-12-02 13:02:09 24 4
gpt4 key购买 nike

我有一个父类和一个子类(都是JInternalFrame),如果我的父类打开,我的子类是必需的。我搜索啊搜索,什么也没有……只有模糊的答案。我如何处理关闭 parent 的 child ? (是的,我知道我说的是“ child ”而不是“ child ”,但“ child ”只能打开一次,所以是的,就是“ child ”)我尝试过实例,但我对此不太擅长。非常感谢。 =)

链接: https://pastebin.com/5MLUZDBZ

最佳答案

对于接下来的问题,建议您提供Minimal, Complete, and Verifiable example .

但是查看您的代码,您只需让每个 Parent 内部框架跟踪其自己的 Child 内部框架(请参阅 List 称为 child )。

然后,当您想要处置父级时,请先处置列表中的每个子级。

这是 Parent 类的修改版本:

import java.util.ArrayList;
import java.util.List;

import javax.swing.JDesktopPane;

public class Parent extends javax.swing.JInternalFrame {

private final JDesktopPane pane;
private final List<Child> children = new ArrayList<>();

public Parent(final JDesktopPane pane) {
this.pane = pane;
initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();

jButton1.setText("Open child");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(final java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jButton2.setText("Close all");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(final java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton1)
.addGap(37, 37, 37)
.addComponent(jButton2)
.addContainerGap(193, Short.MAX_VALUE)));
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(30, 30, 30)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addContainerGap(225, Short.MAX_VALUE)));

pack();

}// </editor-fold>

private void jButton1ActionPerformed(final java.awt.event.ActionEvent evt) {
Child c = new Child();
children.add(c);
pane.add(c);
c.setVisible(true);
c.toFront();
}

private void jButton2ActionPerformed(final java.awt.event.ActionEvent evt) {

//CLOSE THIS WINDOW AND CHILD
for (Child child : children) {

child.dispose();
}

this.dispose();

}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
// End of variables declaration
}

关于java - 如何从 JInternalFrame 父级中处理子级?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44244398/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com