-2

该程序应该转换用户输入的英里数并使用 GUI 界面转换为公里数。程序当前无法编译。

aaaaa.java:10:错误:方法声明无效;需要返回类型 public CalcPanel() { ^ 1 错误

无论我在哪里寻找此类程序的示例,我都看不到该方法的返回类型。有任何想法吗?谢谢

这是程序:

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

public class aaaaa extends JFrame {
private JLabel messageLabel= new JLabel("Enter distance in miles");
private JTextField milesTextField = new JTextField(10);;
private JButton calcButton = new JButton("Calculate"); 

public CalcPanel() {
    JPanel panel = new JPanel();
    panel.add(messageLabel);
    panel.add(milesTextField);
    panel.add(calcButton);

    calcButton.addActionListener(new CalcButtonListener());
}

public static void main(String[] args) {
        JFrame frame = new CalcPanel();
        frame.setTitle("M to Km Converter");
        frame.setLocationRelativeTo(null);
        frame.setSize(300, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
}

class CalcButtonListener implements ActionListener {        public void actionPerformed(ActionEvent e) {
        String input = milesTextField.getText();
        double kilometers = Double.parseDouble(input) * 1.6023;
        JOptionPane.showMessageDialog(null, input + " miles is " + kilometers + " kilometers.");
    }
}
}

所以这个问题已经解决了,但这是另一个问题。面板弹出打开,但我的 messageLabel milesTextField calcButton 没有出现。

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

    public class Aaaa extends JFrame {
    private JPanel panel;
    private JLabel messageLabel= new JLabel("Enter distance in miles");
    private JTextField milesTextField = new JTextField(10);;
    private JButton calcButton = new JButton("Calculate"); 

    public Aaaa() {
        JPanel panel = new JPanel();
        panel.add(messageLabel);
        panel.add(milesTextField);
        panel.add(calcButton);
        setVisible(true);
        calcButton.addActionListener(new CalcButtonListener());
    }

    public static void main(String[] args) {
            JFrame frame = new Aaaa();
            frame.setTitle("M to Km Converter");
            frame.setLocationRelativeTo(null);
            frame.setSize(300, 150);
    }

    class CalcButtonListener implements ActionListener {        public void actionPerformed(ActionEvent e) {
            String input = milesTextField.getText();
            double kilometers = Double.parseDouble(input) * 1.6023;
            JOptionPane.showMessageDialog(null, input + " miles is " + kilometers + " kilometers.");
        }
    }
}
4

2 回答 2

2

您正在尝试创建一个构造函数,但构造函数的名称与类的名称不匹配。

改为public CalcPanel改为public aaaaaa

于 2013-06-26T00:50:12.683 回答
1

似乎是拼写错误,类和构造函数名称不匹配。你的类名是 aaaaa 而你的构造函数名是 CalcPanel。

于 2013-06-26T00:52:06.293 回答