JAVA:如何使用 JMenuItem 和 Jbutton 上的侦听器来计算费率

发布于 2024-10-07 07:16:06 字数 1054 浏览 12 评论 0原文

我正在编写一个货币转换器,但在计算每种货币的汇率时遇到了一些麻烦。基本上我希望用户首先选择一种货币,然后输入金额并按“开始”按钮来计算汇率。但我在 JMenuItem 和 JButton 上的侦听器上遇到了问题。我已经为 menuItem 和 JButton 声明了两个侦听器。如何使用按钮上的侦听器来查找在 menuIten 上所做的选择,以便进行正确的货币计算?

谢谢。

代码:

    private class selectionListener implements ActionListener
    {
        double EuroToSterling(double euro)
        {
            double total = Double.parseDouble(amountField.getText());
            return total;
        }
        public void actionPerformed(ActionEvent e)
        {
            if (e.getActionCommand().equals("Euros"))
               // result = EuroToSterling(10*euro);
                currencyMenu.setLabel("Euros");
               // answerLabel.setText("this" + EuroToSterling(1.22*2));

            if (e.getActionCommand().equals("Japanese Yen"))
                currencyMenu.setLabel("Japanese Yen");

        }
    }



    private class GoButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent evt)
        {

//please help with this section

I'm writing a currency converter but I'm having a bit of trouble caculating the exchange rate for each currency. basically I want the user to select a currecy first then enter an amount and press "go" button to calculate the rate. but i'm having trouble with the listeners on JMenuItem and JButton. I've declared two listeners for menuItem and JButton. how do i use the listener on the button to look out for the selection made on the menuIten so that it makes the right currecy calculation?

thanks.

CODE:

    private class selectionListener implements ActionListener
    {
        double EuroToSterling(double euro)
        {
            double total = Double.parseDouble(amountField.getText());
            return total;
        }
        public void actionPerformed(ActionEvent e)
        {
            if (e.getActionCommand().equals("Euros"))
               // result = EuroToSterling(10*euro);
                currencyMenu.setLabel("Euros");
               // answerLabel.setText("this" + EuroToSterling(1.22*2));

            if (e.getActionCommand().equals("Japanese Yen"))
                currencyMenu.setLabel("Japanese Yen");

        }
    }



    private class GoButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent evt)
        {

//please help with this section

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

初见你 2024-10-14 07:16:06

通常的方法是菜单侦听器更改应用程序的状态(即调用将汇率保存在字段中的方法)。

然后计算代码可以读取这个值并使用它。

The usual approach is that the menu listener changes the state of the application (i.e. calls a method that will save the exchange rate in a field).

Then the calculation code can read this value and use it.

枕头说它不想醒 2024-10-14 07:16:06

用欧元试试这个。应该给你一个开始的地方。


/*
 *
 * Currency converting
 *
 */

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;

import javax.swing.UIManager;

public class CurrencyConverterWin extends JFrame {

    private JLabel promptLabel;
    private JTextField amountField;
    private JButton goButton;
    private JPanel inputPanel;
    private JPanel answerPanel;
    private JLabel answerLabel;
    private JLabel selectLabel;
    private JComboBox currencyMenuBar;
    private JPanel menuPanel;
    private double result = 0.0;
    private double euro = 1.22257;
    private double japYen = 152.073;
    private double rusRuble = 42.5389;
    private double usd = 1.5577;

    public CurrencyConverterWin() {
        super();
        this.setSize(500, 200);
        this.setTitle("Currency Converter Window");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.setLayout(new GridLayout(3, 1));

        this.selectLabel = new JLabel("Select a currency to convert to: ", JLabel.RIGHT);
        this.answerLabel = new JLabel(" ", JLabel.CENTER);

        currencyMenuBar = new JComboBox(new String[]{"Euros","Japanese Yen","Russian Rubles","US Dollars"});

        this.menuPanel = new JPanel();
        this.menuPanel.add(this.selectLabel);
        this.menuPanel.add(this.currencyMenuBar);
        this.add(this.menuPanel);

        this.promptLabel = new JLabel("(select a currency first) ", JLabel.RIGHT);
        this.answerLabel = new JLabel(" ", JLabel.CENTER);

        this.amountField = new JTextField("", 8);
        this.goButton = new JButton("GO");
        goButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonClicked(evt);
            }
        });

        this.inputPanel = new JPanel();
        this.inputPanel.add(this.promptLabel);
        this.inputPanel.add(this.amountField);
        this.inputPanel.add(this.goButton);

        this.add(this.inputPanel);

        this.answerPanel = new JPanel();
        this.answerPanel.add(this.answerLabel);
        this.add(this.answerPanel);
    }

    double EuroToSterling() {
        double total = Double.parseDouble(amountField.getText()) * euro;
        return total;
    }

    double JapYenToSterling()
    {
        double japToSterlingTotal = Double.parseDouble(amountField.getText()) * japYen;
        return japToSterlingTotal;
    }


