栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何等待文本字段中的输入?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何等待文本字段中的输入?

更新: 因此,您要等待用户从GUI输入内容。这是可能的,但由于GUI在另一个线程中运行,因此需要进行 同步

因此,步骤如下:

  1. 创建一个“ holder”对象,将图形用户界面的结果委托给“逻辑”线程
  2. “逻辑”线程等待输入(使用
    holder.wait()
  3. 用户输入文本后,它将同步“ holder”对象并给出结果+通知“逻辑”线程(带有
    holder.notify()
  4. “逻辑”线程从其锁定中释放并继续。

完整示例:

public static void main(String... args) throws Exception {    final List<Integer> holder = new linkedList<Integer>();    final Jframe frame = new Jframe("Test");    final JTextField field = new JTextField("Enter some int + press enter");    frame.add(field);    field.addActionListener(new ActionListener() {        @Override        public void actionPerformed(ActionEvent e) { synchronized (holder) {     holder.add(Integer.parseInt(field.getText()));     holder.notify(); } frame.dispose();        }    });    frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);    frame.pack();    frame.setVisible(true);    // "logic" thread     synchronized (holder) {        // wait for input from field        while (holder.isEmpty()) holder.wait();        int nextInt = holder.remove(0);        System.out.println(nextInt);        //....    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/609537.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号