我的代码中的未知错误

时间:2013-04-01 05:37:07

标签: java

我运行此代码时遇到三个错误,但由于提供了骨架代码,因此不知道它们是什么。我会发布代码,也许有人可以帮助我。它可能是一个缺少的库或简单的东西,但我会感谢有人的帮助。错误是为分拣机(对于代码的底部)说“找不到符号”,我意识到这不是导入的,所以有更简单的方法可以解决这个问题吗?

package sort;

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


public class AnimationTester implements ActionListener
{

private String algorithm="heap";    //Algoritm to sort by
private int elements=20;            //Number of elements to sort
private int delay=20;                 //speed to sort at
private Panel panel;       //The display Panel
private Thread t;           //A Thread for running sorts


/**
 * Creates an Animation Tester object and tells it to go.
 * This lets everything happen.
 * @param args The standard parameter
 * 
 */
public static void main(String[] args)
{
    (new AnimationTester()).go();
}



/**
 * Launches the Sorting animation.
 * Creates a GUI
 */
private void go(){
    JFrame frame = new JFrame("Sort Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    panel = new Panel();
    frame.add(panel, BorderLayout.CENTER);

    frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);



    //    Where the GUI is created:
    JMenuBar menu;
    JMenu sort,num,delay; 



    //      Create the menu bar.
    menu = new JMenuBar();

    //      Build the first menu.
    sort = new JMenu("Algorithm");
    sort.setMnemonic(KeyEvent.VK_A);
    sort.getAccessibleContext().setAccessibleDescription(
                            "Choose an algorithm");

    //      a group of radio button menu items


    JRadioButtonMenuItem bubble = new JRadioButtonMenuItem("Bubble Sort");
    bubble.setActionCommand("bubble");
    JRadioButtonMenuItem merge = new JRadioButtonMenuItem("Merge Sort");
    merge.setActionCommand("merge");
    JRadioButtonMenuItem heap = new JRadioButtonMenuItem("Heap Sort");
    heap.setActionCommand("heap");
    heap.setSelected(true);
    JRadioButtonMenuItem insert = new JRadioButtonMenuItem("Insertion Sort");
    insert.setActionCommand("insert");
    JRadioButtonMenuItem quick = new JRadioButtonMenuItem("Quick Sort");
    quick.setActionCommand("quick");
    JRadioButtonMenuItem shell = new JRadioButtonMenuItem("Shell Sort");
    shell.setActionCommand("shell");
    JRadioButtonMenuItem selection = new JRadioButtonMenuItem("Selection Sort");
    selection.setActionCommand("select");

    //Group the radio buttons.
    ButtonGroup group1 = new ButtonGroup();
    group1.add(bubble);
    group1.add(merge);
    group1.add(heap);
    group1.add(insert);
    group1.add(quick);
    group1.add(shell);
    group1.add(selection);
    sort.add(bubble);
    sort.add(merge);
    sort.add(heap);
    sort.add(insert);
    sort.add(quick);
    sort.add(shell);
    sort.add(selection);

    bubble.addActionListener(this);
    merge.addActionListener(this);
    heap.addActionListener(this);
    insert.addActionListener(this);
    quick.addActionListener(this);
    shell.addActionListener(this);
    selection.addActionListener(this);


    //      Build second menu in the menu bar.
    num = new JMenu("Elements");
    num.setMnemonic(KeyEvent.VK_S);
    num.getAccessibleContext().setAccessibleDescription(
                            "How many elements?");

    //      a group of radio button menu items


    JRadioButtonMenuItem n10 = new JRadioButtonMenuItem("10");
    n10.setActionCommand("10");
    JRadioButtonMenuItem n20 = new JRadioButtonMenuItem("20");
    n20.setActionCommand("20");
    n20.setSelected(true);
    JRadioButtonMenuItem n30 = new JRadioButtonMenuItem("30");
    n30.setActionCommand("30");    
    JRadioButtonMenuItem n50 = new JRadioButtonMenuItem("50");
    n50.setActionCommand("50");
    JRadioButtonMenuItem n100 = new JRadioButtonMenuItem("100");
    n100.setActionCommand("100");

    //Group the radio buttons.
    ButtonGroup group2 = new ButtonGroup();
    group2.add(n10);
    group2.add(n20);
    group2.add(n30);
    group2.add(n50);
    group2.add(n100);
    num.add(n10);
    num.add(n20);
    num.add(n30);
    num.add(n50);
    num.add(n100);

    n10.addActionListener(this);
    n20.addActionListener(this);
    n30.addActionListener(this);
    n50.addActionListener(this);
    n100.addActionListener(this);

    //    Build third menu in the menu bar.


    delay = new JMenu("Speed delay");
    delay.setMnemonic(KeyEvent.VK_S);
    delay.getAccessibleContext().setAccessibleDescription(
                                "How slow?");

    //      a group of radio button menu items


    JRadioButtonMenuItem s0 = new JRadioButtonMenuItem("0");
    s0.setActionCommand("s000");
    JRadioButtonMenuItem s20 = new JRadioButtonMenuItem("20");
    s20.setActionCommand("s020");
    s20.setSelected(true);
    JRadioButtonMenuItem s50 = new JRadioButtonMenuItem("50");
    s50.setActionCommand("s050");
    JRadioButtonMenuItem s100 = new JRadioButtonMenuItem("100");
    s100.setActionCommand("s100");
    JRadioButtonMenuItem s500 = new JRadioButtonMenuItem("500");
    s500.setActionCommand("s500");

    //Group the radio buttons.
    ButtonGroup group3 = new ButtonGroup();
    group3.add(s0);
    group3.add(s20);
    group3.add(s50);
    group3.add(s100);
    group3.add(s500);
    delay.add(s0);
    delay.add(s20);
    delay.add(s50);
    delay.add(s100);
    delay.add(s500);

    s0.addActionListener(this);
    s20.addActionListener(this);
    s50.addActionListener(this);
    s100.addActionListener(this);
    s500.addActionListener(this);


    //add the menus to the Menubar
    menu.add(sort);
    menu.add(num);
    menu.add(delay);

    frame.setJMenuBar(menu);

    frame.setVisible(true);


    startSort();


}

/**
 * Launches a sort.
 * If a sort is already in progress, does nothing.
 *
 */
private void startSort(){
    //do not sort if already busy sorting.
    if(t!=null&&t.isAlive()) return;
    Double[] values = new Double[elements];
    for (int i = 0; i < values.length; i++)
        values[i] = new Double(Math.random() * panel.getHeight()); 
    Runnable r = new Sorter(panel, values, algorithm, delay);
    t=new Thread(r);
    t.start();
}



/**
 * Detects menu commands
 * @param e The event that occured
 */
public synchronized void actionPerformed(ActionEvent e){
    String s=e.getActionCommand();
    if(s=="bubble"||s=="merge"||s=="heap"||
       s=="insert"||s=="quick"||
       s=="shell"||s=="select"){
        algorithm=s;
        startSort();
    }
    if(s=="10"||s=="20"||s=="30"||
       s=="50"||s=="100"){
        elements=Integer.parseInt(s);
        startSort();
    }

    if(s=="s000"||s=="s020"||s=="s050"||
       s=="s100"||s=="s500"){
        delay=Integer.parseInt(s.substring(1,4));
    }



}

//private static final int VALUES_LENGTH = 30;
private static final int FRAME_WIDTH = 400;
private static final int FRAME_HEIGHT = 400;
}

1 个答案:

答案 0 :(得分:1)

您好我检查了您的代码,发现您的类中没有导入ArrayComponent和Sorter ..

即使您已将它们包含在类路径中,直到它们与此类位于同一个包中,它们也不会被隐式导入。

使用import语句

import ArrayComponent ;
import Sorter;

然后它必须工作