2015-09-28 52 views
0

「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)); 
     } 
    } 
} 
+6

「MyActionListener」在哪裏?考慮提供一個[可運行的示例](https://stackoverflow.com/help/mcve),它可以證明你的問題。這不是代碼轉儲,而是您正在做的事情的一個例子,它突出了您遇到的問題。這將導致更少的混淆和更好的反應 – MadProgrammer

+0

華氏度可運行我說我沒有把完整的代碼,我會把它放在 – Sam

+0

@Sam Fahreneit可以處理這個問題的事實是什麼?檢查我的答案......你錯過了這裏的課程:) –

回答

0

MyActionListener()不是一個內部類的PigLatinTranslator,那是你的問題(不存在)。

如果你把你的class放在另一個文件中,提供它,但基本上,如果你只使用這個代碼,你面臨的問題是,你還沒有創建MyActionListener()類。

你知道該怎麼做,我沒有提供代碼。

0

我知道了。

import java.util.Scanner; 
import java.awt.*; 
import java.awt.event.*; 

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 LangListener()); 

    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")); 
    } 


    private class LangListener implements ActionListener 
    { 
     public void actionPerformed (ActionEvent event) 
     { 
       String text = piglatin.getText(); 

       resultLabel.setText(translate(text)); 
    } 
}} 



import java.util.Scanner; 


import javax.swing.JFrame; 

public class PigLatin 
{ 


    //----------------------------------------------------------------- 
    // Reads sentences and translates them into Pig Latin. 
//----------------------------------------------------------------- 
    public static void main (String[] args) 
    { 
     JFrame frame = new JFrame ("Pig Latin"); 
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 

      PigLatinTranslator panel = new PigLatinTranslator(); 

      frame.getContentPane().add(panel); 
      frame.pack(); 
      frame.setVisible(true); 
    } 
    { 
      String input, result, another; 

      Scanner scan = new Scanner (System.in); 

      do 
      { 
      System.out.println(); 
      System.out.println ("Enter a sentence (no punctuation):"); 
      input = scan.nextLine(); 

      System.out.println(); 
      result = PigLatinTranslator.translate (input); 
      System.out.println ("That sentence in Pig Latin is:"); 
      System.out.println (result); 

      System.out.println(); 
      System.out.print ("Translate another sentence (y/n)? "); 
      another = scan.nextLine(); 
      } 
      while (another.equalsIgnoreCase("y")); 
     } 



}