好的所以我已经创建了我的数组并添加了一个动作监听器,这样当单击名为“Submit”的按钮时,我的JTextFields中的所有数据都应该输入到ArrayList中,尽管没有发生这种情况,为什么不会出现任何帮助。赞赏。下面是Action Listener操作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);
}
});
}
}
答案 0 :(得分:6)
虽然您的Main类有字段,因为它也管理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并设置所有valuse。
final ArrayList<House> houses = new ArrayList<House>();
并在您的actionPerformed事件中
House house = new House();
house.setHouseNumber(HouseNumber);
...
houses.add(house);