gpt4 book ai didi

java - 无法获取 java 代码将用户输入的数据分配给 Joptionpane 中的对象字段

转载 作者:行者123 更新时间:2023-12-02 02:12:56 25 4
gpt4 key购买 nike

我正在编写一些代码,我必须在其中创建程序和类。名为“Pet”的类具有“名称”、“动物”和“年龄”字段,我必须创建的程序接受输入的数据并将其返回给用户。我已经为它创建了类,但是当涉及到实际程序时,我在将对象的名称分配给用户输入的内容时遇到了麻烦。

我尝试过更改输入类型。

//模板 GUI 对话框。

import javax.swing.JOptionPane;

public class Project_3
{
public static void main(String[] args)
{


String input;

Pet obj = new Pet();


input = JOptionPane.showMessageDialog (null, "Enter the pet's name ");

obj.setName(input);





JOptionPane.showMessageDialog (null, "The Pet's name is " + obj.getName());



System.exit(0)

}
}

最佳答案

JOptionPane.showMessageDialog(null, "Enter the pet's name ") 返回 null,请尝试使用应返回的 JOptionPane.showInputDialog("Enter the pet's name ")用户输入的字符串。

您可以找到有关JOptionPane here的更多信息.

关于java - 无法获取 java 代码将用户输入的数据分配给 Joptionpane 中的对象字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57318516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com