0

I am trying to make a Java applet on Eclipse that will print an array of bars of random lengths, then sort them by length and print the new array. However, when I run my program, it says that my applet is not initialized. My code is below. Can anyone help me? Thank you so much!

import java.awt.*; 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import java.applet.Applet;

public abstract class Proj1_1 extends Applet implements ActionListener
{
      private static int[] numbers = new int[10]; 

    public void init() 
      {
      Button startButton = new Button("Sort");
      startButton.addActionListener(this);
      add(startButton);

      setSize(300,300);
      setVisible(true);
      }

      public void paint(Graphics screen)
      {
          numbers = Proj1_1.myRandom(numbers);

          int i;

            for (i = 0; i <= numbers.length - 1; i++)
            {   
                screen.fillRect(20, 20 + 10 * i, numbers[i] + 30, 6);
            } 
      }

      public static  int[] myRandom(int[] numbers)
      {
        Random random = new Random();
        for(int i = 0; i < numbers.length; i++)
            numbers[i] = random.nextInt(20);
        return numbers;
      }

       public static int[] selectionSort (int[] numbers)
      {
          MySort sort = new MySort();
          int[] numbers2 = sort.selectionSort(numbers);
          return numbers2;
      }

      public void actionPerformed(ActionEvent e)
      { 
          int[] numbers2 = Proj1_1.selectionSort(numbers);

           int i;

          for (i = 0; i <= numbers2.length - 1; i++)
           {    
            Graphics screen = null;
            screen.fillRect(20, 20 + 10 * i, numbers2[i] + 30, 6);
          }
      }
}


public class MySort {

    int [] numbers;
    public MySort()
    {

    }

    public MySort(int[] numbers)
     {
        selectionSort(numbers);
    }

    public int[] selectionSort (int[] numbers)
    {
        for(int i=0; i<numbers.length; i++)
                {
                    for(int j=0; j<numbers.length; j++)
                    {
                        if(numbers[i] < numbers[j])
                        {
                            int temp = numbers[i];
                            numbers[i] = numbers[j];
                            numbers[j] = temp;
                        }
                    }
                }
        return numbers;
      }

}
4

2 回答 2

1

您正在声明您的类,abstract因此它不能被实例化:

public abstract class Proj1_1 extends Applet

删除abstract关键字

于 2013-05-08T17:35:57.787 回答
0

如果我们删除抽象,它会显示错误:我们定义的类不是抽象的,所以我们不能覆盖列表中的抽象方法

于 2013-06-25T17:19:58.770 回答