简体   繁体   English

如何将输入到 JPanel 中的 TextFields 中的值添加到数组中?

[英]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?我想将我的 JPanel 上的文本字段“HouseNumber,StreetName,Town,Postcode”的值添加到“地址”数组中,关于这个问题,go 的最佳方法是什么? Thanks谢谢

Main Class主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地址 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 ** ** 详细信息 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.首先,如果 JTextField 用于创建新地址,而不是更新现有地址之一,那么 static 数组可能不是正确的选择。 You should use ArrayList instead:您应该改用 ArrayList :

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

Then simply retrieve the data from the JTextFields and construct another Address object:然后简单地从 JTextFields 中检索数据并构造另一个地址 object:

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

Is this enough to solve your doubts?这足以解决你的疑惑吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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