-1

晚上好,我还在做我的项目(努力提高自己。)

我有一个程序可以将货币转换为另一种货币类型(例如欧元到美元),我有我的菜单,我自己设置汇率。它很好地设置了汇率,但是当我尝试兑换货币时,setRate 不会影响它,结果是 NaN 或 Infinity。这是我的整个代码。

    private JMenuBar menuBar = new JMenuBar(); // Window menu bar
    public Converter(String title) {

        setTitle(title);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setJMenuBar(menuBar); // Add the menu bar to the window
        JMenu fileMenu = new JMenu("File"); // Create File menu
        JMenu elementMenu = new JMenu("Rate Setters"); // Create Elements menu
        JMenuItem subTest = new JMenuItem("Test");
        JMenuItem rateMyMoney = new JMenuItem("Set the Rates.");
        subTest.addActionListener(new ActionListener(){     
            public void actionPerformed(ActionEvent actionEvent){
                Converter convert = new Converter();

                GridLayout converterGridLayout = new GridLayout(2,2,12,6);
                convert.setLayout(converterGridLayout);
                convert.setVisible(true);
            }
        });

        rateMyMoney.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent actionEvent){

                SetMyRates setRates = new SetMyRates();

                GridLayout setRateGridLayout = new GridLayout(2,2,12,6);
                setRates.setLayout(setRateGridLayout);
                setRates.setVisible(true);
            }
        });


        menuBar.add(fileMenu); // Add the file menu
        menuBar.add(elementMenu); // Add the element menu
        fileMenu.add(subTest);
        elementMenu.add(rateMyMoney);
    }
    public Converter() {

        JPanel dataPanel = new JPanel(new GridLayout(2, 2, 12, 6));
        dataPanel.add(tlLabel);

        dataPanel.add(dollarsLabel);
        dataPanel.add(tlField);
        dataPanel.add(dollarsField);
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(tlButton);
        buttonPanel.add(dollarsButton);
        Container container = this.getContentPane();
        container.add(dataPanel, BorderLayout.CENTER);
        container.add(buttonPanel, BorderLayout.SOUTH);
        tlButton.addActionListener(new TLConverter());
        dollarsButton.addActionListener(new DollarsConverter());
        buttonPanel.add(setRates);
        pack();

    }
public class SetMyRates extends JFrame{ //extends Converter{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public SetMyRates(){

        JPanel dataPanel = new JPanel(new GridLayout(2, 2, 12, 6));
        dataPanel.add(setTLLabel);

        dataPanel.add(setDollarsLabel);
        dataPanel.add(setTLField);
        dataPanel.add(setDollarsField);
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(setTLButton);
        buttonPanel.add(setDollarsButton);
        Container container = this.getContentPane();
        container.add(dataPanel, BorderLayout.CENTER);
        container.add(buttonPanel, BorderLayout.SOUTH);
        setTLButton.addActionListener(new SetTL());
        setDollarsButton.addActionListener(new SetDollars());
        pack();

    }
};
    private class SetTL implements ActionListener{

        public void actionPerformed(ActionEvent  e)
        {
            try
            {
                String input = setTLField.getText();
                System.out.println("text = " + input);
                double tl = Double.parseDouble(input);
                convertMe.setRate(tl);
            }
            catch(Exception ex)
            {
                JOptionPane.showMessageDialog(null, "Please enter correct Rate.");
            }
        }
    }
    private class SetDollars implements ActionListener{

        public void actionPerformed(ActionEvent  e)
        {
            try
            {
                String input = setDollarsField.getText();
                System.out.println("text = " + input);
                double setDolarRate = Double.parseDouble(input);
                convertMe.setRate(setDolarRate);
            }
            catch(Exception ex)
            {
                JOptionPane.showMessageDialog(null, "Please enter correct Rate.");
            }
        }
    }

    private class TLConverter implements ActionListener
    {
        public void actionPerformed(ActionEvent  e)
        {
            try
            {
                String input = tlField.getText();
                double tl = Double.parseDouble(input);
                convertMe.setTL(tl);

                double dollars = convertMe.getDollars();
                dollarsField.setText(String.format("%.2f", dollars));
            }
            catch(Exception ex)
            {
                JOptionPane.showMessageDialog(null, "Please enter the amount that will be converted.");
            }
        }
    }


    private class DollarsConverter implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            String input = dollarsField.getText();
            double dollars = Double.parseDouble(input);
            convertMe.setDollars(dollars);
            double tl = convertMe.getTL();
            tlField.setText(String.format("%.2f", tl));
        }
    }

钱.java

public class MoneyDetails {

    public double moneyAmount;
    public double myRate;
    public double guaranteeMyRate;



    public double setRate (double myRate){


        System.out.println("myrate = " +myRate);

        this.myRate = myRate;
        System.out.println("this.myrate = " + this.myRate);

        this.guaranteeMyRate = this.myRate;

        System.out.println("garanti = " + guaranteeMyRate);
        return this.myRate;
    }

    public void setTL(double turkishLira) {
System.out.println("setTL = " + turkishLira);
        moneyAmount = turkishLira;
    }

    public void setDollars(double dollars) {
        System.out.println("setDolar = " + dollars);

        moneyAmount = dollars;
    }

    public double getDollars() {

        System.out.println("garanti getdolar = " + guaranteeMyRate);
        return moneyAmount / guaranteeMyRate;
    }

    public double getTL() {

        System.out.println("garanti getTL = " + guaranteeMyRate);

        return moneyAmount * guaranteeMyRate;
    }
}

为什么我尝试转换时 setRate 不影响代码?

4

1 回答 1

0

它不起作用,因为您在单击“Rate setters menu”时在 Converter 对象 convertMe 字段上设置了转换率。

然后,当您单击测试菜单时,您将创建另一个 Converter 实例,您从未为其设置转换率。

解决问题的最快和最肮脏的方法是在对象创建后设置 convertMe 字段:

        subTest.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent actionEvent){
                Converter convert = new Converter();
                convert.convertMe=Converter.this.convertMe;
                GridLayout converterGridLayout = new GridLayout(2,2,12,6);
                convert.setLayout(converterGridLayout);
                convert.setVisible(true);
            }
        });

但我强烈建议重新设计你的 UI 代码。您的 Converter 类基本上根据您调用的构造函数构建两个不同的窗口。为每个窗口创建一个单独的类,一个用于主窗口,另一个用于测试内容......

于 2012-05-02T20:39:38.387 回答