如何处理来自 JInternalFrame parent 的 child?
How dispose a child from JInternalFrame parent?
我有一个parentclass和一个childclass(都是JInternalFrame),如果我的parent我的child是必要的开了。我找啊找,什么都没有……只有模糊的答案。我如何处理 child 关闭 parent? (是的,我知道我说的是 child 而不是 children,但是 child 只能打开一次,所以是 child)我试过实例,但我不是这样很好。非常感谢。 =)
对于接下来的问题,建议您提供Minimal, Complete, and Verifiable example。
然而看看你的代码,你只需要让每个 Parent
内部框架跟踪它自己的 Child
内部框架(参见 List
称为 children
).
然后当你想处置父级时,先处置列表中的每个子级。
这是您的 Parent
class 的修改版本:
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
}
我有一个parentclass和一个childclass(都是JInternalFrame),如果我的parent我的child是必要的开了。我找啊找,什么都没有……只有模糊的答案。我如何处理 child 关闭 parent? (是的,我知道我说的是 child 而不是 children,但是 child 只能打开一次,所以是 child)我试过实例,但我不是这样很好。非常感谢。 =)
对于接下来的问题,建议您提供Minimal, Complete, and Verifiable example。
然而看看你的代码,你只需要让每个 Parent
内部框架跟踪它自己的 Child
内部框架(参见 List
称为 children
).
然后当你想处置父级时,先处置列表中的每个子级。
这是您的 Parent
class 的修改版本:
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
}