有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

目前正在开发一个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和add/display按钮。我还没有对其他按钮进行编码:

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) 个答案

  1. # 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);
    

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

    a.setName(name);
    a.setType(type);
    a.setWeighting(weighting);
    
  2. # 2 楼答案

    你必须使用集合方法

    删除:

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

    加上:

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

    此外,循环中的JOptionPane不是显示结果的好解决方案