gpt4 book ai didi

Java 7 bug Container.remove() 删除所有组件

转载 作者:行者123 更新时间:2023-12-02 07:43:37 26 4
gpt4 key购买 nike

我有一个带有一个直接子级的 AWT 容器,并在索引 0 处添加一个新子级,使其可见,验证父级框架并重新绘制。 (子级是扩展 Panel 的类,但这似乎不相关。)一切都很好,并且 Container 按预期包含 2 个直接子级。

然后,我通过隐藏 child 并调用 Container.remove(this) 来解雇该 child 。从 child 那里获取,然后验证并重新绘制容器。在 1.7 之前的所有 JRE 上,这都可以正常工作,并且原始子项保留在容器中。然而在 1.7 上 remove(this)还删除原来的子项,即清空容器。调用Container.remove(0)具有相同的效果。有人知道如何解决这个问题吗?它看起来像是一个可怕的 JVM 错误。

即- 开始时:容器包含子项 [A]。- 添加 B 后,Container 包含子项 [B,A]。- 调用remove(this)或remove(0)后,Container不包含任何内容:[]。

虽然我可以检索 remove() 之前的原始子项,然后add()它重新回来,这不可避免地会导致重大的重新绘制和 Not Acceptable 性能。

最佳答案

我认为不正确,我无法模拟这个问题,

我这边是Win7、JDK7_04

enter image description here

enter image description here

enter image description here

打印输出

run:
Components Count after Adds :2
Components Count after Adds :3
Components Count after Adds :4
Components Count after Adds :5
Components Count after Adds :6
Components Count after Adds :7
Components Count after Removes :6
Components Count after Removes :5
Components Count after Removes :4
Components Count after Removes :3
Components Count after Removes :2
Components Count after Removes :1
BUILD SUCCESSFUL (total time: 2 minutes 14 seconds)

来自代码

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

public class AddComponentsAtRuntime {

private JFrame f;
private Container panel;
private JCheckBox checkValidate, checkReValidate, checkRepaint, checkPack;

public AddComponentsAtRuntime() {
JButton b = new JButton();
b.setBackground(Color.red);
b.setBorder(new LineBorder(Color.black, 2));
b.setPreferredSize(new Dimension(600, 10));
panel = new Container();
panel.setLayout(new GridLayout(0, 1));
panel.add(b);
f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(panel, "Center");
f.add(getCheckBoxPanel(), "South");
f.setLocation(200, 200);
f.pack();
f.setVisible(true);
}

private JPanel getCheckBoxPanel() {
checkValidate = new JCheckBox("validate");
checkValidate.setSelected(false);
checkReValidate = new JCheckBox("revalidate");
checkReValidate.setSelected(false);
checkRepaint = new JCheckBox("repaint");
checkRepaint.setSelected(false);
checkPack = new JCheckBox("pack");
checkPack.setSelected(false);
JButton addComp = new JButton("Add New One");
addComp.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
JButton b = new JButton();
b.setBackground(Color.red);
b.setBorder(new LineBorder(Color.black, 2));
b.setPreferredSize(new Dimension(600, 10));
panel.add(b);
makeChange();
System.out.println(" Components Count after Adds :" + panel.getComponentCount());
}
});
JButton removeComp = new JButton("Remove One");
removeComp.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
int count = panel.getComponentCount();
if (count > 0) {
panel.remove(0);
}
makeChange();
System.out.println(" Components Count after Removes :" + panel.getComponentCount());
}
});
JPanel panel2 = new JPanel();
panel2.add(checkValidate);
panel2.add(checkReValidate);
panel2.add(checkRepaint);
panel2.add(checkPack);
panel2.add(addComp);
panel2.add(removeComp);
return panel2;
}

private void makeChange() {
if (checkValidate.isSelected()) {
panel.validate();
}
if (checkReValidate.isSelected()) {
panel.revalidate();
}
if (checkRepaint.isSelected()) {
panel.repaint();
}
if (checkPack.isSelected()) {
f.pack();
}
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
AddComponentsAtRuntime makingChanges = new AddComponentsAtRuntime();
}
});
}
}

关于Java 7 bug Container.remove() 删除所有组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11232907/

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