2011-04-29 52 views
2

好所以我做了我的數組並添加了一個動作偵聽器,這樣當點擊名爲「Submit」的按鈕時,我的JTextFields中的所有數據都應該輸入到ArrayList中,儘管這沒有發生,爲什麼不會被讚賞。下面是執行的操作偵聽器操作。從JTextFields發佈到數組

public class Main { 
    String HouseNumber, StreetName, Town, Postcode, Beds, Price, Type; 
    JTextField HouseNumber1, StreetName1, Town1, Postcode1, Beds1, Price1, 
      Type1; 
    JLabel HouseNumberLabel, StreetNameLabel, TownLabel, PostcodeLabel, 
      BedsLabel, PriceLabel, TypeLabel; 
    JButton Submit; 
    JPanel panel; 
    JFrame frame; 

    public static void main(String[] args) { 
     Main gui = new Main(); 
     gui.go(); 
    } 

    public void go() { 
     frame = new JFrame(); 
     panel = new JPanel(); 
     HouseNumberLabel = new JLabel("House Number"); 
     HouseNumber1 = new JTextField(""); 
     StreetNameLabel = new JLabel("Street name"); 
     StreetName1 = new JTextField(""); 
     TownLabel = new JLabel("Town"); 
     Town1 = new JTextField(""); 
     PostcodeLabel = new JLabel("Postcode"); 
     Postcode1 = new JTextField(""); 
     BedsLabel = new JLabel("Number of beds"); 
     Beds1 = new JTextField(""); 
     PriceLabel = new JLabel("Price (£)"); 
     Price1 = new JTextField(""); 
     TypeLabel = new JLabel("Building Type"); 
     Type1 = new JTextField(""); 
     Submit = 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); 

     // Add contents to JFrame and JPanel 
     panel.add(HouseNumberLabel); 
     panel.add(HouseNumber1); 
     panel.add(StreetNameLabel); 
     panel.add(StreetName1); 
     panel.add(TownLabel); 
     panel.add(Town1); 
     panel.add(PostcodeLabel); 
     panel.add(Postcode1); 
     panel.add(BedsLabel); 
     panel.add(Beds1); 
     panel.add(PriceLabel); 
     panel.add(Price1); 
     panel.add(TypeLabel); 
     panel.add(Type1); 
     panel.add(Submit); 
     frame.pack(); 
     frame.show(); 

     final ArrayList<Main> p = new ArrayList<Main>(); 
     Submit.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
       Main array = new Main(); 
       HouseNumber = HouseNumber1.getText(); 
       StreetName = StreetName1.getText(); 
       Town = Town1.getText(); 
       Postcode = Postcode1.getText(); 
       p.add(array); 
      } 
     }); 
    } 
} 
+1

爲了更好地幫助越早,張貼[SSCCE(http://pscode.org/sscce.html)。 – 2011-04-29 17:56:49

+2

使用標準的Java變量命名約定。變量名稱不應以大寫字符開頭。 – camickr 2011-04-29 19:51:13

回答

6

雖然主類中有場,因爲它也是管理GUI,你不希望創建一個ArrayList<Main>

如果你只需要收集所有的字符串,那麼你可以創建

ArrayList<String> houseDetails = new ArrayList<String>(); 

houseDetails.add(HouseNumber); 
houseDetails.add(StreenName); 
houseDetails.add(Town); 
houseDetails.add(Postcode); 

但清潔的事情是創建一個類來管理這些

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; 
    } 
    public String getStreetName() { 
     return streetName; 
    } 
    public void setStreetName(String streetName) { 
     this.streetName = streetName; 
    } 
    public String getTown() { 
     return town; 
    } 
    public void setTown(String town) { 
     this.town = town; 
    } 
    public String getPostcode() { 
     return postcode; 
    } 
    public void setPostcode(String postcode) { 
     this.postcode = postcode; 
    } 
} 

然後創建一個House並設置所有的值。

final ArrayList<House> houses = new ArrayList<House>(); 

,並在您的actionPerformed事件

House house = new House(); 
house.setHouseNumber(HouseNumber); 
... 

houses.add(house); 
+0

定居者是什麼意思? – 2011-04-29 18:43:29

+0

你的'Main'類如何取這些字符串?必須有這些'set'方法或者像'array.HouseNumber = HouseNumber;' – 2011-04-29 18:45:01

+0

這樣的字段,如果我能夠向您展示我的整個代碼,它會更好嗎?即時通訊有點困惑哈哈 – 2011-04-29 18:58:21