【问题标题】:How can I use the input from a JOptionPane.showInputDialog to be used as a message? [closed]如何将 JOptionPane.showInputDialog 的输入用作消息? [关闭]
【发布时间】:2013-08-21 13:15:01
【问题描述】:

所以,我需要接受我从(例如)获得的输入

    JOptionPane.showInputDialog(null, "text");

(如果用户输入一些文本,比如“你好”,它将被用作

中的消息
    JOptionPane.showMessageDialog(null, "Hello");

我想我必须使用如下变量: 变量=JOptionPane.showInputDialog(null, "text");

但是如何将两者联系起来,变量会有什么类型呢?

【问题讨论】:

  • 那么,您在使用 Swing 却不知道如何使用变量?这就像驾驶波音飞机却不知道左右之间的区别。从基础开始。阅读介绍性的 Java 甚至编程书籍或教程,在您熟悉基本的 OO 内容(应该在基本的编程内容之后)之前不要接触 Swing。
  • 请选择正确答案并关闭问题。

标签: java eclipse swing input joptionpane


【解决方案1】:

可以一行添加,

JOptionPane.showMessageDialog(null,JOptionPane.showInputDialog(null,"Please enter a message"));

或者,您可以创建一个字符串来保存消息:

String message =  JOptionPane.showInputDialog(null,"Enter a message");
JOptionPane.showMessageDialog(null,message);

请注意,第一个参数可能是null,在这种情况下,默认 Frame 被用作父级,对话框将在屏幕上居中(取决于 L&F)。

阅读JOptionPane

【讨论】:

    【解决方案2】:

    阅读variables 上的 Oracle 文档。然后咨询javadoc JOptionPane

    String message = JOptionPane.showInputDialog(null, "text");
    JOptionPane.showMessageDialog(null, message);
    

    【讨论】:

      【解决方案3】:

      JOptionPane.showInputDialog("Provide Input"); 该语句返回字符串值,可用于传入showMessageDialog。参考以下代码

      String ans=JOptionPane.showInputDialog("Give some input");
      JOptionPane.showMessageDialog(null, ans);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-14
        • 1970-01-01
        • 2013-07-11
        • 1970-01-01
        • 2011-09-27
        • 1970-01-01
        • 1970-01-01
        • 2017-12-04
        相关资源
        最近更新 更多