2013-05-16 5 views
0

Я работаю над простым графическим интерфейсом Java, но при использовании этого метода возникла ошибка addActionListener из JButton. Вот мои коды. Ошибка отмечена ERROR.Ошибка JButton не может быть применена к заданным типам

import javax.swing.*; 
import java.awt.event.*; 

public class KiloConverter extends JFrame { 

    private JPanel panel;     //To reference a panel 
    private JLabel messageLabel;   //To reference a label 
    private JTextField kiloTextField;  //To reference a text field 
    private JButton calcButton;    //To reference a button 
    private final int WINDOW_WIDTH = 310; //Window width 
    private final int WINDOW_HEIGHT = 100; //Window height 

    public KiloConverter() { 

     setTitle("Kilometer Converter");  //Set the window title 
     setSize(WINDOW_WIDTH, WINDOW_HEIGHT); //Set the size of the window 

     //Specify what happens when the close button is clicked 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     buildPanel();       //Build panel and add to frame 
     add(panel);        //Add panel to content pane 
     setVisible(true);      //Display the window 
    } 

    private void buildPanel() { 

     messageLabel = new JLabel("Enter a distance in kilometers"); 
     kiloTextField = new JTextField(10); 
     calcButton = new JButton("Calculate"); 

     //ERROR - method addActionListener in class AbstractButton cannot be 
     //applied to given types 
     //reason: actual argument KiloConverter.CalcButtonListener cannot be 
     //converted to ActionListener by method invocation conversion. 
     calcButton.addActionListener(new CalcButtonListener()); 



     panel = new JPanel(); 

     panel.add(messageLabel); 
     panel.add(kiloTextField); 
     panel.add(calcButton); 
    } 

    private class CalcButtonListener implements ActionListener { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      String input; 
      double miles; 

      input = kiloTextField.getText(); 
      miles = Double.parseDouble(input) * 0.6214; 

      JOptionPane.showMessageDialog(null, input + "kilometers is " + 
        miles + " miles."); 
     } 
    } 

    public static void main(String[] args) { 
     new KiloConverter(); 
    } 
} 

Интерфейс класса:

import java.awt.event.ActionEvent; 

public interface ActionListener { 

    public void actionPerformed(ActionEvent e); 
} 
+0

Вы повторно реализовали интерфейс ActionListener? 'JButton' ожидает экземпляр' java.awt.ActionListener' – MadProgrammer

+0

'private final int WINDOW_WIDTH = 310;' Не устанавливайте размер контейнеров верхнего уровня. Вместо этого разместите контент и вызовите 'pack()'. –

+0

Что вы подразумеваете под обновлением интерфейса ActionListener? @AndrewThompson О, хорошо, я буду помнить об этом. – Pclef

ответ

1

От звука ваших комментариев, вы создали свой собственный interface называется ActionListener.

JButton требует экземпляра java.awt.ActionListener.

Попробуйте удалить и внедрить их в свой прием. java.awt.ActionListener внутри ваших собственных слушателей.

Смежные вопросы