Compiler issues

2019-09-15 01:55发布

问题:

I've been encountering some issues with a Java program which I've begun developing. It would be an online ordering system for the Honeydukes Candy Store of Harry Potter fame, and would be two separate programs. The client program would be in Swing, and would take the order and all that, sending it off to the server program when the order is made. The server program will be significantly less pretty, and its goal would be to write the orders to a text file before giving the green light to the client program that all went well.

For the client program, I'm wanting to have three panels, with the main panel containing the other two. On the left side would be the invntryPanel, containing a JList that has the store's stock. The right side would hold the infoPanel, which uses the CardLayout to change between various panels, using the currently selected item from the JList to determine which panel to display. The infoPanel will also be where the user selects the quantity the wish to purchase.

A button whose position I haven't quite decided on yet can be clicked when the user is done making their order, and it will bring up a separate window asking for general information(name, etc), as well as displaying their total. One final click of a button on that window will send the order to the server program, and the client application will close upon receiving confirmation from the server program that the request went through.

I've gotten started on it, but the compiler seems to be in a bad mood today. What am I doing wrong?

I do apologize for the fact that this is a self-centered question, but I'm just not really sure where else to turn to. I've checked multiple other resources and my code seems to be in order, but obviously it's not.

Thank you in advance.

Source:

import javax.swing.*;
import java.util.*;
import java.awt.*;

public class ClientApp extends JFrame
{
    public static void main(String[] args)
    {
        new ClientApp();
    }


    public ClientApp()
    {
        this.setSize(320,200);
        this.setTitle("Honeydukes Candy Order");
        this.setDefaultCloseOperation(
            JFrame.EXIT_ON_CLOSE);

        ButtonListener bl = new ButtonListener();

        JPanel mainPanel = new JPanel();
        JPanel infoPanel = new JPanel(new CardLayout());
        JPanel invntryPanel = new JPanel();

        String[] candy = {"Acid Pops", "Bat's Blood Soup",
                          "Bertie Bott's Every Flavour Beans",
                          "Blood-flavoured Lollipops",
                          "Cauldron Cakes", "Charm Choc",
                          "Chocoballs", "Chocolate Cauldrons",
                          "Chocolate Frogs", "Chocolate Skeletons",
                          "Chocolate Wands", "Choco-Loco", "Cockroach Clusters",
                          "Nougat", "Crystallised Pineapple",
                          "Drooble's Best Blowing Gum", "Exploding Bonbons",
                          "Toffees", "Fizzing Whizzbees",
                          "Fudge Flies", "Ice Mice",
                          "Jelly Slugs", "Liquourice Wands",
                          "Pepper Imps", "Peppermint Toads",
                          "Pink Coconut Ice", "Pixie Puffs",
                          "Pumpkin Fizz", "Salt Water Taffy",
                          "Shock-o-Choc", "Skeletal Sweets",
                          "Splindle's Lick'O'Rish Spiders",
                          "Sugar Quills", "Sugared Butterfly Wings",
                          "Toothflossing Stringmints", "Tooth-Splintering Strongmints",
                          "Treacle Fudge", "Chocolates", "Wizochoc"};
        JList candyList = new JList(candy);
        candyList.setVisibleRowCount(candy.length);
        candyList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        JScrollPane scroll = new JScrollPane(candyList);
        invntryPanel.add(scroll);
        mainPanel.add(invntryPanel);
        this.setVisible(true);
    }
}

Errors:

ClientApp.java:20: error: cannot find symbol
                BasicButtonListener bl = new BasicButtonListener();
                ^
  symbol:   class BasicButtonListener
  location: class ClientApp
ClientApp.java:20: error: cannot find symbol
                BasicButtonListener bl = new BasicButtonListener();
                                             ^
  symbol:   class BasicButtonListener
  location: class ClientApp
Note: ClientApp.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
2 errors

回答1:

How about this to start with:

String[] candy = new String[38];
String[] candy = {"Acid Pops", "Bat's Blood Soup", /* ... */ };

You're trying to declare the same variable twice in the same scope. You can't do that. Just get rid of the first declaration - the second one is fine.

(Hint: get rid of the hard-coding of the number 38 twice. Use candy.length if you want to know how many elements the array has...)

EDIT: Now we've seen the compiler errors, you also need to:

  • Fix up the arrays elements as per thegrinner's answer
  • Work out what you mean by ButtonListener. Did you mean to use BasicButtonListener instead, perhaps?
  • Declare the candyList variable (currently you're just trying to assign to it)
  • Add an import for java.awt.CardLayout

The errors you've given are reasonably clear - except the multiple issues with candyList, which are all explained by the fact that you're missing the declaration.



回答2:

You're missing a comma after "Pixie Puffs" in your candy array. I believe that's making the compiler believe the line should end there (hence the error expecting a }). This also leads into the illegal start of expression error.