这是JDK1.6/JDK1.7中Swing中的一个bug,而不是JDK1.5中的bug吗?

这是JDK1.6/JDK1.7中Swing中的一个bug,而不是JDK1.5中的bug吗?,swing,java,Swing,Java,我有一个应用程序,GUI是用JavaSwingJDK1.5开发的。我计划将JDK升级到JDK1.6,但这样做会给我带来问题 问题陈述:如果我打开几个对话框(比如10)并处理它们,然后调用方法“getOwnedWindows()”,它在JDK1.5中返回0,但在JDK1.6中返回10。在JDK1.6中,它返回10,我的设置焦点的算法无法正常工作,因为它能够找到invlay/disposed对话框并尝试在其上设置焦点,但不能在正确有效的对话框或组件上设置焦点,因为算法使用getOwnedWindow

我有一个应用程序,GUI是用JavaSwingJDK1.5开发的。我计划将JDK升级到JDK1.6,但这样做会给我带来问题

问题陈述:如果我打开几个对话框(比如10)并处理它们,然后调用方法“getOwnedWindows()”,它在JDK1.5中返回0,但在JDK1.6中返回10。在JDK1.6中,它返回10,我的设置焦点的算法无法正常工作,因为它能够找到invlay/disposed对话框并尝试在其上设置焦点,但不能在正确有效的对话框或组件上设置焦点,因为算法使用getOwnedWindows()来获取有效且当前打开的对话框

有人能给我建议在JDK1.6中避免这个问题的解决方法吗

下面的代码可以演示这个问题

自定义对话框类: Java代码:

import javax.swing.JDialog;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionEvent;

public class CustomDialog extends JDialog implements ActionListener {
    private JPanel myPanel = null;
    private JButton yesButton = null;
    private JButton noButton = null;
    private boolean answer = false;
    public boolean getAnswer() { return answer; }

    public CustomDialog(JFrame frame, boolean modal, String myMessage) {
    super(frame, modal);
    myPanel = new JPanel();
    getContentPane().add(myPanel);
    myPanel.add(new JLabel(myMessage));
    yesButton = new JButton("Yes");
    yesButton.addActionListener(this);
    myPanel.add(yesButton);
    noButton = new JButton("No");
    noButton.addActionListener(this);
    myPanel.add(noButton); 
    pack();
    setLocationRelativeTo(frame);
    setVisible(true);
    //System.out.println("Constrtuctor ends");
    }

    public void actionPerformed(ActionEvent e) {
    if(yesButton == e.getSource()) {
        System.err.println("User chose yes.");
        answer = true;
        //setVisible(false);
    }
    else if(noButton == e.getSource()) {
        System.err.println("User chose no.");
        answer = false;
        //setVisible(false);
    }
    }

    public void customFinalize() {
        try {
            finalize();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
import java.awt.Window;

public class TestTheDialog implements ActionListener {
    JFrame mainFrame = null;
    JButton myButton = null;
    JButton myButton_2 = null;

    public TestTheDialog() {
        mainFrame = new JFrame("TestTheDialog Tester");
        mainFrame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {System.exit(0);}
            });
        myButton = new JButton("Test the dialog!");
        myButton_2 = new JButton("Print no. of owned Windows");
        myButton.addActionListener(this);
        myButton_2.addActionListener(this);
        mainFrame.setLocationRelativeTo(null);

        FlowLayout flayout = new FlowLayout();
        mainFrame.setLayout(flayout);

        mainFrame.getContentPane().add(myButton);
        mainFrame.getContentPane().add(myButton_2);

        mainFrame.pack();
        mainFrame.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        if(myButton == e.getSource()) {
            System.out.println("getOwnedWindows 1 " + mainFrame.getOwnedWindows().length);
            createMultipleDialogs();

            int i = 0;
            for (Window singleWindow : mainFrame.getOwnedWindows()) {
                System.out.println("getOwnedWindows " + i++ + " "
                        + singleWindow.isShowing() + " "
                        + singleWindow.isVisible() + " " + singleWindow);
            }
            System.out.println("getOwnedWindows 2 " + mainFrame.getOwnedWindows().length);
            //System.gc();
            System.out.println("getOwnedWindows 3 " + mainFrame.getOwnedWindows().length);
            //System.gc();
            System.out.println("getOwnedWindows 4 " + mainFrame.getOwnedWindows().length);
        } else if (myButton_2 == e.getSource()) {
            System.out.println("getOwnedWindows now: " + mainFrame.getOwnedWindows().length);
        }
    }

    public void createMultipleDialogs() {
        for (int a = 0; a < 10; a++) {
            CustomDialog myDialog = new CustomDialog(mainFrame, false,
                    "Do you like Java?");
            myDialog.dispose();
            myDialog.customFinalize();
        }
    }

    public static void main(String argv[]) {

        TestTheDialog tester = new TestTheDialog();
    }
}
主要类别:

Java代码:

import javax.swing.JDialog;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionEvent;

public class CustomDialog extends JDialog implements ActionListener {
    private JPanel myPanel = null;
    private JButton yesButton = null;
    private JButton noButton = null;
    private boolean answer = false;
    public boolean getAnswer() { return answer; }

    public CustomDialog(JFrame frame, boolean modal, String myMessage) {
    super(frame, modal);
    myPanel = new JPanel();
    getContentPane().add(myPanel);
    myPanel.add(new JLabel(myMessage));
    yesButton = new JButton("Yes");
    yesButton.addActionListener(this);
    myPanel.add(yesButton);
    noButton = new JButton("No");
    noButton.addActionListener(this);
    myPanel.add(noButton); 
    pack();
    setLocationRelativeTo(frame);
    setVisible(true);
    //System.out.println("Constrtuctor ends");
    }

