Java:JmenuitemとJbuttonでリスナーを使用してレートを計算する方法
-
10-10-2019 - |
質問
私は通貨コンバーターを書いていますが、各通貨の為替レートを少し抑制するのに少し苦労しています。基本的に、ユーザーに最初にカレーを選択し、金額を入力して「GO」ボタンを押してレートを計算してもらいたいです。しかし、私はJmenuitemとJbuttonのリスナーと問題を抱えています。 MenuitemとJbuttonの2人のリスナーを宣言しました。ボタン上のリスナーを使用して、Menuitenで行われた選択に注意して、適切なCurrecy計算を行うにはどうすればよいですか?
ありがとう。
コード:
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
解決
ユーロでこれを試してみてください。あなたに始める場所を与えるべきです。
/*
*
* 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);
}
}
他のヒント
通常のアプローチは、メニューリスナーがアプリケーションの状態を変更することです(つまり、フィールドの為替レートを節約するメソッドを呼び出します)。
計算コードはこの値を読み取り、それを使用できます。
また、jcomboboxを使用して通貨を保存することをお勧めします。通貨名と変換率の両方を保存するオブジェクトを作成します。次に、変換された量を計算する必要がある場合、選択したアイテムをコンボから取得し、計算でその変換率を使用します。このアプローチを使用すると、サポートする通貨の数を簡単に拡大できます。
チェックアウト: マップ要素をjcomboboxのテキストとして使用する方法 例として、コンボボックスでオブジェクトの使用を開始します。
私は個人的に列挙を追加して、通貨変換の種類を示すものです。例えば:
public enum ConversionType {
DOLLARS,
EUROS,
RUBLES
//ETC...
}
これを使用して、クラス内の状態変数を保持できます。
private ConversionType fromType;
これは、選択リスナーに設定したものです。
そこから、状態変数(fromType)に基づいて、アクションリスナーで異なる変換を行うことが問題です。このようなもの:
if( fromType== EUROS ) {
convertEurosToSterling( value1, value2 );
}
これは一種の一般的なアプローチです - これが役立つことを願っています。