简体   繁体   English

Jlist和DefaultListModel出现问题

[英]Trouble with Jlist and DefaultListModel

I am having a lot of trouble getting this Jlist to work. 让这个Jlist工作很麻烦。 The problem is occuring when I attempt to construct the JList. 当我尝试构造JList时出现问题。 When I compile I get a message saying: 当我编译时,我收到一条消息:

"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"

Also, I am having trouble adding elements to the DefaultListModel. 另外,我在向DefaultListModel添加元素时遇到了麻烦。 If you un-comment the code where I attempt to add strings to the model, I get a message saying: 如果取消注释试图在模型中添加字符串的代码,则会收到一条消息,提示:

JList.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Below I have copied my entire program so if you would like you can copy this and compile it yourself. 下面,我复制了我的整个程序,因此,如果您愿意,可以复制并自己编译。 Thank you! 谢谢!

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

Sigh.... 叹....

You've named your own class JList : 您已经将自己的类命名为JList

public class JList
{
  public static void main (String args[])
  {
    new MyFrameClass();
  }
}

and so have created a name class for the compiler. 因此为编译器创建了一个名称类。 Because of this, when you create a JList variable it is of your JList type, not javax.swing.JList , and when you call a constructor, the compiler looks for one for your own JList class, a constructor which does not exist. 因此,当您创建一个JList变量时,它是您的 JList类型,而不是javax.swing.JList ,并且在调用构造函数时,编译器会为您自己的JList类(一个不存在的构造函数)寻找一个。

Solution: Don't do this, don't give your classes names that clash with those of critical core Java classes, but instead change the name of your class to something different, eg, 解决方案:不要这样做,不要给您的类命名与关键的Java类冲突的名称,而应将您的类的名称更改为其他名称,例如,

public class JListTest
{
  public static void main (String args[])
  {
    new MyFrameClass();
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM