Ok So i开发者_如何学Python have made my array and added an action listener so that when the button named "Submit" is clicked all data from my JTextFields should be entered into an ArrayList although this is not happening, any help on why not would be appreciated. below is the Action Listener action Performed.
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);
}
});
}
}
Although your Main class has the fields, since it's also managing the GUI, you don't want to create an ArrayList<Main>
If you just need to collect all the strings then you can create
ArrayList<String> houseDetails = new ArrayList<String>();
houseDetails.add(HouseNumber);
houseDetails.add(StreenName);
houseDetails.add(Town);
houseDetails.add(Postcode);
but the cleaner thing to do would be to create a class to manage these
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;
}
}
and then create a House and set all the valuse.
final ArrayList<House> houses = new ArrayList<House>();
and in your actionPerformed event
House house = new House();
house.setHouseNumber(HouseNumber);
...
houses.add(house);
精彩评论