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

发布于 2024-11-03 21:06:12 字数 4083 浏览 3 评论 0原文

我想将 JPanel 上的文本字段“HouseNumber、StreetName、Town、Postcode”的值添加到“Address”数组中,最好的方法是什么? 谢谢

主类

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

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

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

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

发布评论

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

评论(1

时光沙漏 2024-11-10 21:06:12

我真的不明白这个问题。您拥有所需的所有方法。
无论如何,我会尝试给你一些建议。

首先,如果 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);

这足以解决您的疑问吗?

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 ?

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