【问题标题】:Java JTextField cannot typeJava JTextField 无法键入
【发布时间】:2017-10-27 19:14:16
【问题描述】:

我查看了有关此主题的所有主题,但找不到解决问题的方法。

我有一个 JPanel 列表,其中一些包含 JTextField。 其他带有 JButtons 和 JCheckboxes 的面板可以正常工作。

JTextField 是可选择的,但我无法输入。这是用于生成面板的代码

        setSize(new Dimension(450, 33));

        JLabel lblPrefix = new JLabel(propertyToEdit.prefix);
        lblPrefix.setBounds(12, 16, 56, 16);
        add(lblPrefix);

        JTextField stringInputField = new JTextField();
        stringInputField.setEnabled(true);
        stringInputField.setEditable(true);
        stringInputField.setBounds(80, 13, 135, 22);
        stringInputField.setColumns(10);
        add(stringInputField);

这是 Github 上的代码,供鲁莽的 :)

See code on Github

【问题讨论】:

  • Here's the code on Github - 代码应该以正确的minimal reproducible example 的形式在论坛中发布,以证明问题。注意,我不喜欢看到所有的 setBounds() statements()。 Swing 旨在与布局管理器一起使用。
  • 我的猜测是,要么是使用了空布局,要么是窗口无法聚焦
  • 好吧,我可以专注于面板和文本字段,但我会调查 null 布局的可能性

标签: java swing input jtextfield


【解决方案1】:

找到答案了!就是因为这段代码

KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher

开启github

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多