「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可以處理這個問題的事實是什麼?檢查我的答案......你錯過了這裏的課程:) –