“Cannont实例化类型ActionListener”该代码在我的华氏面板类中工作......按照预期的方式工作。我试图让PigLatin使用一个GUI,就像华氏一样......但它返回那个错误......我需要一个不同类型的监听器吗?我尝试了文字听众和多,但没有奏效。我有一个华氏类,创建一个GUI,显示华氏转换为摄氏温度,并与ActionListener(新的TempListener)一起工作,没有错误。我知道一个接口不能像对象那样实例化,但为什么我可以在其他类中实现呢?无法实例化ActionListener
import javax.swing.*;
public class PigLatinTranslator extends JPanel
{
private JLabel inputLabel, outputLabel, resultLabel;
private JTextField piglatin;
public PigLatinTranslator()
{
inputLabel = new JLabel ("What would you like to translate:");
outputLabel = new JLabel ("Pig Latin translation: ");
resultLabel = new JLabel ("---");
piglatin = new JTextField (5);
piglatin.addActionListener (new MyActionListener());
add (inputLabel);
add (piglatin);
add (outputLabel);
add (resultLabel);
setPreferredSize (new Dimension(300, 75));
setBackground (Color.red);
}
//-----------------------------------------------------------------
// Translates a sentence of words into Pig Latin.
//-----------------------------------------------------------------
public static String translate (String sentence)
{
String result = "";
sentence = sentence.toLowerCase();
Scanner scan = new Scanner (sentence);
while (scan.hasNext())
{
result += translateWord (scan.next());
result += " ";
}
return result;
}
//-----------------------------------------------------------------
// Translates one word into Pig Latin. If the word begins with a
// vowel, the suffix "yay" is appended to the word. Otherwise,
// the first letter or two are moved to the end of the word,
// and "ay" is appended.
//-----------------------------------------------------------------
private static String translateWord (String word)
{
String result = "";
if (beginsWithVowel(word))
result = word + "yay";
else
if (beginsWithBlend(word))
result = word.substring(2) + word.substring(0,2) + "ay";
else
result = word.substring(1) + word.charAt(0) + "ay";
return result;
}
//-----------------------------------------------------------------
// Determines if the specified word begins with a vowel.
//-----------------------------------------------------------------
private static boolean beginsWithVowel (String word)
{
String vowels = "aeiou";
char letter = word.charAt(0);
return (vowels.indexOf(letter) != -1);
}
//-----------------------------------------------------------------
// Determines if the specified word begins with a particular
// two-character consonant blend.
//-----------------------------------------------------------------
private static boolean beginsWithBlend (String word)
{
return (word.startsWith ("bl") || word.startsWith ("sc") ||
word.startsWith ("br") || word.startsWith ("sh") ||
word.startsWith ("ch") || word.startsWith ("sk") ||
word.startsWith ("cl") || word.startsWith ("sl") ||
word.startsWith ("cr") || word.startsWith ("sn") ||
word.startsWith ("dr") || word.startsWith ("sm") ||
word.startsWith ("dw") || word.startsWith ("sp") ||
word.startsWith ("fl") || word.startsWith ("sq") ||
word.startsWith ("fr") || word.startsWith ("st") ||
word.startsWith ("gl") || word.startsWith ("sw") ||
word.startsWith ("gr") || word.startsWith ("th") ||
word.startsWith ("kl") || word.startsWith ("tr") ||
word.startsWith ("ph") || word.startsWith ("tw") ||
word.startsWith ("pl") || word.startsWith ("wh") ||
word.startsWith ("pr") || word.startsWith ("wr"));
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FahrenheitPanel extends JPanel
{
private JLabel inputLabel, outputLabel, resultLabel;
private JTextField fahrenheit;
//-----------------------------------------------------------------
// Constructor: Sets up the main GUI components.
//-----------------------------------------------------------------
public FahrenheitPanel()
{
inputLabel = new JLabel ("Enter Fahrenheit temperature:");
outputLabel = new JLabel ("Temperature in Celsius: ");
resultLabel = new JLabel ("---");
fahrenheit = new JTextField (5);
fahrenheit.addActionListener (new TempListener());
add (inputLabel);
add (fahrenheit);
add (outputLabel);
add (resultLabel);
setPreferredSize (new Dimension(300, 75));
setBackground (Color.yellow);
}
//*****************************************************************
// Represents an action listener for the temperature input field.
//*****************************************************************
private class TempListener implements ActionListener
{
//--------------------------------------------------------------
// Performs the conversion when the enter key is pressed in
// the text field.
//--------------------------------------------------------------
public void actionPerformed (ActionEvent event)
{
int fahrenheitTemp, celsiusTemp;
String text = fahrenheit.getText();
fahrenheitTemp = Integer.parseInt (text);
celsiusTemp = (fahrenheitTemp-32) * 5/9;
resultLabel.setText (Integer.toString (celsiusTemp));
}
}
}
“MyActionListener”在哪里?考虑提供一个[可运行的示例](https://stackoverflow.com/help/mcve),它可以证明你的问题。这不是代码转储,而是您正在做的事情的一个例子,它突出了您遇到的问题。这将导致更少的混淆和更好的反应 – MadProgrammer
华氏度可运行我说我没有把完整的代码,我会把它放在 – Sam
@Sam Fahreneit可以处理这个问题的事实是什么?检查我的答案......你错过了这里的课程:) –