我有很多麻烦让这个Jlist工作。当我试图构造JList时,问题就出现了。当我编译时,我得到一条消息说:
"error: constructor JList in class JList cannot be applied to given types; list = new JList(model);
required: no arguments
found: DefaultListModel
reason: actual and formal argument lists differ in length"
此外,我有麻烦添加元素到DefaultListModel。如果您取消注释我试图向模型添加字符串的代码,我得到一条消息说:
JList.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
下面我复制了我的整个程序,所以如果你愿意,你可以复制这个并自己编译。谢谢你!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.lang.*;
import java.awt.event.*;
import javax.swing.filechooser.*;
import java.io.*;
public class JList
{
public static void main (String args[])
{
new MyFrameClass();
}
}
class MyFrameClass extends JFrame implements ActionListener
{
JMenuBar menuBar;
JMenu fileMenu, itemMenu;
JMenuItem loadMenuItem, saveMenuItem, saveAsMenuItem, newMenuItem, deleteMenuItem, deleteAllMenuItem;
JPanel menuBarPanel;
JFileChooser fc;
File file;
JList list;
DefaultListModel model;
MyFrameClass()
{
Container cp;
fc = new JFileChooser();
menuBar = new JMenuBar();
menuBar.setLayout(new FlowLayout());
fileMenu = new JMenu("File");
itemMenu = new JMenu("Item");
loadMenuItem = new JMenuItem("Load");
loadMenuItem.addActionListener(this);
saveMenuItem = new JMenuItem("Save");
saveMenuItem.addActionListener(this);
saveAsMenuItem = new JMenuItem("SaveAs");
saveAsMenuItem.addActionListener(this);
newMenuItem = new JMenuItem("New");
newMenuItem.addActionListener(this);
deleteMenuItem = new JMenuItem("Delete");
deleteMenuItem.addActionListener(this);
deleteAllMenuItem = new JMenuItem("DeleteAll");
deleteAllMenuItem.addActionListener(this);
fileMenu.add(loadMenuItem);
fileMenu.add(saveMenuItem);
fileMenu.add(saveAsMenuItem);
itemMenu.add(newMenuItem);
itemMenu.add(deleteMenuItem);
itemMenu.add(deleteAllMenuItem);
menuBar.add(fileMenu);
menuBar.add(itemMenu);
menuBarPanel = new JPanel();
menuBarPanel.setLayout(new FlowLayout());
menuBarPanel.add(menuBar);
model = new DefaultListModel();
list = new JList(model);
//model.addElement("USA");
/*model.addElement("India");
model.addElement("Vietnam");
model.addElement("Canada");
model.addElement("Denmark");
model.addElement("France");
model.addElement("Great Britain");
model.addElement("Japan");
*/
cp = getContentPane();
cp.add(menuBarPanel);
setupMainFrame();
}
void setupMainFrame()
{
Toolkit tk;
Dimension d;
tk = Toolkit.getDefaultToolkit();
d = tk.getScreenSize(); // Get screen resolution.
setSize(d.width/2, d.height/2); // Set size and location based
setLocation(d.width/4, d.height/4); // on the resolution.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("JList Project"); // For the title bar
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if( e.getSource() == loadMenuItem )
{
System.out.print("load");
int decision = fc.showOpenDialog(this);
if (decision == JFileChooser.APPROVE_OPTION) {
file = fc.getSelectedFile();
System.out.print("Opening: " + file.getName() + "." + "n");
}
}
else if( e.getSource() == saveMenuItem )
{
System.out.print("save");
int decision = fc.showSaveDialog(this);
if (decision == JFileChooser.APPROVE_OPTION) {
file = fc.getSelectedFile();
}
}
else if( e.getSource() == saveAsMenuItem )
{
System.out.print("save as");
int decision = fc.showSaveDialog(this);
if (decision == JFileChooser.APPROVE_OPTION) {
file = fc.getSelectedFile();
}
}
else if( e.getSource() == newMenuItem )
{
System.out.print("new");
}
else if( e.getSource() == deleteMenuItem )
{
System.out.print("delete");
}
else if( e.getSource() == deleteAllMenuItem )
{
System.out.print("delete all");
}
}
}
叹息....
您已经将自己的类命名为JList
:
public class JList
{
public static void main (String args[])
{
new MyFrameClass();
}
}
等为编译器创建了一个名称类。因此,当您创建JList变量时,它是您的 JList类型,而不是javax.swing.JList
,并且当您调用构造函数时,编译器会为您自己的JList类查找一个不存在的构造函数。
解决方案:不要这样做,不要给你的类名与那些关键的核心Java类名冲突,而是把你的类名改为其他的,例如
public class JListTest
{
public static void main (String args[])
{
new MyFrameClass();
}
}