    public void actionPerformed(ActionEvent e) {
    if(yesButton == e.getSource()) {
        System.err.println("User chose yes.");
        answer = true;
        //setVisible(false);
    }
    else if(noButton == e.getSource()) {
        System.err.println("User chose no.");
        answer = false;
        //setVisible(false);
    }
    }

    public void customFinalize() {
        try {
            finalize();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
import java.awt.Window;

public class TestTheDialog implements ActionListener {
    JFrame mainFrame = null;
    JButton myButton = null;
    JButton myButton_2 = null;

    public TestTheDialog() {
        mainFrame = new JFrame("TestTheDialog Tester");
        mainFrame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {System.exit(0);}
            });
        myButton = new JButton("Test the dialog!");
        myButton_2 = new JButton("Print no. of owned Windows");
        myButton.addActionListener(this);
        myButton_2.addActionListener(this);
        mainFrame.setLocationRelativeTo(null);

        FlowLayout flayout = new FlowLayout();
        mainFrame.setLayout(flayout);

        mainFrame.getContentPane().add(myButton);
        mainFrame.getContentPane().add(myButton_2);

        mainFrame.pack();
        mainFrame.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        if(myButton == e.getSource()) {
            System.out.println("getOwnedWindows 1 " + mainFrame.getOwnedWindows().length);
            createMultipleDialogs();

            int i = 0;
            for (Window singleWindow : mainFrame.getOwnedWindows()) {
                System.out.println("getOwnedWindows " + i++ + " "
                        + singleWindow.isShowing() + " "
                        + singleWindow.isVisible() + " " + singleWindow);
            }
            System.out.println("getOwnedWindows 2 " + mainFrame.getOwnedWindows().length);
            //System.gc();
            System.out.println("getOwnedWindows 3 " + mainFrame.getOwnedWindows().length);
            //System.gc();
            System.out.println("getOwnedWindows 4 " + mainFrame.getOwnedWindows().length);
        } else if (myButton_2 == e.getSource()) {
            System.out.println("getOwnedWindows now: " + mainFrame.getOwnedWindows().length);
        }
    }

    public void createMultipleDialogs() {
        for (int a = 0; a < 10; a++) {
            CustomDialog myDialog = new CustomDialog(mainFrame, false,
                    "Do you like Java?");
            myDialog.dispose();
            myDialog.customFinalize();
        }
    }

    public static void main(String argv[]) {

        TestTheDialog tester = new TestTheDialog();
    }
}
导入java.awt.event.ActionListener;
导入javax.swing.JFrame;
导入javax.swing.JButton;
导入java.awt.event.WindowAdapter;
导入java.awt.event.WindowEvent;
导入java.awt.event.ActionEvent;
导入java.awt.FlowLayout;
导入java.awt.Window;
公共类TestTheDialog实现ActionListener{
JFrame=null;
JButton myButton=null;
JButton myButton_2=null;
公共测试对话{
大型机=新JFrame(“测试对话测试仪”);
mainFrame.addWindowListener(新的WindowAdapter(){
public void windowClosing(WindowEvent e){System.exit(0);}
});
myButton=newjbutton(“测试对话框!”);
myButton_2=新的JButton(“自有窗口的打印编号”);
myButton.addActionListener(这个);
myButton_2.addActionListener(此);
mainFrame.setLocationRelativeTo(空);
FlowLayout flayout=新的FlowLayout();
大型机设置布局(flayout);
mainFrame.getContentPane().add(myButton);
mainFrame.getContentPane().add(myButton_2);
mainFrame.pack();
mainFrame.setVisible(true);
}
已执行的公共无效操作(操作事件e){
如果(myButton==e.getSource()){
System.out.println(“getOwnedWindows 1”+mainFrame.getOwnedWindows().length);
createMultipleDialogs();
int i=0;
对于(单一窗口:mainFrame.getOwnedWindows()){
System.out.println(“getOwnedWindows”+i++”
+singleWindow.isShowing()+“”
+isVisible()+“”+singleWindow);
}
System.out.println(“getOwnedWindows 2”+mainFrame.getOwnedWindows().length);
//gc();
System.out.println(“getOwnedWindows 3”+mainFrame.getOwnedWindows().length);
//gc();
System.out.println(“getOwnedWindows 4”+mainFrame.getOwnedWindows().length);
}else if(myButton_2==e.getSource()){
System.out.println(“getOwnedWindows now:+mainFrame.getOwnedWindows().length”);
}
}
public void createMultipleDialogs(){
对于(int a=0;a<10;a++){
CustomDialog myDialog=新建CustomDialog(大型机,错误,
“你喜欢Java吗?”);
myDialog.dispose();
myDialog.customFinalize();
}
}
公共静态void main(字符串argv[]){
TestTheDialog tester=新建TestTheDialog();
}
}
运行上述代码会为JDK1.5和JDK1.6提供不同的输出

我将感谢你在这方面的帮助


谢谢

“有人能给我建议解决方法吗?”请明确提及对话框。虽然我认为这是“明智的做法”,而不是“解决办法”。你能解释得多一点吗?你不懂什么?具体点。出于这个确切的原因,在查询中增加更多细节而不是减少细节是值得的。