【问题标题】:User input not displaying when called via button. Using arraylists Java通过按钮调用时不显示用户输入。使用数组列表 Java
【发布时间】:2014-12-07 09:13:18
【问题描述】:

目前正在开发一个 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());
    }


}

【问题讨论】:

    标签: java swing user-interface input arraylist


    【解决方案1】:

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

    // 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);
    

    【讨论】:

      【解决方案2】:

      你必须使用设置方法

      删除:

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

      添加:

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

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

      【讨论】:

        猜你喜欢
        • 2018-08-08
        • 2012-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-18
        • 1970-01-01
        • 2014-01-01
        相关资源
        最近更新 更多