如何将方法链接到gui?

时间:2017-12-29 16:34:06

标签: java netbeans

我正在尝试创建一个足球管理系统,允许用户将数据输入到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;
    }
}                     

Image of the gui

2 个答案:

答案 0 :(得分:1)

从表面上看,您需要为添加按钮(稍后)应用 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);
}

答案 1 :(得分:0)

使用“链接到GUI”我想你的意思是在GUI中发生事件时调用这些方法。

您可以首先在GUI类中使用您的Player类的实例,然后在相应的事件处理程序中调用这些方法,这些处理程序非常方便但是由NetBeans创建的非组织(那些是ActionAerPerformed方法)。

如果我没有为您解释足够的内容,请详细说明您要尝试做什么,并请name your GUI components更清晰地了解您的代码。