我正在尝试创建一个足球管理系统,该系统将允许用户将数据输入到gui中,然后将其保存到数据库中。我有如下代码所示的方法,例如“ getName”,我不确定如何将其链接到我的GUI。我提供了方法的代码以及指向gui图像的链接,以使您可以看到代码很长的样子。任何帮助,将不胜感激。谢谢。
import java.util.Date;
public class Player {
private int id;
private String forename;
private String surname;
private Date dob;
private String position;
private int number;
private int teamid;
public int getID() {
return id;
}
public void setID(int i) {
id = i;
}
}
从外观上看,您需要为您的“添加按钮”应用一个ActionPerformed事件(不适用):
// ADD Button.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Player player = new Player();
// Player ID
String id = textPlayerID.getText();
if (!id.equals("")) {
// Make sure a numerical value was supplied.
if (id.matches("\\d+")) {
player.setID(Integer.parseInt(id);
}
}
// Player First Name
String firstName = textForename.getText();
if (!firstName.equals("")) {
player.setForename(firstName);
}
// Player Last Name
String LastName = textSurname.getText();
if (!lastName.equals("")) {
player.setSurname(lastName);
}
// Player Date Of Birth
String dob = textDOB.getText();
if (!dob.equals("")) {
// You should add code here to 'validate' the fact that
// a valid date was supplied within the JTextField.
// Format the date desired.
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
// Convert String date to a Date data Type.
Date dateOfBirth = formatter.parse(dob);
player.setDOB(dateOfBirth);
}
// Player Position
String position = textPosition.getText();
if (!position.equals("")) {
player.setPosition(position);
}
// Player Number
String number = textNumber.getText();
if (!number.equals("")) {
// Make sure a numerical value was supplied.
if (id.matches("\\d+")) {
player.setNumber(Integer.parseInt(number);
}
}
// Player Team ID
String teamID = textTeamID.getText();
if (!teamID.equals("")) {
// Make sure a numerical value was supplied.
if (id.matches("\\d+")) {
player.setTeamID(Integer.parseInt(teamID);
}
}
// Create and call a method to add the contents
// of the player object into database. If the
// player already exists within the database then
// use the UPDATE sql statement. If the player
// does not exist within the databse then use the
// INSERT INTO sql statement.
addToDatabase(player);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句