距离转换器问题

时间:2019-05-07 18:12:38

标签: java

由于未知的逻辑错误,我无法获得准确的转换。该程序具有三个输入字段。用户在所需字段中输入所需值的位置。根据输入项和输入项字段来转换数据。

package dist;
import java.awt.*;          
import java.awt.event.*;    
import javax.swing.*;   
@SuppressWarnings("serial")
public class DistanceConvertor extends JFrame
{  
    private JLabel KilometreLabel;      
    private JLabel MilesLabel;
private JLabel NauticalMilesLabel;

private JTextField KilometreTF;     
private JTextField MilesTF; 
private JTextField NauticalMilesTF;
private kiloHandler KilometreHandler;
private nauticalHandler NauticalMilesHandler;
private milesHandler MilesHandler;
private static final int WIDTH = 400;
private static final int HEIGHT = 100;   
private static final double OFFSET = 0.0621;
private static final double OFFSET1 = 0.5399568;

public DistanceConvertor()
{
    setTitle("Distance Convertor");
    Container c = getContentPane();
    c.setLayout(new GridLayout(3,4));

    KilometreLabel = new JLabel("Distance in Kilometre:\n ",
                                        SwingConstants.RIGHT);
    MilesLabel = new JLabel("Distance in Miles: ",
                                        SwingConstants.RIGHT);
    NauticalMilesLabel = new JLabel("Distance in Nautical Miles: ",
            SwingConstants.RIGHT);
    KilometreTF = new JTextField(7);
    MilesTF = new JTextField(7);
    NauticalMilesTF = new JTextField(7);
    c.add(KilometreLabel);
    c.add(KilometreTF);
    c.add(MilesLabel);
    c.add(MilesTF);
    c.add(NauticalMilesLabel);
    c.add(NauticalMilesTF);
    KilometreHandler = new kiloHandler();
    MilesHandler = new milesHandler();
    NauticalMilesHandler=new nauticalHandler();

    KilometreTF.addActionListener(KilometreHandler);
    MilesTF.addActionListener(MilesHandler);
    NauticalMilesTF.addActionListener(NauticalMilesHandler);
    setSize (WIDTH, HEIGHT);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

private class kiloHandler implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        double kilometre, miles,nauticalmiles;

        kilometre =
                Double.parseDouble(KilometreTF.getText());
        nauticalmiles =
                Double.parseDouble(NauticalMilesTF.getText());
        miles = kilometre *  OFFSET;

        MilesTF.setText(String.format("%.4f", miles));
    }
}

private class milesHandler implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        double kilometres, miles;
        miles = 
                Double.parseDouble(MilesTF.getText());
        kilometres = (miles / OFFSET) ;
        KilometreTF.setText(String.format("%.4f", kilometres));
    }
}
private class nauticalHandler implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        double kilometres, miles,nauticalmiles;
        kilometres = 
                Double.parseDouble(KilometreTF.getText());
         nauticalmiles= (kilometres / OFFSET1) ;
        NauticalMilesTF.setText(String.format("%.4f", nauticalmiles));
    }
}  

public static void main(String[] args)
{
    DistanceConvertor DistConv = new DistanceConvertor();
    DistConv.setLocationRelativeTo(null);
}
}

当我在三个字段中的任何一个中输入任何值时,我想获得转换值。但是由于某些错误,我无法获得转换后的值。

0 个答案:

没有答案