从另一个班级

时间:2016-03-22 22:39:54

标签: java eclipse user-interface

这是我的arraylist类的代码; 包餐厅;

 import java.util.ArrayList;



    public class Controlclass {

        public static void main(String[] args) {
            // Create ArrayList for foodItems subclasses
            ArrayList<Mains> MainsList = new ArrayList<Mains>();
    //add items to the MainsList
            Mains  CurriedFriedRice = new Mains (" Curried fried rice ", 4.00, " Yummy curried fried rice " , 500);
            Mains  LambChopsWithCarrotPuree = new Mains (" Lamb chops with carrot puree ", 6.00, " Delicious Lamb chops with carrot puree " , 580);
            Mains  PadThaiNoodles = new Mains (" Pad Thai noodles ", 5.00, " Amazing thai noodles " , 480);
            Mains  SalmonAndAsparagusFajitas = new Mains (" Salmon and asparagus fajitas ", 4.90, " Fajita you will always come back for " , 450);


    MainsList.add(CurriedFriedRice);
            MainsList.add(LambChopsWithCarrotPuree);
            MainsList.add(PadThaiNoodles);
            MainsList.add(SalmonAndAsparagusFajitas);


    for (Mains mains: MainsList){
                System.out.println(mains);
            }

这是我需要在这些arraylists中使用的GUI类;

*

public class TheFoodLoversLounge {
    private final GridLayout layout; // layout of this frame
    private JFrame frame;
    private final JPanel buttonbar;
    private final JPanel combobar;
    private final JPanel receiptbar;
private final JComboBox<String> mainsc; 
public class TheFoodLoversLounge {
    private final GridLayout layout; // layout of this frame
    private JFrame frame;
    private final JPanel buttonbar;
    private final JPanel combobar;
    private final JPanel receiptbar;
    private final JComboBox<String> starters; 
    private final JComboBox<String> mainsc; 
    private final JComboBox<String> dessert; 
    private final JButton getTotalPrice;
    private final JLabel l2;
    private final JLabel l3;
    private final JLabel l4;
    private final JLabel l5;
    private final JLabel l6;
    private final JLabel l7;
    private final JLabel l8;
    private final JLabel l9;
    private final JLabel l10;
    private final JLabel r1;
    private final JLabel r2;
    private final JLabel r3;
    private final JLabel r4;
    private final JLabel r5;
    private final JLabel r6;
    private final JLabel r7;
    private final JLabel r8;
    private final JLabel r9;
    private Controlclass control;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TheFoodLoversLounge window = new TheFoodLoversLounge();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**
     * Create the application.
     */
    public TheFoodLoversLounge() {
        MAXTABLES=10;
        MAXDINERS=10;
        initialize();
        control = new Controlclass();
        layout = new GridLayout(0,1,0,5);
        starters=new JComboBox<>(); 
        mainsc=new JComboBox<>(); 
        dessert=new JComboBox<>(); 

        for (Starters starterI: control.StarterList){
            starters.addItem(starterI.toString());
        }
        starters.setMaximumRowCount(5);
        for (Mains mainsI: control.MainsList){
            mainsc.addItem(mainsI.toString());
        }
        mainsc.setMaximumRowCount(5);
        for (Dessert dessertI: control.DessertList){
            dessert.addItem(dessertI.toString());
        }
buttonbar= new JPanel(layout);
        buttonbar.setBackground(Color.LIGHT_GRAY);
        buttonbar.add(getTotalPrice); buttonbar.add(l10);
combobar= new JPanel(layout);
        combobar.setBackground(Color.PINK);
        combobar.add(l1); combobar.add(starters);
        combobar.add(l2); combobar.add(mainsc);
        combobar.add(l4); combobar.add(dessert);
private void initialize() {
        frame = new JFrame();
        frame.setLocationRelativeTo(null);
        //frame.setResizable(false);
        frame.setSize(768, 800);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
private class TheListener implements ActionListener
    {
        // process text field events
        @Override
        public void actionPerformed(ActionEvent event)
        { 
            String string = "";
            double arr [];
            Orders ord = new Orders();
            if (event.getSource() ==getTotalPrice){
                ord.diners= Integer.parseInt((String) diners.getItemAt(diners.getSelectedIndex()));
                ord.tableno=Integer.parseInt((String) tableno.getItemAt(tableno.getSelectedIndex()));
                ord.item[0]=starters.getItemAt(starters.getSelectedIndex());
                // ord.qty[1]=
                        ord.rate[0]=control.StarterList.get(starters.getSelectedIndex()).price;
                        ord.item[1]=mainsc.getItemAt(mainsc.getSelectedIndex());
                        // ord.qty[2]=
                        ord.rate[1]=control.MainsList.get(mainsc.getSelectedIndex()).price;
                        ord.item[2]=dessert.getItemAt(dessert.getSelectedIndex());
                        // ord.qty[3]=
                        ord.rate[2]=control.DessertList.get(dessert.getSelectedIndex()).price;
}
        }}}

* 每个人都可以忽略语法错误,因为它不是完整的代码,我只想专注于arraylist因为每次我尝试编译它时会给我以下错误,而实际代码在语法上是正确的。 线程“AWT-EventQueue-0”中的异常java.lang.Error:未解决的编译问题:

StarterList cannot be resolved or is not a field
    MainsList cannot be resolved or is not a field
    DessertList cannot be resolved or is not a field
    DrinksList cannot be resolved or is not a field
    FishList cannot be resolved or is not a field
    StarterList cannot be resolved or is not a field
    MainsList cannot be resolved or is not a field
    DessertList cannot be resolved or is not a field
    FishList cannot be resolved or is not a field
    DrinksList cannot be resolved or is not a field

    at Restaurant.TheFoodLoversLounge.<init>(TheFoodLoversLounge.java:121)
    at Restaurant.TheFoodLoversLounge$1.run(TheFoodLoversLounge.java:91)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

这些是我得到的错误。我不知道如何解决这些问题。请有人帮我。不完整代码的原因是我的项目,我不敢冒险。所以来自你们的解决方案将受到高度赞赏。

2 个答案:

答案 0 :(得分:1)

请勿在{{1​​}}方法中定义您的列表。将它们定义为成员并在构造函数中初始化它们。然后为每个列表编写getter方法。

main

最后,使用getter方法访问列表。

public class Controlclass {
   private ArrayList<Mains> MainsList;

   public ControlClass() {
       MainsList = new ArrayList<Mains>()
   }

   public ArrayList<Mains> getMainsList() {
        return MainsList;
   }
}

答案 1 :(得分:1)

Controlclass不提供对您在ArrayList方法中定义的任何main的任何访问权限(并且对您的问题有很大的暗示)。

首先在Controlclass和提供public getter中定义实例字段,以允许他们访问这些字段(通过您可以控制的方法 - 例如,您可以返回不可变列表或复制返回之前列表)

import java.util.ArrayList;
import java.util.List;

public class Controlclass {

        private List<...> starterList;
        private List<...> mainsList;
        private List<...> dessertList;
        private List<...> drinksList;
        private List<...> fishList;

    public static void main(String[] args) {
        Controlclass controlClass = new Controlclass();
    }

    public Controlclass() {
        starterList = new ArrayList<>(25);
        mainsList = new ArrayList<>(25);
        dessertList = new ArrayList<>(25);
        drinksList = new ArrayList<>(25);
        fishList = new ArrayList<>(25);

        // Populate the values...
    }

    public List<...> getStarterList() {
        return starterList;
    }

    public List<...> getMainsList() {
        return mainsList;
    }

    public List<...> getDessertList() {
        return dessertList;
    }

    public List<...> getDrinksList() {
        return drinksList;
    }

    public List<...> getFishList() {
        return fishList;
    }
}

(ps你需要用适当的泛型替换<...>,我真的不想深入研究所有代码来解决这个问题)

接下来将Controlclass的实例传递给其他类

public class TheFoodLoversLounge {
    //...
    private Controlclass control;

    public TheFoodLoversLounge(Controlclass control) {
        this.control = control
        //...

最后,请使用Controlclass

的属性
for (Starters starterI: control.getStarterList()) {
    starters.addItem(starterI.toString());
}

(并且让你的思想成为程序的主要切入点,有两个main方法将会非常混乱)