该方法对类型JFrame没有定义。

时间:2022-07-03 20:59:21

I'm trying to make a gui with two menu lists, with 3 items in each. What my problem is, is that when I click on an item, I get an error "The method displayList(int, AirplaneList) is undefined for the type JFrame"

我正在尝试制作一个包含两个菜单列表的gui,每个列表包含3个项目。我的问题是,当我点击一个项目时,我得到一个错误“方法displayList(int, AirplaneList)没有为JFrame类型定义”

Code for AirplaneController.java:

AirplaneController.java代码:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import java.util.StringTokenizer;

public class AirplaneController implements ActionListener{
       public static StringTokenizer myTokens; //Declares a string tokenizer.
       public static String[] animals;
       public static int i;//Total including error. 
       public static int a; //Total strings

        final static String[] DATA = 
            {...
            };
        final int Cargo = 0;
        final int Passenger = 1;
        final int Business = 2;
        final int All= 4;
        int count=0;
        AirplaneList close= new AirplaneList();
        AirplaneList end=new AirplaneList();

        CargoJet Cairplanes[]= new CargoJet[5];
        BusinessJet Bairplanes[]= new BusinessJet[5];
        PassengerJet Pairplanes[]= new PassengerJet[5];
    JFrame gui;

    /**
     * One-argument constructor that sets the default JFrame and sets
     * to listen on buttons of that frame.
     * @param frame
     */
    public AirplaneController(JFrame frame) {
        gui = frame;
        //gui.setButtonListener;
    }

    public void runList(){
        Airplane parts2 []= new Airplane[8];
        String hate[]= new String [80];
        //AirplaneList close= new AirplaneList();
          i=0;
          animals = new String[80];

         for(int i=0; i<8; i++){
             myTokens = new StringTokenizer(DATA[i],",");

          while (myTokens.hasMoreTokens()) {

             animals[a]=myTokens.nextToken();
             hate[a]=animals[a];
             a++;
          }
         }
          a=0;
          int k=0;
          int z=0;
          int d=0;
          for(i=0; i<80; i+=10){
                 if(hate[i].equals("B")){
                     Bairplanes[k]= new BusinessJet(hate[i],hate[i+1],hate[i+2],hate[i+3],hate[i+4],hate[i+5],hate[i+6],hate[i+7],hate[i+8],hate[i+9]);
                     parts2[d]=Bairplanes[k];
                     d++;
                     k++;
                     count++;
                 }
                 if(hate[i].equals("C")){
                     Cairplanes[a]= new CargoJet(hate[i],hate[i+1],hate[i+2],hate[i+3],hate[i+4],hate[i+5],hate[i+6],hate[i+7],hate[i+8],hate[i+9]);// hate[i]="Catastrophy";
                     parts2[d]=Cairplanes[k];
                     d++;
                     a++;
                     count++;
                     }
                 if(hate[i].equals("P")){
                     Pairplanes[z]= new PassengerJet(hate[i],hate[i+1],hate[i+2],hate[i+3],hate[i+4],hate[i+5],hate[i+6],hate[i+7],hate[i+8],hate[i+9]);// hate[i]="Catastrophy";
                     parts2[d]=Pairplanes[k];
                     d++;
                     z++;
                     count++;
                 }

          }

          for(i=0; i<8; i++){
              System.out.println(parts2[i]+" Parts");
              close.append(parts2[i]);
          }

          System.out.println(close);


    }

