0

试图在 JAVA 中向已制作的程序添加按钮。它将温度从华氏温度转换为摄氏度。我的按钮不会出现。我错过了一些东西。这个想法是您将能够同时按下回车键或按钮来获得结果。该程序的主要部分是:

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

2 回答 2

1

该按钮不会显示,因为ActionListener代码没有编译为

  • ButtonListener需要一个actionPerformed方法
  • TempListener是的内部类,ButtonListener但被引用为外部类

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

push.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
      ...
    }
});
于 2013-10-30T19:47:30.813 回答
1

你需要ButtonListener什么TempListener

//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));
        }
    });
    ...
于 2013-10-30T21:04:24.300 回答