我将如何通过 JOptionpanes 添加到我的数组?

发布于 2024-11-02 08:18:37 字数 1094 浏览 1 评论 0原文

目前我必须手动将项目添加到我的数组中,但我希望用户能够通过 JOptionPanes 自己执行此操作,最好的方法是什么?这是我当前的代码。

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

  //Create new Person objects

 Address p[] = new Address[3];
 p[0] = new Address("27","Abbey View","Hexham","NE46 1EQ");
 p[1] = new Address("15", "Chirdon Crescent", "Hexham", "NE46 1LE");
 p[2] = new Address("6", "Causey Brae", "Hexham", "NE46 1DB");
 Details c[] = new Details[3];
 c[0] = new Details ("3", "175,000", "Terraced");
 c[1] = new Details  ("6", "300,000", "Bungalow");
 c[2] = new Details ("4", "250,000", "Detached");

  //Send some messages to the  objects
  c[0].setBeds("3 ");
  c[1].setBeds("6");
  c[2].setBeds("4");
  c[0].setPrice("175,000");
  c[1].setPrice("300,000");
  c[2].setPrice("250,000");
  c[0].setType("Terraced");
  c[1].setType("Bungalow");
  c[2].setType("Detached");


  //Set up the association
  p[0].ownsDetails(c[0]);
  p[1].ownsDetails(c[1]);
  p[2].ownsDetails(c[2]);

  //print details
  p[1].printDetails();
  p[2].printDetails();
  p[3].printDetails();


}
  System.exit(0);
}
}

At the moment I have to manually add items to my array but I would like to enable users to do this themselves perhaps through JOptionPanes, what would be the best way to go about this? Here is my current code.

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

  //Create new Person objects

 Address p[] = new Address[3];
 p[0] = new Address("27","Abbey View","Hexham","NE46 1EQ");
 p[1] = new Address("15", "Chirdon Crescent", "Hexham", "NE46 1LE");
 p[2] = new Address("6", "Causey Brae", "Hexham", "NE46 1DB");
 Details c[] = new Details[3];
 c[0] = new Details ("3", "175,000", "Terraced");
 c[1] = new Details  ("6", "300,000", "Bungalow");
 c[2] = new Details ("4", "250,000", "Detached");

  //Send some messages to the  objects
  c[0].setBeds("3 ");
  c[1].setBeds("6");
  c[2].setBeds("4");
  c[0].setPrice("175,000");
  c[1].setPrice("300,000");
  c[2].setPrice("250,000");
  c[0].setType("Terraced");
  c[1].setType("Bungalow");
  c[2].setType("Detached");


  //Set up the association
  p[0].ownsDetails(c[0]);
  p[1].ownsDetails(c[1]);
  p[2].ownsDetails(c[2]);

  //print details
  p[1].printDetails();
  p[2].printDetails();
  p[3].printDetails();


}
  System.exit(0);
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

似狗非友 2024-11-09 08:18:37

您可以在 JOptionPane 上使用 showXXXX 方法,并像在控制台上一样不断提示用户。

不过,我建议只创建一个简单的 JFrame,其中包含允许用户输入多个项目的控件,而不是显示一个又一个对话框。

You could use the showXXXX methods on JOptionPane and keep prompting the user just as you would on the console.

However, I suggest just creating a simple JFrame that would have controls that allows the user to enter multiple items instead of showing one dialog after another.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文