    public void createList(int selection) {

        int numPlanes = DATA.length;
        AirplaneList list = new AirplaneList();
        Airplane parts2 []= new Airplane[8];
        String hate[]= new String [80];
        //AirplaneList close= new AirplaneList();
          i=0;
          animals = new String[80];

         for(int i=0; i<8; i++){
             myTokens = new StringTokenizer(DATA[i],",");

          while (myTokens.hasMoreTokens()) {

             animals[a]=myTokens.nextToken();
             hate[a]=animals[a];
             a++;
          }
         }
          a=0;
          int k=0;
          int z=0;
          int d=0;
          for(i=0; i<80; i+=10){
                 if(hate[i].equals("B")){
                     Bairplanes[k]= new BusinessJet(hate[i],hate[i+1],hate[i+2],hate[i+3],hate[i+4],hate[i+5],hate[i+6],hate[i+7],hate[i+8],hate[i+9]);
                     parts2[d]=Bairplanes[k];
                     d++;
                     k++;
                     count++;
                 }
                 if(hate[i].equals("C")){
                     Cairplanes[a]= new CargoJet(hate[i],hate[i+1],hate[i+2],hate[i+3],hate[i+4],hate[i+5],hate[i+6],hate[i+7],hate[i+8],hate[i+9]);// hate[i]="Catastrophy";
                     parts2[d]=Cairplanes[k];
                     d++;
                     a++;
                     count++;
                     }
                 if(hate[i].equals("P")){
                     Pairplanes[z]= new PassengerJet(hate[i],hate[i+1],hate[i+2],hate[i+3],hate[i+4],hate[i+5],hate[i+6],hate[i+7],hate[i+8],hate[i+9]);// hate[i]="Catastrophy";
                     parts2[d]=Pairplanes[k];
                     d++;
                     z++;
                     count++;
                 }

          }

        for (int i = 0; i < numPlanes; i++) {
            switch (selection) 
            {
            case Business:
                list.append(Bairplanes[i]);
                break;
            case Passenger:
                list.append(Pairplanes[i]);
                break;
            case Cargo:
                list.append(Cairplanes[i]);
            default:
                list.insert(parts2[i]);
            }
             gui.displayList(selection, list); // PROBLEM HERE!!
        }
    }
    //@Override


    /**
     * Create a LinkedList of airplane objects either by append, prepend or insert.
     * And display the LinkedList on the GUI.
     * 
     * @param selection     given order of the LinkedList 
     *                      0 for APPEND, 1 for PREPEND, 2 for INSERT
     */

    /*public AirplaneList createList(int selection) {
        gui.displayList();

        if(selection==All) return close;
        return close;
    }*/


        //gui.displayList(selection, close.toString());

    @Override
    public void actionPerformed(ActionEvent arg0) {
        String item = arg0.getActionCommand();
        if (item.equals("Start")){
            createList(All);

        }
          else if(item.equals("Clear")){

        }

        else if (item.equals("Passenger")){
            createList(Passenger);

        }
        else if( item.equals("Business")){
            createList(Business);
        }
        else if( item.equals("Cargo")){
            createList(Cargo);
        }
        else 
            System.exit(0);

}

    }

The problem is here: gui.displayList(selection, list); and creates this error: Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: The method displayList(int, AirplaneList) is undefined for the type JFrame

问题在这里:gui。displayList(选择列表);并在线程“AWT-EventQueue-0”java.lang中创建这个错误:Exception。错误:未解决的编译问题:JFrame类型的方法displayList(int, AirplaneList)没有定义

Here's the current code for AirplaneGUI.java:

以下是目前airplaneguie .java的代码:

import java.awt.*;

import javax.swing.*;
import javax.swing.border.Border;

public class AirplaneGUI {
    public static void main(String[] args) {

    initialize();

    }
    //JButton[] buttons;
    private JMenuBar menuBar;
    private JMenuItem item;
    JFrame frame;
    final Color[] colors = {Color.blue, Color.yellow, Color.green};
    JTextArea[] textAreas;
    final int NUM_LISTS = 3;
    final String[] LIST = {"passenger", "cargo", "bussiness"};
    JTextArea spite;

    public static void initialize() {

        JFrame frame = new JFrame();
        frame.setTitle("flight schedule");
        frame.setSize(250, 250);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 500);

        AirplaneController listener = new AirplaneController(frame);
        listener.runList();
        JMenuBar menuBar = new JMenuBar();

