我在使这个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");
}
}
}
答案 0 :(得分:2)
...叹息
您已命名自己的班级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();
}
}