0

我正在为各个城市之间的距离创建一个转换计算器。但是我在如何从转换方法调用计算时遇到问题。比如这里:

private class ComboBoxListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        int selectedRow = destinationBox.getSelectedIndex();
        int selectedCol = originBox.getSelectedIndex();
        distanceText.setText(fileData[selectedRow + 1][selectedCol + 1]);
    }
}

private class RadioButtonListener implements ActionListener
{

    public void actionPerformed (ActionEvent event)
    {
        if (event.getSource() == kilometersRadioButton)
        {
            distanceLabel.setText("Distance (kilometers):");
            avgspeedLabel.setText("Avg. Speeg (kph): ");  
        }
        else if (event.getSource() == milesRadioButton)
        {
            distanceLabel.setText("Distance (miles): ");
            avgspeedLabel.setText("Avg. Speeg (mph): ");
        }
    }
}

这部分是二维数组选择值的地方,我的目的是在单击单选按钮时进行从英里到公里的计算。我从中绘制的文件默认以英里为单位。

public class MetricConverter
{

    public static double milesToKilometers(double miles) //This static method holds the miles to kilometers conversion equation
    {
        return miles * 1.609;
    }

    public static double kilometersToMiles(double kilometers) //This static method holds the kilometers to miles conversion equation
    {
        return kilometers / 1.609;
    }
}

这是转换方法。我如何称呼它们,因为数组的值在 TextField/String 中。

谢谢你,

4

3 回答 3

2

由于您的方法是静态的,您可以直接调用Classname.methodName();

如果要存储在 String 中,可以执行以下操作:

String str = String.valueOf(MetricConverter.kilometersToMiles( kilometers));
于 2013-04-12T10:11:48.000 回答
1

想象一下,您知道如何调用转换方法。考虑这种情况:

  1. 您选择两个城市。距离以英里为单位。
  2. 您选择公里。距离以公里为单位。
  3. 你改变了其中一个城市。距离以英里为单位显示,即使选择了单选按钮“公里”。

问题是您输入的文本distanceText取决于两种不同的输入操作:设置城市和设置显示类型。因此,您在设置文本时需要所有这些信息。

假设您的事件处理程序被声明为内部类,我建议该数据的一些字段、相应的设置器和“更新文本”方法:

private int row, column;
private boolean inKm;

private void setCities(int row, int column) {
    this.row = row;
    this.column = column;
}

private void setKm(boolean inKm) {
    this.inKm = inKm;
}

// one place to set the distance text
private void updateDistanceText() {
    String text = fileData[row+1][column+1];
    if (inKm) {
        text = String.valueOf(
                   MetricConverter.milesToKilometers(Double.valueOf(text)));
    }
    distanceText.setText(text);        
}

最后,更新您的事件处理程序:

private class ComboBoxListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        setCities(destinationBox.getSelectedIndex(), 
                  originBox.getSelectedIndex());
        updateDistanceText();
    }
}

private class RadioButtonListener implements ActionListener {
    public void actionPerformed (ActionEvent event) {
        if (event.getSource() == kilometersRadioButton) {
            distanceLabel.setText("Distance (kilometers):");
            avgspeedLabel.setText("Avg. Speed (kph): ");
            setKm(true);
        } else if (event.getSource() == milesRadioButton) {
            distanceLabel.setText("Distance (miles): ");
            avgspeedLabel.setText("Avg. Speed (mph): ");
            setKm(false);
        }
        updateDistanceText();
    }
}

编辑如果你不想使用你还没有学过的技术,你可以使用一些复制粘贴......一般来说,在多个地方拥有相同的代码是不好的做法,因为改变它是一种痛苦,但是对于说明目的,无论如何我都会这样做:

private class ComboBoxListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        int selectedRow = destinationBox.getSelectedIndex();
        int selectedCol = originBox.getSelectedIndex();

        String text = fileData[selectedRow+1][selectedColumn+1];

        if (kilometersRadioButton.isSelected()) {
            text = String.valueOf(
                     MetricConverter.milesToKilometers(Double.valueOf(text)));
        }

        distanceText.setText(text);        
    }
}

private class RadioButtonListener implements ActionListener {
    public void actionPerformed (ActionEvent event) {
        int selectedRow = destinationBox.getSelectedIndex();
        int selectedCol = originBox.getSelectedIndex();

        String text = fileData[selectedRow+1][selectedColumn+1];

        if (event.getSource() == kilometersRadioButton) {
            distanceLabel.setText("Distance (kilometers):");
            avgspeedLabel.setText("Avg. Speeg (kph): ");  

            text = String.valueOf(
                     MetricConverter.milesToKilometers(Double.valueOf(text)));
        } else if (event.getSource() == milesRadioButton) {
            distanceLabel.setText("Distance (miles): ");
            avgspeedLabel.setText("Avg. Speeg (mph): ");
        }

        distanceText.setText(text);
    }
}
于 2013-04-12T10:25:59.437 回答
0

只是:

distanceText.setText(String.valueOf(MetricConverter.milesToKilometers(fileData[selectedRow + 1][selectedCol + 1])));

或英里:

distanceText.setText(String.valueOf(MetricConverter.kilometersToMiles(fileData[selectedRow + 1][selectedCol + 1])));

这当然只有在 fileData 包含双值时才有效。

于 2013-04-12T10:12:54.503 回答