我正在为各个城市之间的距离创建转换计算器。但我遇到了如何从转换方法调用计算的问题。比如这里:
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): ");
}
}
}
这部分是2D数组选择值的地方,我的目的是在点击单选按钮时进行从英里到KM的计算。我绘制的文件默认以英里为单位。
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中。
谢谢,
答案 0 :(得分:2)
由于您的方法是静态的,您可以通过Classname.methodName();
如果你想存储在String中,你可以这样做:
String str = String.valueOf(MetricConverter.kilometersToMiles( kilometers));
答案 1 :(得分:1)
想象一下,您知道如何调用转换方法。请考虑以下情况:
问题是您在distanceText
中编写的文本取决于两种不同的输入操作:设置城市和设置显示类型。因此,在设置文本时需要所有这些信息。
假设您的事件处理程序被声明为内部类,我建议为该数据,相应的setter和“更新文本”方法提供一些字段:
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);
}
}
答案 2 :(得分:0)
只是:
distanceText.setText(String.valueOf(MetricConverter.milesToKilometers(fileData[selectedRow + 1][selectedCol + 1])));
或里程:
distanceText.setText(String.valueOf(MetricConverter.kilometersToMiles(fileData[selectedRow + 1][selectedCol + 1])));
当然,只有当fileData包含双精度值时才有效。