【问题标题】:holding key breaks java swing form拿着钥匙打破java swing表格
【发布时间】:2017-03-27 04:58:06
【问题描述】:

我正在开发一个基本的 Java Swing UI 应用程序,它似乎工作正常,除了我在使用我创建的表单时注意到的一个问题。

问题是当我在文本框中输入时,按住一个键似乎会破坏表单——即,在执行此操作后,文本框似乎不再接受任何输入。我很难相信这是 Java Swing 组件的已发布问题,但我也看不出它与我的代码有什么关系。有没有人见过类似的问题?如果相关,我正在使用带有 Intellij IDEA 的 OS x。

我的表单是这样定义的:

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;


@SuppressWarnings("serial")
public class LoginDialogMcve extends JFrame {

  protected JTextField stringEntry, dateEntry;

  public LoginDialogMcve() {
    super("Create Textbox");

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout());

    this.stringEntry = new JTextField(5);
    add(this.stringEntry);

    this.dateEntry = new JTextField(5);
    add(this.dateEntry);

  }

  public static void main(String... args) {
    LoginDialogMcve me = new LoginDialogMcve();
    me.pack();
    me.setLocationByPlatform(true);
    me.setVisible(true);
  }
}

编辑:感谢您的反馈,这很可能不是 Java 问题。可能与 OSx 有关...使用 mcve 上传更简单的示例

【问题讨论】:

  • 上述代码(显然已删除了很多代码)是否充分重现了您的问题?单独为我们证明您的问题就足够了吗?还有为什么你在 ActionMap 中放了一个值,而在 InputMap 中却没有放同样的值?
  • 是的,据我所知,任何摇摆文本框都可以重现该问题。特别是,它发生在调用 actionPerformed 之前,这就是为什么我希望该方法无关紧要。我认为我不需要 inputMap,因为我将触发操作的唯一输入是单击验证按钮。这有意义吗?如果不是,我会用 InputMap 做什么?
  • 好的,在尝试自己测试您的代码之后,包括更改 main 方法中的类名称 -- 我无法重现您的问题,这意味着您的代码目前还不够。建议在发布有效的minimal reproducible example 之前暂停此问题。
  • 我可以用这个确切的代码重现。您可以按住字母键并观察输入,例如 dddddddddddd?对我来说它会立即停止,我无法再使用该表单。
  • 我用我的 MCVE 尝试过,但请注意,我们大多数人都没有 MIG 布局。我将在下面发布我的 MCVE 作为社区 wiki,以便您自己测试。

标签: java macos swing key-events


【解决方案1】:

我的 MCVE 运行良好。自己测试看看。

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
// import net.miginfocom.swing.MigLayout;

@SuppressWarnings("serial")
public class LoginDialogMcve extends JFrame {

    protected JTextField stringEntry, dateEntry;
    protected JLabel stringEntryLabel, dateEntryLabel;
    protected JButton print;
    protected Action validateAction;

    public LoginDialogMcve() {
        super("Create Textbox");

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());
        // setLayout(new MigLayout("ins 10, gap 5",
        // "[][grow]",
        // "[][][]"));

        this.stringEntryLabel = new JLabel("Name:");
        add(this.stringEntryLabel);

        this.stringEntry = new JTextField(5);
        add(this.stringEntry);

        this.dateEntryLabel = new JLabel("Date:");
        add(this.dateEntryLabel);

        this.dateEntry = new JTextField(5);
        add(this.dateEntry);

        this.validateAction = new MyAction();

        this.print = new JButton(this.validateAction);
        add(this.print);

        getRootPane().getActionMap().put("validate", this.validateAction);
    }

    public static void main(String... args) {
        LoginDialogMcve me = new LoginDialogMcve();
        me.pack();
        me.setLocationByPlatform(true);
        me.setVisible(true);
    }

    public class MyAction extends AbstractAction {

        public MyAction() {
            super("Validate");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            // lots of irrelevant code
        }

    }

}

【讨论】:

  • 我遇到了与该代码相同的问题。你使用什么 OS/Swing jar?
猜你喜欢
  • 1970-01-01
  • 2017-06-09
  • 1970-01-01
  • 2015-04-18
  • 2016-03-20
  • 1970-01-01
  • 2014-10-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多