类DepartmentHhooser中的构造函数DepartmentChooser不能应用于给定的类型

时间:2014-07-27 10:08:49

标签: java swing user-interface bluej

我被要求为我的大学创建一个程序,程序进展顺利,直到我遇到2个错误,我一直试图修复它们一段时间,我在这里阅读了大部分帖子但不幸的是他们做了不帮我 现在第一个错误是“类DepartmentHhooser中的构造函数DepartmentChooser无法应用于给定的类型” 第二个是“注册不是抽象的,不能覆盖抽象方法actionPerformed”

该程序包含几个类,但我附上了其中的一些。

希望你能尽快帮助我,因为截止日期很快。 感谢

departmentChooser

//import java.awt.event.*;
import java.awt.Dimension;
import java.util.Iterator;
import java.awt.List;
import java.util.Collection;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.ListSelectionModel;
import javax.swing.JFrame;
import javax.swing.event.*;

/**
 * Write a description of class DepartmentChooser here.
 * 
 * @author () 
 * @version (19/07/2014)
 */
public class DepartmentChooser extends JPanel implements ListSelectionListener
{
   private static ChooserListener callback;
   private JList Settings;
   private Department[] departments;
   private JFrame frame;
   private String DepartmentCode;
   private String[] adding;


   public DepartmentChooser (ChooserListener callback, School s)
   {
       super ();
       this.callback = callback;

       Collection <Department> x = s.getDepartments ();
       departments = new Department [x.size()];
       String [] adding = new String [x.size()];
       int i = 0;

       for (Iterator itr = x.iterator (); itr.hasNext();)
       {
           departments [i] = (Department) itr.next ();
           adding [i] = departments [i].getName();  
           i++;
        }

        Settings = new JList (adding);
        Settings.setPreferredSize(new Dimension(400, 100));
        //Settings.SetColor(Color.gray);
        Settings.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //Settings.addMouseListener(mouseclicks);
        Settings.addListSelectionListener(this);
        Settings.setVisibleRowCount(6);
        add (new JScrollPane (Settings));
    }

    public void valueChanged (ListSelectionEvent e)
    {
        if (e.getValueIsAdjusting())
        {
            Department lister = departments [Settings.getSelectedIndex()];
            if (lister == null)
            {
                System.out.println ("Empty");
            }
            else
            {
                callback.tell(lister);
            }
        }
    }

    public ChooserListener getCallBack ()
    {
        return this.callback;
    }

    public Department [] getDepartments()
    {
        return this.departments;
    }

    public void repeat()
    {
        int i = Settings.getSelectedIndex();
        if (i < 0)
        {
            callback.tell(null);
        }
        else
        {
            callback.tell (departments[i]);
        }
    }

    public void passThrough (Object o)
    {
        if (o instanceof Department)
        {
            Department list = (Department) o;
            System.out.println(list.getCode());
        }
    }
}

这是测试它

import javax.swing.*;
import java.util.*;
import java.awt.*;

/**
 * Write a description of class DepartmentTester here.
 * 
 * @author () 
 * @version (20/07/2014)
 */
public class DepartmentTester implements ChooserListener 
{
    public DepartmentTester()
    {
        Loader.load();
        School sch = School.get("SCIM");
        DepartmentChooser DepartmentCho = new DepartmentChooser(sch,null);
        JFrame frame = new JFrame ("Department Chooser");
        frame.add (DepartmentCho);
        frame.setVisible(true);
        frame.pack();
    }

    public static void main (String [] args)
    {
        new DepartmentTester();
    }

    public void tell (Object o)
    {
        System.out.println(((School)o).getName() + "\n");
    }

}

最后这是我收到错误的地方

  

“注册不是抽象的,不能覆盖抽象方法actionPerformed”

import java.awt.event.*;
import java.awt.Dimension;
import java.util.Iterator;
import java.awt.List;
import java.util.Collection;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.ListSelectionModel;
import javax.swing.JFrame;
import javax.swing.event.*;

/**
 * Write a description of class DepartmentChooser here.
 * 
 * @author () 
 * @version (19/07/2014)
 */
public class DepartmentChooser extends JPanel implements ListSelectionListener
{
   private static ChooserListener callback;
   private JList Settings;
   private Department[] departments;
   private JFrame frame;
   private String DepartmentCode;
   private String[] adding;


   public DepartmentChooser (ChooserListener callback, School s)
   {
       super ();
       this.callback = callback;

       Collection <Department> x = s.getDepartments ();
       departments = new Department [x.size()];
       String [] adding = new String [x.size()];
       int i = 0;

       for (Iterator itr = x.iterator (); itr.hasNext();)
       {
           departments [i] = (Department) itr.next ();
           adding [i] = departments [i].getName();  
           i++;
        }

        Settings = new JList (adding);
        Settings.setPreferredSize(new Dimension(400, 100));
        //Settings.SetColor(Color.gray);
        Settings.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //Settings.addMouseListener(mouseclicks);
        Settings.addListSelectionListener(this);
        Settings.setVisibleRowCount(6);
        add (new JScrollPane (Settings));
    }

    public void valueChanged (ListSelectionEvent e)
    {
        if (e.getValueIsAdjusting())
        {
            Department lister = departments [Settings.getSelectedIndex()];
            if (lister == null)
            {
                System.out.println ("Empty");
            }
            else
            {
                callback.tell(lister);
            }
        }
    }

    public ChooserListener getCallBack ()
    {
        return this.callback;
    }

    public Department [] getDepartments()
    {
        return this.departments;
    }

    public void repeat()
    {
        int i = Settings.getSelectedIndex();
        if (i < 0)
        {
            callback.tell(null);
        }
        else
        {
            callback.tell (departments[i]);
        }
    }

    public void passThrough (Object o)
    {
        if (o instanceof Department)
        {
            Department list = (Department) o;
            System.out.println(list.getCode());
        }
    }
}

3 个答案:

答案 0 :(得分:3)

您只需阅读错误消息:

  部门DepartmentChooser中的

构造函数DepartmentChooser不能应用于给定类型

这意味着您没有将正确的类型传递给DepartmentChooser构造函数。实际上,构造函数定义为

public DepartmentChooser (ChooserListener callback, School s)

因此,它将ChooserListener作为第一个参数,将School作为第二个参数。你用

来调用它
School sch = School.get("SCIM");
DepartmentChooser DepartmentCho = new DepartmentChooser(sch,null);

所以你将School作为第一个参数而不是ChooserListener传递,并将null作为第二个参数传递。

您没有发布注册代码,但该消息也不言自明:

  

注册不是摘要,并且不会覆盖抽象方法actionPerformed

所以你可能将Registration类定义为

public class Registration implements ActionListener {
    ...
}

由于您声明该类实现了ActionListener,因此它必须实现ActionListener接口中定义的方法。因此必须有以下方法:

@Override
public void actionPerformed(ActionEvent e) {
    ...
}

或者也许它不应该实现ActionListener。

答案 1 :(得分:1)

DepartmentChooser类将ChooserListener和School作为参数进行传递

     DepartmentChooser DepartmentCho = new DepartmentChooser(sch,null); 

学校作为你的第一个参数,它是类型不匹配

答案 2 :(得分:0)

第一个错误:

你是在没有传递参数的情况下实例化对象但是在你的构造函数中你有两个参数所以它不兼容

表示第二个错误:

您正在使用接口,您应该为正在实现的接口上的每个方法创建主体