为什么JTEXTFIELD的setText在Action Performed方法中不起作用



im制作货币计算器,我遇到了一个问题,无法将计算结果设置为结果JTEXTFIELD,这是对象ethertextfield。

这是我的班级:

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;
        }
    }
}

我知道货币不正确,还有其他逻辑缺陷和不良的变量名称,但是我现在的问题是为什么我不能将方法从方法中计算到我的jtextfield ersettextfield?

中?

帮助您感谢THX!

您正在创建一个初始化本地变量JTextField nokTextField
因此private JTextField nokTextField;没有初始化。这就是为什么它会给您NullPoInterException。

只是在行号37上更改:

JTextField nokTextField = new JTextField();

to

nokTextField = new JTextField();

您的问题与行:

JTextField nokTextField = new JTextField();

将其更改为:

nokTextField = new JTextField();

发生这种情况的原因是您先前在顶部定义了Noktextfield,然后您在构造函数中初始化了一个单独的变量。使用诸如Eclipse之类的IDE使这些类型的错误非常容易捕获。我建议您使用一个。

最新更新