        JMenu menu = new JMenu("File");
        JMenuItem startItem = new JMenuItem("Start");
        startItem.addActionListener(listener);
        menu.add(startItem);

        JMenuItem clearItem = new JMenuItem("Clear");
        clearItem.addActionListener(listener);
        menu.add(clearItem);

        JMenuItem exitItem = new JMenuItem("Exit");
        exitItem.addActionListener(listener);
        menu.add(exitItem);

        JMenu menu1 = new JMenu("Filter");

        JMenuItem passengerItem = new JMenuItem("Passenger");
        passengerItem.addActionListener(listener);
        menu1.add(passengerItem);
        //frame.setBackground(Color.blue);


        JMenuItem bussinessItem = new JMenuItem("Business");
         bussinessItem .addActionListener(listener);
        menu1.add( bussinessItem );
        //frame.setBackground(Color.yellow);


        JMenuItem cargoItem = new JMenuItem("Cargo");
        cargoItem.addActionListener(listener);
        menu1.add(cargoItem);
        //frame.setBackground(Color.green);

        menuBar.add(menu);
        menuBar.add(menu1);


        Container pane = frame.getContentPane();
        frame.setJMenuBar(menuBar);
        frame.setVisible(true);

    }

    public void setupDisplayPanel() {

        Border squareBorder = BorderFactory.createLineBorder(Color.BLACK, 0);
        textAreas = new JTextArea[NUM_LISTS];
        for (int i = 0; i < NUM_LISTS; ++i) {
            textAreas[i] = new JTextArea(LIST[i] + "\n");
            textAreas[i].setBorder(squareBorder);
            textAreas[i].setBackground(colors[i]);
}

    }

    public void displayList(int selection, AirplaneList result) {
        spite.append(result.toString());
    }

    public void deleteList(){
        spite=null;
    }
}

I've tried changing the JFrame gui; in AirplaneController to AirplaneGUI gui;, which then created errors in AirplaneGUI.java:

我尝试改变JFrame gui;在AirplaneGUI的AirplaneController中,它在AirplaneGUI中创建了错误。

javax.swing.JFrame cannot be cast to AirplaneGUI

javax.swing。JFrame不能被投射到AirplaneGUI

How can I fix this?

我怎么解决这个问题?

1 个解决方案

#1


3  

displayList is a method you have written in AirplaneGUI why do you expect it to be in JFrame? Change JFrame gui; to AirplaneGUI gui; and public AirplaneController(JFrame frame) to public AirplaneController(AirplaneGUI frame).

displayList是你在AirplaneGUI中编写的一个方法为什么你希望它在JFrame中呢?JFrame gui变化;AirplaneGUI gui;并将公共飞机控制器(JFrame)与公共飞机控制器(AirplaneGUI框架)连接。

Having said that there are too many compilation errors and missing classes for me to be able to run it.

我说过有太多的编译错误和丢失的类让我无法运行它。

May be you need extend JFrame like public class AirplaneGUI extends JFrame{ . I said "may be" because do not get what you are trying to do .

可能您需要像公共类AirplaneGUI那样扩展JFrame{。我说“可能”是因为你不知道你想做什么。

#1


3  

displayList is a method you have written in AirplaneGUI why do you expect it to be in JFrame? Change JFrame gui; to AirplaneGUI gui; and public AirplaneController(JFrame frame) to public AirplaneController(AirplaneGUI frame).

displayList是你在AirplaneGUI中编写的一个方法为什么你希望它在JFrame中呢?JFrame gui变化;AirplaneGUI gui;并将公共飞机控制器(JFrame)与公共飞机控制器(AirplaneGUI框架)连接。

Having said that there are too many compilation errors and missing classes for me to be able to run it.

我说过有太多的编译错误和丢失的类让我无法运行它。

May be you need extend JFrame like public class AirplaneGUI extends JFrame{ . I said "may be" because do not get what you are trying to do .

可能您需要像公共类AirplaneGUI那样扩展JFrame{。我说“可能”是因为你不知道你想做什么。