代码之家  ›  专栏  ›  技术社区  ›  Student

关注jtextfield的问题

  •  1
  • Student  · 技术社区  · 16 年前

    (问题仅在Ubuntu中出现。在Windows中工作正常。我不知道在其他Linux环境中)

    我已经使用componentListener的方法在一个对话框中调用了jtextfield中的focus,但对于这种情况,我不知道为什么。它在文本字段中显示焦点,并快速切换到按钮。运行并查看:

    import java.awt.Component;
    import java.awt.GridLayout;
    import java.awt.event.ComponentAdapter;
    import java.awt.event.ComponentEvent;
    
    import javax.swing.JDialog;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    
    public class User {
    
        private String username = "";
        private String password = "";
    
        public User() {
        // default constructor
        }
    
        public User(String username, String password) {
        this.username = username;
        this.password = password;
        }
    
        /** Create a panel containing the componet and tha label. */
        public JPanel createLabeledComponent(JLabel label, Component comp) {
        GridLayout layout = new GridLayout(2, 1);
        JPanel panel = new JPanel(layout);
        panel.add(label);
        panel.add(comp);
        label.setLabelFor(comp);
        return panel;
        }
    
        public void showEditDialog() {
    
        JLabel usernameLbl = new JLabel(username);
        final JTextField usernameField = new JTextField();
        usernameField.setText(username);
        JPanel usernamePnl = createLabeledComponent(usernameLbl, usernameField);
    
        JLabel passwordLbl = new JLabel(password);
        JPasswordField passwordField = new JPasswordField(password);
        JPanel passwordPnl = createLabeledComponent(passwordLbl, passwordField);
    
        Object[] fields = { "User:", usernamePnl, "Password:", passwordPnl };
    
        JOptionPane optionPane = new JOptionPane(fields, JOptionPane.PLAIN_MESSAGE,
            JOptionPane.OK_CANCEL_OPTION, null, null);
        JDialog dialog = optionPane.createDialog("User Data");
    
        dialog.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentShown(ComponentEvent e) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                usernameField.requestFocusInWindow();
                }
            });
            }
        });
    
        dialog.setVisible(true);
        }
    
        public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
            new User().showEditDialog();
            }
        });
        }
    
    }
    

    知道怎么解决这个问题吗?

    --更新

    现在一切都在EDT上运行。不幸的是,同样的行为。

    顺便说一下,使用joptionpane构造函数的最后一个参数(object initialvalue)都不起作用。

    3 回复  |  直到 16 年前
        1
  •  1
  •   darri    16 年前

    我记得有一个类似的问题,我使用了本页底部的解决方案:

    http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5018574

        2
  •  0
  •   camickr    16 年前

    也许这个 Dialog Focus 解决方案将在Ubuntu上工作(我无法测试它)。

    它在文本字段中显示焦点 快速切换到按钮。

    或者,您可以尝试将requestFocusInWindow()方法调用包装在swinguities.invokelater()中,以将您的request放在EDT的末尾。

        3
  •  0
  •   eaolson    16 年前

    从 How to Use the Focus Subsytem :

    窗口如何获得焦点取决于窗口系统。在所有平台上都没有万无一失的方法来确保窗口获得焦点。

    此外,从 Component.requestFocusInWindow :

    我们将尽一切努力来满足这一要求;然而,在某些情况下,这样做可能是不可能的。在这个组件接收到焦点获得事件之前,开发人员不能假定这个组件是焦点所有者。

    在调用之前可能无法实现组件 requestFocusInWindow . 你试过把 dialog.pack(); 在 setVisible(true) ?

    推荐文章