gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:18:36 26 4
gpt4 key购买 nike

我被要求为我的大学创建一个程序,该程序运行良好,直到我遇到 2 个错误,我已经尝试修复它们一段时间了,我阅读了这里的大部分帖子,但不幸的是他们没有不帮我。现在第一个错误是“类 DepartmentChooser 中的构造函数 DepartmentChooser 不能应用于给定类型”第二个是“Registration is not an abstract and dose not override abstract method 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());
}
}
}

这是为了测试它

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");
}

}

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

"Registration is not an abstract and dose not override abstract method 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());
}
}
}

最佳答案

你只需要阅读错误信息:

constructor DepartmentChooser in class DepartmentChooser can not be applied to given types

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

public DepartmentChooser (ChooserListener callback, School s)

因此,它需要一个 ChooserListener 作为第一个参数,一个 School 作为第二个参数。而你正在调用它使用

School sch = School.get("SCIM");
DepartmentChooser DepartmentCho = new DepartmentChooser(sch,null);

因此,您传递一个 School 作为第一个参数而不是一个 ChooserListener,并将 null 作为第二个参数。

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

Registration is not an abstract and dose not override abstract method actionPerformed

所以您可能将注册类定义为

public class Registration implements ActionListener {
...
}

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

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

或者它不应该实现 ActionListener。

关于java - 类 DepartmentChooser 中的构造函数 DepartmentChooser 不能应用于给定类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24979898/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com