什么是超型? import javax.swing.*; 导入java.awt.*; 导入java.io.*; 导入java.util.*; //明星我的方法实验室 公共类方法扩展了JPanel{ //我将要使用的两个数组列表。 ArrayList english=新的ArrayList(); ArrayList french=新的ArrayList(); //将文本文件作为数组 公共void loadEnglishWords(){ //输入我的文件 字符串filename=“english.txt”; 文件f=新文件(文件名); 试一试{ 扫描器s=新扫描器(f); //逐行扫描所有阵列 而(s.hasNextLine()){ 字符串行=s.nextLine(); 英语。添加(行); } }catch(FileNotFoundException e){//错误的文件名会弹出错误消息 String errorMessage=“错误!”; showMessageDialog(null,errorMessage,“错误!”,JOptionPane.ERROR\u消息); } } //与英语相同的数组作业进行比较 公共无效加载法语单词(){ 字符串filename=“french.txt”; 文件f=新文件(文件名); 试一试{ 扫描器s=新扫描器(f); 而(s.hasNextLine()){ 字符串行=s.nextLine(); 法语。添加(行); } }catch(filenotfounde异常){ String errorMessage=“错误!”; showMessageDialog(null,errorMessage,“错误!”,JOptionPane.ERROR\u消息); } } //检查每条线,使其与阵列平行,以达到相同的位置 公共字符串查找(字符串字){ for(int i=0;i
//我的问题是,每次编译此程序时,错误消息都是: “Method.java:88:error:Method不重写或实现超类型中的方法 @凌驾 ^ 1错误“ //本程序使用arraylist将法语单词翻译成英语单词 我正在使用一个.txt文件来处理我的英语和法语单词集,并让它通过arraylist进行翻译 //在我的程序中,我需要使用JPanel或弹出框要求用户输入他们希望翻译的单词什么是超型? import javax.swing.*; 导入java.awt.*; 导入java.io.*; 导入java.util.*; //明星我的方法实验室 公共类方法扩展了JPanel{ //我将要使用的两个数组列表。 ArrayList english=新的ArrayList(); ArrayList french=新的ArrayList(); //将文本文件作为数组 公共void loadEnglishWords(){ //输入我的文件 字符串filename=“english.txt”; 文件f=新文件(文件名); 试一试{ 扫描器s=新扫描器(f); //逐行扫描所有阵列 而(s.hasNextLine()){ 字符串行=s.nextLine(); 英语。添加(行); } }catch(FileNotFoundException e){//错误的文件名会弹出错误消息 String errorMessage=“错误!”; showMessageDialog(null,errorMessage,“错误!”,JOptionPane.ERROR\u消息); } } //与英语相同的数组作业进行比较 公共无效加载法语单词(){ 字符串filename=“french.txt”; 文件f=新文件(文件名); 试一试{ 扫描器s=新扫描器(f); 而(s.hasNextLine()){ 字符串行=s.nextLine(); 法语。添加(行); } }catch(filenotfounde异常){ String errorMessage=“错误!”; showMessageDialog(null,errorMessage,“错误!”,JOptionPane.ERROR\u消息); } } //检查每条线,使其与阵列平行,以达到相同的位置 公共字符串查找(字符串字){ for(int i=0;i,java,Java,//我的问题是,每次编译此程序时,错误消息都是: “Method.java:88:error:Method不重写或实现超类型中的方法 @凌驾 ^ 1错误“ //本程序使用arraylist将法语单词翻译成英语单词 我正在使用一个.txt文件来处理我的英语和法语单词集,并让它通过arraylist进行翻译 //在我的程序中,我需要使用JPanel或弹出框要求用户输入他们希望翻译的单词 //请注意,我是Java的初学者,请有人帮助我,指出我错在哪里,这样我就可以修改它了。非常感谢你 错误是,在第88
//请注意,我是Java的初学者,请有人帮助我,指出我错在哪里,这样我就可以修改它了。非常感谢你 错误是,在第88行中,您正在使用
@Override
从父类JPanel
重新定义名为init
的方法。但是由于JPanel
就是它(即Java的一部分),它没有init
方法,因此不能重新定义它,因此会出现错误。最可能的情况是,您应该删除@覆盖
,这意味着您希望添加一个新方法,而不是重新定义它
继承是一种获取现有类并根据需要对其进行修改的机制。在您的例子中,类名为Method
,它扩展(继承自)JPanel
,因此JPanel
是类的超类型
如果您刚刚开始,请阅读并自学面向对象的概念。有很多教程,包括。学习愉快 除了前面提到的以外,您还需要做一些更改:
public void init(){
应该是public static void main(字符串args[]){
然后你需要使你的方法成为静态的,即
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.*;
//star my method lab
public class Method extends JPanel {
//two array lists that I am going to use.
ArrayList<String> english = new ArrayList<>();
ArrayList<String> french = new ArrayList<>();
//bring text file as an array
public void loadEnglishWords() {
//input my file
String filename = "english.txt";
File f = new File(filename);
try {
Scanner s = new Scanner(f);
//scan all array line by line
while (s.hasNextLine()) {
String line = s.nextLine();
english.add(line);
}
} catch (FileNotFoundException e) { //wrong file name makes error massage pop up
String errorMessage = "Wrong!";
JOptionPane.showMessageDialog(null, errorMessage, "Wrong!",JOptionPane.ERROR_MESSAGE);
}
}
//same array job with English to compare
public void loadFrenchWords() {
String filename = "french.txt";
File f = new File(filename);
try {
Scanner s = new Scanner(f);
while (s.hasNextLine()) {
String line = s.nextLine();
french.add(line);
}
} catch (FileNotFoundException e) {
String errorMessage = "Wrong!";
JOptionPane.showMessageDialog(null, errorMessage, "Wrong!",JOptionPane.ERROR_MESSAGE);
}
}
//check each line to parallel my arrays to get to same position
public String lookup(String word){
for (int i = 0; i < english.size();i++) {
if (word.equals(english.get(i))) {
return french.get(i);
}
}
//wrong values in arrays
return "No match found";
}
//infinite loop to run my program until get the result
public void mainLoop() {
while (true) {
//pop-up box to ask English words
String tmp = JOptionPane.showInputDialog("Please Enter an English Word!");
//store the result in variable r
String r = lookup(tmp);
String a;
//
if (r == ("No match found")) {
a = "Write a Right Word!";
} else {
a = "The French word is : " + r + ". Play agian?";
}
//asking want to play more or not
int result;
result = JOptionPane.showConfirmDialog(null,a,"RESULT!",JOptionPane.YES_NO_OPTION);
//doens't want to play then shut down
if (result == JOptionPane.NO_OPTION) {
break;
}
}
}
//make all things run in order
@Override
public void init() {
loadEnglishWords();
loadFrenchWords();
mainLoop();
}
}
另外,arrayLists
也需要是static
还有一件事,你应该和.equals()
比较,而不是=
我已经稍微重新编写了您的代码,现在应该可以工作了:
public static void loadEnglishWords() {
static ArrayList english=new ArrayList();
静态ArrayList french=新ArrayList();
//将文本文件作为数组
公共静态void loadEnglishWords(){
//输入我的文件
试一试{
扫描仪s=新扫描仪(新文件(“english.txt”);
//逐行扫描所有阵列
而(s.hasNextLine()){
字符串行=s.next();
英语。添加(行);
}
}catch(FileNotFoundException e){//错误的文件名会弹出错误消息
String errorMessage=“错误!”;
showMessageDialog(null,errorMessage,“错误!”,JOptionPane.ERROR\u消息);
}
}
//与英语相同的数组作业进行比较
公共静态void loadFrenchWords(){
试一试{
扫描仪s=新扫描仪(新文件(“french.txt”);
而(s.hasNextLine()){
字符串行=s.nextLine();
法语。添加(行);
}
}catch(filenotfounde异常){
String errorMessage=“错误!”;
乔蒂奥
static ArrayList<String> english = new ArrayList<>();
static ArrayList<String> french = new ArrayList<>();
//bring text file as an array
public static void loadEnglishWords() {
//input my file
try {
Scanner s = new Scanner(new File("english.txt"));
//scan all array line by line
while (s.hasNextLine()) {
String line = s.next();
english.add(line);
}
} catch (FileNotFoundException e) { //wrong file name makes error massage pop up
String errorMessage = "Wrong!";
JOptionPane.showMessageDialog(null, errorMessage, "Wrong!", JOptionPane.ERROR_MESSAGE);
}
}
//same array job with English to compare
public static void loadFrenchWords() {
try {
Scanner s = new Scanner(new File("french.txt"));
while (s.hasNextLine()) {
String line = s.nextLine();
french.add(line);
}
} catch (FileNotFoundException e) {
String errorMessage = "Wrong!";
JOptionPane.showMessageDialog(null, errorMessage, "Wrong!", JOptionPane.ERROR_MESSAGE);
}
}
//check each line to parallel my arrays to get to same position
public static String lookup(String word) {
for (int i = 0; i < english.size(); i++) {
if (word.equals(english.get(i))) {
return french.get(i);
}
}
//wrong values in arrays
return "No match found";
}
//infinite loop to run my program until get the result
public static void mainLoop() {
while (true) {
//pop-up box to ask English words
String tmp = JOptionPane.showInputDialog("Please Enter an English Word!");
//store the result in variable r
String r = lookup(tmp);
String a;
//
if (r.equals("No match found")) {
a = "Write a Right Word!";
} else {
a = "The French word is : " + r + ". Play agian?";
}
//asking want to play more or not
int result;
result = JOptionPane.showConfirmDialog(null, a, "RESULT!", JOptionPane.YES_NO_OPTION);
//doens't want to play then shut down
if (result == JOptionPane.NO_OPTION) {
break;
}
}
}
//make all things run in order
public static void main(String args[]) {
loadEnglishWords();
loadFrenchWords();
mainLoop();
}
}