Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将对象数组传递给按钮?_Java_Arrays_Swing_Jtable_Abstracttablemodel - Fatal编程技术网

Java 如何将对象数组传递给按钮?

Java 如何将对象数组传递给按钮?,java,arrays,swing,jtable,abstracttablemodel,Java,Arrays,Swing,Jtable,Abstracttablemodel,我有两个班:一个叫学生,另一个叫课程。我想做一个简单的注册系统模拟 我的学生课程部分的形式如下: class Student { private String id, private Course[] listOfCourses; private int numCourse; //accesing methods public registration(Course course){ listOfCourses[numCourse]=c

我有两个班:一个叫
学生
,另一个叫
课程
。我想做一个简单的注册系统模拟

我的
学生
课程部分的形式如下:

class Student
{
    private String id,
    private Course[] listOfCourses;
    private int numCourse;
    //accesing methods
    public registration(Course course){
          listOfCourses[numCourse]=course;
          numCourse++;
    }
    public Course[] getCourse(){
          return listOfCourses;
    }
}
class Course
{
     String id, String courseName;
     //constructor
     //accesing methods
}
课程
课程的形式如下:

class Student
{
    private String id,
    private Course[] listOfCourses;
    private int numCourse;
    //accesing methods
    public registration(Course course){
          listOfCourses[numCourse]=course;
          numCourse++;
    }
    public Course[] getCourse(){
          return listOfCourses;
    }
}
class Course
{
     String id, String courseName;
     //constructor
     //accesing methods
}
我希望通过按下JavaSwing表单中的按钮,将某个特定学生注册的课程内容显示到jTable中。我尝试了以下方法,但没有任何结果:

Student e=new Student();
Course d[]=new Course[4];   
d=e.getCourses();    //to receive the array of Courses from the Student class
for (int i=0;i<d.length;i++){
      jTable2.setValueAt(estLista[i].getName(), i, 0);
}
Student e=新学生();
课程d[]=新课程[4];
d=e.getCourses()//接收来自学生班级的一系列课程

对于(inti=0;i好的,这对我来说还不是很清楚,但我会放一些代码,告诉我它是否对您有帮助

注意:未测试

对于学生(对不起,我更喜欢使用列表而不是数组):

对于您的UI,我刚刚创建了一个UI的“模拟”,我假设您已经实现了一些更完整的东西……我假设您已经将组件初始化为框架或面板的全局变量,或者至少您有获取它们的方法

public class UIHelper extends JFrame {

    Student student = new Student();
    JButton btnAction;
    JTable myTable;
    public UIHelper() {
        //Methods for creating UI 
        //.
        //.
        //.
        student.setId("stackoverflowed");
        student.setTakenCourses(new ArrayList<Course>());

        btnAction = new JButton("Action!");
        //Methods of the JButton (...)
        btnAction.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                //Now just process since student is a global variable (you can set it static as well) but it shouldn't be a good practice at all
                for (Course course : student.getTakenCourses()) {
                    System.out.println(course.getName());
                             //Add the element to your table.
                }
            }
        });
    }

    public static void main(String[] args) {
        //Assume this is your UI
        new UIHelper();
    }
public类UIHelper扩展JFrame{
学生=新生();
按钮按钮;
JTable-myTable;
公共助理(){
//创建用户界面的方法
//.
//.
//.
student.setId(“堆栈溢出”);
student.setTakenCourses(新ArrayList());
btnAction=newjbutton(“Action!”);
//JButton()的方法
addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
//既然student是一个全局变量(您也可以将其设置为静态),现在只需处理这个过程,但这根本不是一个好的实践
for(课程:student.getTakenCourses()){
System.out.println(course.getName());
//将元素添加到表中。
}
}
});
}
公共静态void main(字符串[]args){
//假设这是您的UI
新的UIHelper();
}

希望我能给你一个想法,向你致意。

从你提供的代码来看,我相信至少有一个原因让你无法获得课程。。因为它没有在注册过程中设置:)(语法也不正确,除非你有注册类。)这可能不是一个完整的解决方案,但它纠正了其中一个问题

public void registration(Course course){
     // listOfCourses[numCourse];
        listOfCourses[numCourse]=course;
      numCourse++;
}

您到底遇到了什么问题?@MrD,我遇到的问题是,我接收课程数组的方式不是我可以将其内容放入JTable中,但您希望有一个完整的UI用于注册?还是只需要一个按钮用于注册应用程序中的硬编码数据?按钮的行为只是打印cou一个学生感到很抱歉,这是我的一个打字错误,无论如何它仍然不起作用