通过按钮调用时不显示用户输入。使用arraylists Java

时间:2014-10-12 19:45:35

标签: java swing user-interface input arraylist

目前正致力于Java Swing应用程序,以存储大学生评估的名称,类型和权重。他们可以使用GUI按钮添加多个评估(以存储在ArrayList中),并使用GUI上的“显示”按钮显示输入。我已经搜索了我的问题的答案,我是Java的初学者,并且正在使用带有GUI的可实例化类,并且不理解使用扫描仪的相关问题,如果我错了就道歉。

我的代码中没有错误,但是当我按下显示按钮时,“名称”,“类型”和“加权”字段为空白。

这是我的GUI图片:

这是我的评估课,我在其中声明我的变量/ getter和setter:

package arraylistexample;

public class Assessment {
    private String name;
    private String type;
    private double weighting;
    private int count;

    public Assessment(){
        name = new String();
        type = new String();
        count = 0;
        weighting = 0.0;

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public double getWeighting() {
        return weighting;
    }

    public void setWeighting(double weighting) {
        this.weighting = weighting;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

}

这是我的GUI的相关源代码,其中包含我的ArrayList和添加/显示按钮。我还没有编码其他按钮:

package arraylistexample;

import java.util.ArrayList;
import javax.swing.JOptionPane;


public class AssessmentGUI extends javax.swing.JFrame {

private ArrayList<Assessment> aList;
private String name, type;
private double weighting;
private int count;

/**
 * Creates new form AssessmentGUI
 */
public AssessmentGUI() {
    initComponents();
    aList = new ArrayList<>();
    name = new String();
    type = new String();
    weighting = 0.0;
    count = 0;

}


private void addBtnActionPerformed(java.awt.event.ActionEvent evt) {                                       
    // TODO add your handling code here:
    //get Textfield text

    name = nameTf.getText();
    type = typeTf.getText();
    weighting = Double.parseDouble(weightingTf.getText());

    Assessment a = new Assessment();
    a.getName();
    a.getType();
    a.getWeighting();

    //add to arraylist
    aList.add(a);

    //increment counter
    count++;
}                                      



private void displayBtnActionPerformed(java.awt.event.ActionEvent evt) {                                           
    // TODO add your handling code here:
    for(int i = 0; i < aList.size();i++) {
        JOptionPane.showMessageDialog(null, "Name: "+aList.get(i).getName()+"\n Type: "+aList.get(i).getType()+"\n Weighting: "+aList.get(i).getWeighting());
    }


}

2 个答案:

答案 0 :(得分:3)

你必须使用set方法

删除:

a.getName();
a.getType();
a.getWeighting();

添加:

a.setName(nameTf.getText());
a.setType(TypeTf.getText());
a.setWeighting(Double.parseDouble(weightingTf.getText()));

循环中的JOptionPane也不是显示结果的好方法

答案 1 :(得分:2)

创建后,您不会将任何数据放入评估对象中。

// you get data but do nothing with it here
name = nameTf.getText();
type = typeTf.getText();
weighting = Double.parseDouble(weightingTf.getText());

// you create an Assessment object
Assessment a = new Assessment();

// you call a bunch of getters??? Shouldn't you be calling setters?
a.getName();
a.getType();
a.getWeighting();

aList.add(a);

解决方案:当您想要设置Assessment实例的状态时,请调用您的Assessment setter方法,而不是getter方法。

a.setName(name);
a.setType(type);
a.setWeighting(weighting);