我正在写一个货币转换器,但我在计算每种货币的汇率方面遇到了一些麻烦。基本上我希望用户先选择一个货币,然后输入一个金额,然后按“开始”按钮计算费率。但是我在JMenuItem和JButton的听众遇到了麻烦。我已经为menuItem和JButton声明了两个监听器。我如何使用按钮上的监听器来查看menuIten上的选择,以便进行正确的计算?
感谢。
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
答案 0 :(得分:3)
通常的方法是菜单监听器改变应用程序的状态(即调用一个方法来保存字段中的汇率)。
然后计算代码可以读取该值并使用它。
答案 1 :(得分:1)
用欧元试试吧。应该给你一个入门的地方。
/*
*
* 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);
}
}
答案 2 :(得分:0)
我个人会添加一个Enumeration来表示货币转换类型。例如:
public enum ConversionType {
DOLLARS,
EUROS,
RUBLES
//ETC...
}
使用此功能,您可以在类中保留状态变量:
private ConversionType fromType;
这是您在选择侦听器中设置的内容。
从那里开始,基于状态变量(fromType)在动作监听器中进行不同的转换。像这样:
if( fromType== EUROS ) {
convertEurosToSterling( value1, value2 );
}
这是一种通用方法 - 我希望这会有所帮助。
答案 3 :(得分:0)
我还建议您使用JComboBox来存储货币。您将创建一个对象来存储货币名称和转换率。然后,当您需要计算转换金额时,您将从组合中获取所选项目并在计算中使用其转换率。使用这种方法,您可以轻松扩展您支持的货币数量。
查看:How to use Map element as text of a JComboBox以获取一个示例,让您开始使用组合框中的对象。