[英]Applet Not Initialized
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. 我试图在Eclipse上制作一个Java小程序,该程序将打印随机长度的条形数组,然后按长度对它们进行排序并打印新的数组。 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;
}
}
You're declaring your class as abstract
so it can't be instantiated: 您将您的类声明为
abstract
类,因此无法实例化:
public abstract class Proj1_1 extends Applet
Remove the abstract
keyword 删除
abstract
关键字
如果我们删除抽象,它将显示错误为:我们定义的类不是抽象,因此我们无法覆盖liste中的抽象方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.