gpt4 book ai didi

java - 为什么JTextField的setText在actionPerformed方法中不起作用?

转载 作者:行者123 更新时间:2023-12-02 13:19:53 24 4
gpt4 key购买 nike

我正在制作一个货币计算器,但我遇到了一个问题,无法将计算结果设置到结果 JTextField(即 otherTextField 对象)中。

这是我的类(class):

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 中?

感谢您的帮助!

最佳答案

您正在创建一个初始化局部变量JTextField nokTextField
因此 private JTextField nokTextField; 未初始化。这就是为什么它会给你 nullpointerException。

只需在第 37 行进行更改:

JTextField nokTextField = new JTextField();

nokTextField = new JTextField();

关于java - 为什么JTextField的setText在actionPerformed方法中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43615214/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com