焦点问题与Mac OSX上java7目录模式对话框 [英] Focus issues with java7 modal dialogs on mac osx
问题描述
我已经验证,关于一个小程序的Mac OSX上运行的Swing应用程序。
I've been validating a swing application that runs on an applet for mac osx.
在此确认,我发现了以下问题与模态对话框:
During this validation I found the following issues with the modal dialogs:
- 当一个对话框打开,是setModal(真),它可以阻止根窗口的内容,但如果你点击根窗口上的某个地方,在对话框的推移下,但它应保留在根窗口的顶部
- 如果该对话框中有一个JTextInputField它不接收焦点,甚至当你点击它。
所以,我创建了一个小程序,以显示该问题。你可以帮我了解什么是错在这里?
So I created a small program to show the problem. Can you please help me to understand what is wrong here?
package com.macosx.tests;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class DialogExample extends JApplet{
private static final long serialVersionUID = 1L;
private JPanel panel;
private JButton openDialogBtn;
private void doStart() {
panel = new JPanel();
panel.setPreferredSize(new Dimension(500,500));
openDialogBtn = new JButton("open dialog");
openDialogBtn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
ModalDialog dialog = new ModalDialog(panel, true);
dialog.setVisible(true);
}
});
panel.add(openDialogBtn);
setContentPane(panel);
}
class ModalDialog extends JDialog {
private static final long serialVersionUID = 1L;
public ModalDialog(Component parent, boolean modal) {
Dimension dimensionParentFrame = parent.getSize();
setSize(new Dimension((parent == null) ? 300 : dimensionParentFrame.width / 2, 75));
setModal(modal);
setModalityType(ModalityType.APPLICATION_MODAL);
JTextField txtField = new JTextField();
add(txtField, BorderLayout.CENTER);
}
}
@Override
public void start() {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
doStart();
}
});
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
使用上面创建一个.jar文件(Test.jar的)。一旦做到这一点,创建一个包含以下内容的HTML文件:
Use the above to create a .jar file (test.jar). Once that is done, create a html file with the following content:
<html>
<head>
<title>Dialog test Applet</title>
</head>
<body>
<applet id="DialogTestApplet" height="800" width="600"
code="com.macosx.tests.DialogExample"
archive="test.jar">
</applet>
</div>
</body>
</html>
在这样做,运行HTML文件。你会看到一个小程序,具有灰色背景和一个按钮。然后尝试:
When this is done, run the html file. You'll see an applet with a gray background and with a button. Then try to:
- 单击按钮打开对话框。完成后,点击某处的灰色区域:在对话框云浏览器窗口下,但应保留在顶部,右侧 ?
- 单击按钮打开对话框。之后,单击对话框的文本框,并尝试写东西:textdialog没有收到焦点 。
那么,我究竟做错了什么?可有人用Mac电脑测试这个吗?
So, what am I doing wrong here? Can someone with a mac computer test this please?
感谢
规格:
java.vendor Oracle Corporation
java.version 1.7.0_07
os.name Mac OS X
os.version 10.7.4
browser firefox 15
注意:请注意,当小程序在浏览器上,只有在Mac OSX上运行,这只是发生
推荐答案
我发现了另外一个解决方法。当打开的窗口中,显示出几毫秒的选项窗格并关闭它。它给焦点和选项窗格,然后回到对话框,允许忽略错误。
I found another workaround. When the window is opened, show an optionpane for a few milliseconds and close it. It give the focus to the optionpane and then back to the dialog, allowing to ignore the bug.
code这个snipet添加到您的对话框的构造函数,它应该工作:
Add this snipet of code to your dialog constructor and it should work:
addWindowListener(new WindowAdapter(){
public void windowOpened(WindowEvent e){
JOptionPane pane = new JOptionPane();
final JDialog dialog = pane.createDialog("Please Wait");
Timer timer = new Timer(50, new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialog.dispose();
}
});
timer.setRepeats(false);
timer.start();
dialog.setVisible(true);
}
这篇关于焦点问题与Mac OSX上java7目录模式对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!