//String currencyEntered = yearField.getText();
    public void buttonClicked(ActionEvent evt) {
        if(currencyMenuBar.getSelectedItem().equals("Euros"))
        {
            answerLabel.setText(EuroToSterling() + " Euros");
        }
        if(currencyMenuBar.getSelectedItem().equals("Japanese Yen"))
        {
            answerLabel.setText(JapYenToSterling() + " Japanese Yen");
        }
    }

    public static void main(String[] args) {        
        try{UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");}
        catch (Exception e){e.printStackTrace();}
        CurrencyConverterWin win = new CurrencyConverterWin();
        win.setVisible(true);
    }
}

Try this out with the Euros. Should give you a place to get started.


/*
 *
 * Currency converting
 *
 */

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;

import javax.swing.UIManager;

public class CurrencyConverterWin extends JFrame {

    private JLabel promptLabel;
    private JTextField amountField;
    private JButton goButton;
    private JPanel inputPanel;
    private JPanel answerPanel;
    private JLabel answerLabel;
    private JLabel selectLabel;
    private JComboBox currencyMenuBar;
    private JPanel menuPanel;
    private double result = 0.0;
    private double euro = 1.22257;
    private double japYen = 152.073;
    private double rusRuble = 42.5389;
    private double usd = 1.5577;

    public CurrencyConverterWin() {
        super();
        this.setSize(500, 200);
        this.setTitle("Currency Converter Window");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.setLayout(new GridLayout(3, 1));

        this.selectLabel = new JLabel("Select a currency to convert to: ", JLabel.RIGHT);
        this.answerLabel = new JLabel(" ", JLabel.CENTER);

        currencyMenuBar = new JComboBox(new String[]{"Euros","Japanese Yen","Russian Rubles","US Dollars"});

        this.menuPanel = new JPanel();
        this.menuPanel.add(this.selectLabel);
        this.menuPanel.add(this.currencyMenuBar);
        this.add(this.menuPanel);

        this.promptLabel = new JLabel("(select a currency first) ", JLabel.RIGHT);
        this.answerLabel = new JLabel(" ", JLabel.CENTER);

        this.amountField = new JTextField("", 8);
        this.goButton = new JButton("GO");
        goButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonClicked(evt);
            }
        });

        this.inputPanel = new JPanel();
        this.inputPanel.add(this.promptLabel);
        this.inputPanel.add(this.amountField);
        this.inputPanel.add(this.goButton);

        this.add(this.inputPanel);

        this.answerPanel = new JPanel();
        this.answerPanel.add(this.answerLabel);
        this.add(this.answerPanel);
    }

    double EuroToSterling() {
        double total = Double.parseDouble(amountField.getText()) * euro;
        return total;
    }

    double JapYenToSterling()
    {
        double japToSterlingTotal = Double.parseDouble(amountField.getText()) * japYen;
        return japToSterlingTotal;
    }


//String currencyEntered = yearField.getText();
    public void buttonClicked(ActionEvent evt) {
        if(currencyMenuBar.getSelectedItem().equals("Euros"))
        {
            answerLabel.setText(EuroToSterling() + " Euros");
        }
        if(currencyMenuBar.getSelectedItem().equals("Japanese Yen"))
        {
            answerLabel.setText(JapYenToSterling() + " Japanese Yen");
        }
    }

    public static void main(String[] args) {        
        try{UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");}
        catch (Exception e){e.printStackTrace();}
        CurrencyConverterWin win = new CurrencyConverterWin();
        win.setVisible(true);
    }
}

掩耳倾听 2024-10-14 07:16:06

我还建议您使用 JComboBox 来存储货币。您将创建一个对象来存储货币名称和兑换率。然后,当您需要计算转换金额时,您可以从组合中获取所选项目,并在计算中使用其转换率。通过这种方法,您可以轻松扩展支持的货币数量。

查看:如何使用 Map 元素作为 JComboBox 的文本作为示例,让您开始在组合框中使用对象。

I would also suggest you use a JComboBox to store the currencies. You would create an object to store both the currency name and the conversion rate. Then when you need to calculate the converted amount you get the selected item from the combo and use its conversion rate in your calculation. With this approach you can easily expand the number of currencies you support.

Check out: How to use Map element as text of a JComboBox for an example to get you start on using an object in the combo box.

-残月青衣踏尘吟 2024-10-14 07:16:06

我个人会添加一个枚举来表示货币转换类型。例如:

public enum ConversionType {
   DOLLARS,
   EUROS,
   RUBLES
   //ETC...
}

使用这个,您可以在类中保留一个状态变量:

private ConversionType fromType;

这是您在选择侦听器中设置的内容。

从这里开始,就需要根据状态变量 (fromType) 在操作侦听器中进行不同的转换。像这样的事情:

if( fromType== EUROS ) {
 convertEurosToSterling( value1, value2 );
} 

这是一种通用方法 - 我希望这会有所帮助。

I would personally add in an Enumeration to denote the currency conversion type. eg:

public enum ConversionType {
   DOLLARS,
   EUROS,
   RUBLES
   //ETC...
}

Using this, you can keep a state variable in the class:

private ConversionType fromType;

This is what you set in your selection listener.

From there it's a matter of doing the different conversions in your action listener based on the state variable (fromType). Something like this:

if( fromType== EUROS ) {
 convertEurosToSterling( value1, value2 );
} 

This is sort of a general approach - I hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文