2

我在 Ubuntu 上使用 java 有 2 个问题:

  1. JTextField 无法访问,您无法在其中输入任何内容。要重现,您必须单击标签(“单击此标签”),它将打开我的扩展 JDialog。使用取消按钮将其关闭后,JTextField 将无法访问。问题是它不会一直发生。我认为大约 1 对 10 次尝试。发生这种情况时,您必须单击浏览器窗口上的其他位置或再次打开对话框。

  2. 第二个问题是,当 ubuntu 打开 JDialog 时,它会创建其他进程,显示在左侧应用栏上。然后,您可以单击对话框下方小程序上的某个位置,即使认为它是模态的并且应该在顶部,该对话框也会进入浏览器下方。

有没有人在 ubuntu 上遇到过类似的错误,并且知道如何解决它。在 Windows 上一切正常。我们使用 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 运行小程序:

    <html>
    <body>
    <Applet Code="TestApplet.class" width="200" height="100" >
    </Applet>
    </body>
    </html>
4

0 回答 0