目前我必须手动添加项目到我的数组,但我想让用户做到这一点,也许通过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);
}
}
您可以在JOptionPane
上使用showXXXX方法,并像在控制台上一样不断提示用户。
但是,我建议只创建一个简单的JFrame,它将具有允许用户输入多个项目的控件,而不是一个接一个地显示对话框。