こんばんは、私はまだ自分のプロジェクトに取り組んでいます(自分自身をさらに改善しようとしています.)
お金を別の種類の通貨 (ユーロからドルなど) に変換するプログラムがあり、メニューがあり、料金を自分で設定しています。レートは非常にうまく設定されますが、お金を変換しようとすると、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));
}
}
Money.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 がコードに影響しないのはなぜですか?