gpt4 book ai didi

java - Jlist 和 DefaultListModel 的问题

转载 作者:行者123 更新时间:2023-12-02 04:20:42 25 4
gpt4 key购买 nike

我在让这个 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();
}
}

关于java - Jlist 和 DefaultListModel 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32803540/

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