1

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

问题陈述:如果我打开几个对话框(比如 10 个)并处理它们,然后调用方法 'getOwnedWindows()' ,它在 JDK1.5 中返回 0,但在 JDK1.6 中返回 10。与在 JDK1.6 中它返回 10 一样,我设置焦点的算法无法正常工作,因为它能够找到 invlaid/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();
        }
    }
}

主类:

Java 代码:

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();
    }
}

运行上面的代码会为 JDK1.5 和 JDK1.6 提供不同的输出

我将感谢您在这方面的帮助。

谢谢

4

0 回答 0