简体   繁体   中英

How do i add values entered into TextFields in a JPanel to an array?

I would like to add the values of "HouseNumber,StreetName,Town,Postcode" which are the textfields on my JPanel to the "Address" Array, What would be the best way to go about this? Thanks

Main Class

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);
 }
 }

Address Class

    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;
  }

** Details Class **

 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;
  }
  }

I really don't understand the problem. You have all the methods that you need. I'll try to give you some tips anyway.

First of all, if the JTextField are used to create new address, and not updating one of the existing, then a static array may not be the right choice. You should use ArrayList instead:

ArrayList<Address> p = new ArrayList<Address>();

Then simply retrieve the data from the JTextFields and construct another Address object:

Address newAddress = new Address(HouseNumber.getText(),
                                 StreetName.getText(),
                                 Town.getText(),
                                 Postcode.getText());
p.add(newAddress);

Is this enough to solve your doubts?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM