晚上好,我还在继续我的项目(努力提高自己。)
我有一个程序将钱转换为另一种货币类型(如欧元兑美元),我有我的菜单,我自己设定费率。它设置了很好的速率但是当我尝试转换钱时,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不会影响代码?
答案 0 :(得分:0)
它不起作用,因为您在单击“Rate setters”菜单时在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类基本上根据您调用的costructor构建两个不同的窗口。为每个窗口创建一个单独的类,一个用于主窗口,另一个用于测试内容......