Java 如何使用来自 JOptionPane.showInputDialog 的输入作为消息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18358469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:51:13  来源:igfitidea点击:

How can I use the input from a JOptionPane.showInputDialog to be used as a message?

javaeclipseswinginputjoptionpane

提问by Zahand

So, I need to take the input I get from (for example)

所以,我需要从我得到的输入(例如)

    JOptionPane.showInputDialog(null, "text");

(and if the user enters some text, say, "hello" it would be used as a message in

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

    JOptionPane.showMessageDialog(null, "Hello");

I think I have to use a variable like: variable=JOptionPane.showInputDialog(null, "text");

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

but how do I connect the two and what type will the variable have?

但是我如何连接两者以及变量有什么类型?

回答by Reimeus

Read the Oracle docs on variables. Then consult the javadocfor JOptionPane

阅读有关变量的 Oracle 文档。然后查阅JOptionPane的javadoc

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

回答by Vimal Bera

JOptionPane.showInputDialog("Provide Input");This statement returns String value which can be use to pass in showMessageDialog. Refer below code

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

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

回答by Azad

You can add it in one line,

您可以在一行中添加它,

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

Or, you can create a string to hold a message:

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

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

Note that first parameter may be null, in which case a default Frame is used as the parent, and the dialog will be centered on the screen (depending on the L&F).

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

Read about JOptionPane

阅读 JOptionPane