gpt4 book ai didi

java - 我的代码中存在未知错误

转载 作者:行者123 更新时间:2023-12-01 14:43:08 26 4
gpt4 key购买 nike

当我运行此代码时,我收到三个错误,但不知道它们是什么,因为这是提供了骨架代码。我将发布代码,也许有人可以帮助我。这可能是缺少库或简单的东西,但我会感谢有人的帮助。错误是说“无法找到排序器的符号”(朝向代码底部),我意识到它没有导入,所以有没有更简单的方法来解决这个问题?

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

最佳答案

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

即使您已将它们包含在类路径中,除非它们与此类位于同一包中,否则它们不会被隐式导入。

使用导入语句作为

import ArrayComponent ;
import Sorter;

那么它一定可以工作

关于java - 我的代码中存在未知错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15738186/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com