【问题标题】:I want to get the text from a JTextField [duplicate]我想从 JTextField 中获取文本 [重复]
【发布时间】:2014-07-25 18:20:46
【问题描述】:

我正在尝试编写一个游戏,用户尝试猜测一个数字,随机生成在 1 到 10 之间,我需要知道如何从 JTextField 中获取文本

    JFrame frame = new JFrame("The Guessing Game");
    frame.setVisible(true);
    frame.setSize(400, 300);
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

    JPanel panel = new JPanel();

    JButton rndbutton = new JButton("Click to create random nubmer!");
    panel.add(rndbutton);
    frame.add(panel);

    JTextField tField = new JTextField(15);
    panel.add(tField);



    //^^ The GUI code ^^



    game.game();
}

}

【问题讨论】:

  • 当你问这个问题时,它没有提示重复吗?
  • 这很简单——只需调用 JTextField 的 getText() 方法。还是你的问题不清楚?

标签: java text input get field


【解决方案1】:

用途:

tField.getText()

如果您不确定如何做某事,您应该首先查看文档以查看是否有相应的功能。在 Google 上搜索也是一个好主意。

【讨论】:

  • 另外,这个问题可能在堆栈溢出之前已经回答过,所以你应该先搜索堆栈溢出。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-11
  • 2012-08-17
  • 1970-01-01
  • 2012-02-25
  • 2010-12-08
  • 2013-05-27
相关资源
最近更新 更多