为什么JTextField的setText在actionPerformed方法中不起作用?
即时制作一个货币计算器,我有一个问题,无法将计算结果设置为结果JTextField,这是对象otherTextField。为什么JTextField的setText在actionPerformed方法中不起作用?
这里是我的类:
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!
您正在创建初始化局部变量JTextField nokTextField
。
因此private JTextField nokTextField;
未初始化。这就是为什么它给你nullpointerexception。
只是改变行号37:
JTextField nokTextField = new JTextField();
要
nokTextField = new JTextField();
耶稣基督,我怎么可能是这个盲人。你看,我首先在构造函数的本地实例化了所有的字段,然后将它们移出,所以它在那里,我一定忘记了解决这个问题。解决了。谢谢队友:) –
如果它解决了你的问题,你可以接受我的答案,以便其他用户可能会觉得它有用 –
你的问题是与线:
JTextField nokTextField = new JTextField();
将其更改为:
nokTextField = new JTextField();
发生这种情况的原因是,您之前在顶部定义了nokTextField,然后在构造函数中使用相同名称初始化了一个单独的变量。使用诸如Eclipse之类的IDE可以很容易地捕捉到这些类型的错误。我建议你使用一个。
非常感谢!所以马虎...... –
尝试设置断点并在缩进箱体后运行调试器 –