即时制作货币计算器,我遇到的问题是无法将计算结果设置到结果JTextField中,这是对象otherTextField。
这是我的班级:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ValutaKalkulator implements ActionListener {
private double nokValue;
private double otherValue;
private JButton buttonRemoveNok;
private JButton removeOther;
private JButton removeBoth;
private JButton exitButton;
private JButton usdButton;
private JButton sekButton;
private JButton gbpButton;
private JButton eurButton;
private JLabel nokLabel;
private JTextField nokTextField;
private JLabel otherLabel;
private JTextField otherTextField;
public ValutaKalkulator()
{
JFrame frame = new JFrame();
frame.setTitle("VALUTAKALKULATOR");
buttonRemoveNok = new JButton("Fjern NOK");
removeOther = new JButton("Fjern annen valuta");
removeBoth = new JButton("Fjern begge");
exitButton = new JButton("Avslutt");
usdButton = new JButton("USD");
sekButton = new JButton("SEK");
gbpButton = new JButton("GBP");
eurButton = new JButton("EUR");
nokLabel = new JLabel("NOK");
JTextField nokTextField = new JTextField();
otherLabel = new JLabel("Annen valuta");
otherTextField = new JTextField();
buttonRemoveNok.addActionListener(this);
removeOther.addActionListener(this);
removeBoth.addActionListener(this);
exitButton.addActionListener(this);
usdButton.addActionListener(this);
sekButton.addActionListener(this);
gbpButton.addActionListener(this);
eurButton.addActionListener(this);
JPanel pnlSouth = new JPanel(new GridLayout(1, 4));
JPanel pnlCenter = new JPanel(new GridLayout(2, 2));
JPanel pnlNorth = new JPanel(new GridLayout(1, 4));
pnlNorth.add(nokLabel);
pnlNorth.add(nokTextField);
pnlNorth.add(otherLabel);
pnlNorth.add(otherTextField);
pnlCenter.add(gbpButton);
pnlCenter.add(eurButton);
pnlCenter.add(usdButton);
pnlCenter.add(sekButton);
pnlSouth.add(buttonRemoveNok);
pnlSouth.add(removeOther);
pnlSouth.add(removeBoth);
pnlSouth.add(exitButton);
frame.add(pnlNorth, BorderLayout.NORTH);
frame.add(pnlSouth, BorderLayout.SOUTH);
frame.add(pnlCenter, BorderLayout.CENTER);
frame.setVisible(true);
frame.pack();
}
public String calculateFromNok(String nokValueString, String toValue)
{
double result = 0;
double nokValue = Double.valueOf(nokValueString);
switch(toValue)
{
case "GBP":
result = nokValue * 10.8980 / 100;
break;
case "EUR":
result = nokValue * 9.2450 / 100;
break;
case "USD":
result = nokValue * 8.5223 / 100;
break;
case "SEK":
result = nokValue * 96.48 / 100;
break;
}
String resultString = Double.toString(result);
return resultString;
}
public void actionPerformed(ActionEvent event)
{
String text = event.getActionCommand();
switch(text)
{
case "USD":
String txt = nokTextField.getText();
String txt2 = calculateFromNok(txt, "USD");
otherTextField.setText(txt2);
break;
}
}
}
我知道货币不正确,还有其他逻辑缺陷和错误的变量名称,但我现在的问题是为什么我不能将方法calculateFromNok的结果放入我的JTextField otherTextField?
帮助赞赏thx!
答案 0 :(得分:1)
您正在创建初始化本地变量JTextField nokTextField
所以private JTextField nokTextField;
没有初始化。这就是为什么它会给你nullpointerexception。
只需更改第37行:
JTextField nokTextField = new JTextField();
到
nokTextField = new JTextField();
答案 1 :(得分:0)
您的问题在于:
JTextField nokTextField = new JTextField();
将其更改为:
nokTextField = new JTextField();
发生这种情况的原因是您之前在顶部定义了nokTextField,然后在构造函数中初始化了一个具有相同名称的单独变量。使用Eclipse之类的IDE可以很容易地捕获这些类型的错误。我建议你用一个。