我想将“HouseNumber,StreetName,Town,Postcode”的值(我的JPanel上的文本字段)添加到“地址”数组中,最好的方法是什么?谢谢
主要类
public class Main{
public static void main(String[] args){
JFrame frame = new JFrame("Burgess-Brown-Pearson Homes");
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JLabel HouseNumberLabel = new JLabel("House Number");
JTextField HouseNumber = new JTextField("");
JLabel StreetNameLabel = new JLabel("Street Name");
JTextField StreetName = new JTextField("");
JLabel TownLabel = new JLabel("Town");
JTextField Town = new JTextField("");
JLabel PostCodeLabel = new JLabel("PostCode");
JTextField PostCode = new JTextField("");
JLabel BedsLabel = new JLabel("Number of Beds");
JTextField Beds = new JTextField("");
JLabel PriceLabel = new JLabel("Price");
JTextField Price = new JTextField("");
JLabel TypeLabel = new JLabel("Building Type");
JTextField Type = new JTextField("");
JButton Submit = new JButton("Submit");
frame.setSize(500,500);
panel.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
panel.add(HouseNumberLabel);
panel.add(HouseNumber);
panel.add(StreetNameLabel);
panel.add(StreetName);
panel.add(TownLabel);
panel.add(Town);
panel.add(PostCodeLabel);
panel.add(PostCode);
panel.add(BedsLabel);
panel.add(Beds);
panel.add(PriceLabel);
panel.add(Price);
panel.add(TypeLabel);
panel.add(Type);
panel.add(Submit);
frame.pack();
frame.show();
//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]);
System.exit(0);
}
}
地址类
public final class Address{
//Class properties
private String HouseNumber, StreetName, Town, Postcode;
//Allow this person to own a car
private Details owns;
//Constructor
public Address(String aHouseNumber, String aStreetName, String Town, String Postcode)
{
setHouseNumber(aHouseNumber);
setStreetName(aStreetName);
setTown(Town);
setPostcode(Postcode);
}
public Address(){
}
}
//Add a house
public void ownsDetails(Details owns){
this.owns = owns;
}
//Set methods for properties
public void setHouseNumber(String aName){
HouseNumber = aName;
}
public void setStreetName(String aName){
StreetName = aName;
}
public void setTown(String anName){
Town = anName;
}
public void setPostcode (String anName){
Postcode = anName;
}
//Get methods for properties
public String getHouseNumber(){
return HouseNumber;
}
public String setStreetName(){
return StreetName;
}
public String setTown(){
return Town;
}
public String setPostcode(){
return Postcode;
}
**详情类**
public final class Details{
//Class properties
private String Type, Beds, Price;
//Constructor
public Details(String aType, String aBeds, String aPrice){
setType(aType);
setBeds(aBeds);
setPrice(aPrice);
}
//Set methods for properties
public void setType(String aType){
Type = aType;
}
public void setBeds(String aBeds){
Beds = aBeds;
}
public void setPrice(String aPrice){
Price = aPrice;
}
//Get methods for properties
public String getType(){
return Type;
}
public String getBeds() {
return Beds;
}
public String getPrice(){
return Price;
}
}
答案 0 :(得分:2)
我真的不明白这个问题。您拥有所需的所有方法。 无论如何,我会尝试给你一些提示。
首先,如果JTextField用于创建新地址,而不是更新其中一个,那么静态数组可能不是正确的选择。您应该使用ArrayList:
ArrayList<Address> p = new ArrayList<Address>();
然后只需从JTextFields中检索数据并构造另一个Address对象:
Address newAddress = new Address(HouseNumber.getText(),
StreetName.getText(),
Town.getText(),
Postcode.getText());
p.add(newAddress);
这足以解决你的疑虑吗?