我在Ubuntu上有两个java问题:
JTextField无法访问,您无法在其中键入任何内容。要重现,您必须单击标签('单击此标签'),它将打开我的扩展JDialog。使用“取消”按钮关闭它后,JTextField将无法访问。问题是它不会一直发生。我认为大约有1对10次尝试。如果发生这种情况,您必须单击浏览器窗口上的其他位置或再次打开对话框。
第二个问题是,当ubuntu打开JDialog时,它会创建一个显示在左侧应用栏上的其他进程。你可以点击对话框下applet上的某个地方,这个对话框会在浏览器下面,即使它是模态的,也应该在顶部。
是否有人在使用ubuntu时遇到类似的错误并知道如何修复它。在窗户上一切正常。我们使用ubuntu-12.04-desktop和java 1.6.0_34-b04。它在firefox 11.0和Google Chrome(我认为最新)中进行了测试
这是我的代码TestApplet.java类:
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JApplet;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import client.utilities.GUIUtilities;
@SuppressWarnings("serial")
public class TestApplet extends JApplet {
public void init() {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
JApplet applet = TestApplet.this;
applet.setLayout(new FlowLayout());
JTextField ts = new JTextField("Test text");
ts.setColumns(10);
applet.add(ts);
applet.add(getCallCalendarButton(ts));
}
});
} catch (Exception e) {
System.err.println(e.getCause());
}
}
private JLabel callCalendarButton;
private MyDialog aDialog;
protected JLabel getCallCalendarButton(final JComponent cmp) {
if (callCalendarButton == null) {
callCalendarButton = new JLabel("Click this label!!");
callCalendarButton.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (callCalendarButton.isEnabled()) {
Frame parentFrame = null;
if (parentFrame == null)
parentFrame = GUIUtilities.getParentFrame(cmp);
System.out.println(parentFrame);
aDialog = new MyDialog(parentFrame, cmp);
aDialog.setVisible(true);
System.out.println("qwewqe");
cmp.requestFocusInWindow();
}
}
});
}
return callCalendarButton;
}
}
这里是扩展的JDialog类(MyDialog.java):
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class MyDialog extends JDialog {
private JButton okButton;
private JButton cancelButton;
private JComponent owner;
private int WIDTH = 230;
private int HEIGHT = 230;
Frame parent;
public MyDialog(Frame parent, JComponent owner) {
super(parent);
this.parent = parent;
this.owner = owner;
okButton = new JButton("OK");
okButton.setMnemonic('O');
okButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
MyDialog.this.setVisible(false);
MyDialog.this.dispose();
}
});
cancelButton = new JButton("Cancel");
cancelButton.setMnemonic('C');
cancelButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
MyDialog.this.setVisible(false);
}
});
this.setLayout(new BorderLayout());
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
bottomPanel.add(okButton);
bottomPanel.add(cancelButton);
this.add(bottomPanel, BorderLayout.SOUTH);
this.setModal(true);
this.setBounds(100, 100, WIDTH, HEIGHT);
//
this.addComponentListener(new ComponentAdapter(){
@Override
public void componentHidden(ComponentEvent e){
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
MyDialog.this.owner.requestFocusInWindow();
//MyDialog.this.parent.toFront();
//MyDialog.this.parent.requestFocusInWindow();
}
});
}
});
}
}
使用此html运行applet:
<html>
<body>
<Applet Code="TestApplet.class" width="200" height="100" >
</Applet>
</body>
</html>