Java按钮和侦听器无法同步工作

时间:2013-10-30 19:39:33

标签: java swing

尝试在JAVA中为已经制作的程序添加一个按钮。它将温度从华氏温度转换为摄氏温度。我的按钮不会显示。我错过了什么。我们的想法是,您既可以按Enter键,也可以按按钮获得结果。该计划的主要部分是:

import javax.swing.JFrame;

public class Fahrenheit
{
    public static void main (String[] args)
    {
        JFrame frame = new JFrame ("Fahrenheit");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        FahrenheitPanel panel = new FahrenheitPanel();
        frame.getContentPane().add(panel);
        frame.pack();
        frame.setVisible(true);

        frame.getContentPane().add(panel);
        frame.pack();
        frame.setVisible(true);
    }
}

然后在另一个文件中:

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

public class FahrenheitPanel extends JPanel
{
    private JLabel inputLabel, outputLabel, resultLabel;
    private JButton push;
    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);

        //Here's some button code
        push = new JButton ("Push!!!");
        push.addActionListener (new ButtonListener());
        add (push);

        setPreferredSize (new Dimension(300, 75));
        setBackground (Color.red);
    }

    private class ButtonListener implements ActionListener
    {

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

2 个答案:

答案 0 :(得分:1)

该按钮不会显示,因为ActionListener代码无法编译为

  • ButtonListener需要actionPerformed方法
  • TempListenerButtonListener的内部类,但被引用为外部类

可以使用匿名ActionListeners来简化代码,例如

push.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
      ...
    }
});

答案 1 :(得分:1)

您需要什么ButtonListenerTempListener ??

//Here's some button code
    push = new JButton ("Push!!!");
    add (push);

    push.addActionListener (new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            int fahrenheitTemp, celsiusTemp;
            String text = fahrenheit.getText();
            fahrenheitTemp = Integer.parseInt (text);
            celsiusTemp = (fahrenheitTemp-32) * 5/9;
            resultLabel.setText (Integer.toString (celsiusTemp));
        }
    });

fahrenheit

    ...
    add (fahrenheit);
    fahrenheit.addActionListener (new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            int fahrenheitTemp, celsiusTemp;
            String text = fahrenheit.getText();
            fahrenheitTemp = Integer.parseInt (text);
            celsiusTemp = (fahrenheitTemp-32) * 5/9;
            resultLabel.setText (Integer.toString (celsiusTemp));
        }
    });
    ...