发布到数组列表

时间:2011-05-01 13:43:16

标签: java swing

我希望将在Textfields中输入的值传递到数组列表中,尽管我似乎无法做到这一点。我可以查看使用

时在控制台中输入的详细信息
       System.out.println(houses.get(1).getHouseNumber())

但是这不会将新输入的值发布到我的代码中的arraylist中,我无法理解为什么。

ArrayList和ActionListener代码

final ArrayList<House> houses = new ArrayList<House>();
        houses.add(new House());
 button.addActionListener(new ActionListener()
 {
        public void actionPerformed(ActionEvent e) {
        House house = new House();
        house.setHouseNumber(houseNumber.getText());
        house.setStreetName(streetName.getText());
        house.setTown(town.getText());
        house.setPostcode(postcode.getText());
        houses.add(house);
            System.out.println(houses.get(1).getHouseNumber());
            System.out.println(houses.get(1).getStreetName());
            System.out.println(houses.get(1).getTown());
            System.out.println(houses.get(1).getPostcode());
       }
   });

GUI代码

  public void go(){
    frame = new JFrame();
    panel = new JPanel();
    HouseNumberLabel = new JLabel ("House Number");
    houseNumber = new JTextField ("");
    StreetNameLabel = new JLabel ("Street name");
    streetName = new JTextField ("");
    TownLabel = new JLabel ("Town");
    town = new JTextField ("");
    PostcodeLabel = new JLabel ("Postcode");
    postcode = new JTextField ("");
    BedsLabel = new JLabel ("Number of beds");
    beds = new JTextField ("");
    PriceLabel = new JLabel ("Price (£)");
    price = new JTextField ("");
    TypeLabel = new JLabel ("Building Type");
    type = new JTextField ("");
    button = new JButton("Submit");
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    frame.getContentPane().add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300,300);
    frame.setVisible(true)

众议院班级代码

class House {
    private String houseNumber;
private String streetName;
private String town;
private String postcode;

public String getHouseNumber() {
    return houseNumber;
}
public void setHouseNumber(String houseNumber) {
    this.houseNumber = houseNumber;
}

2 个答案:

答案 0 :(得分:2)

String houseNumber;  

不需要这个变量,摆脱它;

JTextField HouseNumber1; 

这不符合正确的java命名约定。变量名称不以大写字符开头。可变名称应为:

JTextField houseNumber; 

现在,在ActionListener中,您可以直接从文本字段中获取文本:

house.setHouseNumber( houseNumber.getText() );   

编辑:

okButton.addActionListener( new ActionListener()
{
    public void actionPerformed(ActionEvent e)
    {
        System.out.println( "ok" );
    }
});

答案 1 :(得分:1)

  

用它打印出数组,并显示[demo2.House@e2eec8, demo2.House@aa9835]

House@e2eec8House@aa9835分别指代ArrayList中的第一和第二宫。尝试这样的事情:

System.out.println(houses.get(0).getHouseNumber());

附录:

  

我在代码中的ArrayList中看不到详细信息。

您可以在toString()中覆盖House;那样你就可以这样做:

System.out.println(houses.get(0));

附录:

  

这不会将新输入的值发布到ArrayList

System.out.println(houses.get(1).getHouseNumber());

是的,这总是得到第二个条目,从0开始。以下将始终将最后一个条目发送到add()

System.out.println(houses.get(houses.size() - 1).getHouseNumber());

附录:你可以像这样循环houses

for (House h : houses) {
    System.out.print(h.getHouseNumber());
    System.out.print(h.getStreetName());
    ...
}