我有一个用Java Swing JDK1.5开发GUI的应用程序。我打算将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提供了不同的输出
感谢您在这方面的帮助。
由于