Java 方法返回值中JTextField上的SetText方法适用于一个方法,但不适用于另一个相同的方法
我认为这个标题很能说明问题 在我的ActionPerformed方法中,在条件语句的前两个语句上:Java 方法返回值中JTextField上的SetText方法适用于一个方法,但不适用于另一个相同的方法,java,Java,我认为这个标题很能说明问题 在我的ActionPerformed方法中,在条件语句的前两个语句上: if(!checkIfNokTextFieldIsEmpty() && checkIfOtherTextFieldIsEmpty()) { otherTextField.setText(calculateFromNok(txt, "GBP")); otherLabel.setText("GBP"); }
if(!checkIfNokTextFieldIsEmpty() && checkIfOtherTextFieldIsEmpty()) {
otherTextField.setText(calculateFromNok(txt, "GBP"));
otherLabel.setText("GBP");
}
else if(checkIfNokTextFieldIsEmpty() && !checkIfOtherTextFieldIsEmpty()) {
nokTextField.setText(calculateToNok(txt, "GBP"));
}
。。。即使我已将calculateFromNok和calculateToNok设置为完全相同,但只有最上面的一个(如果)可以工作。最上面的一个将calculateFromNok方法的返回值设置为otherTextField中的setText,我希望在仅设置otherTextField时执行相同但相反的操作。但正如我所说的,尽管这两种方法完全相同,但只有otherTextField的setText起作用。我已经将它们设置为相同的,以检查它是否工作,并且只是为了说它是货币数学,我将在稍后处理
这是我的全部代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ValutaKalkulator implements ActionListener
{
private JFrame frame;
private JOptionPane optionPane;
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 JLabel otherLabel;
private JTextField otherTextField;
private JTextField nokTextField;
public ValutaKalkulator()
{
frame = new JFrame();
frame.setResizable(false);
optionPane = new JOptionPane();
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");
otherLabel = new JLabel("Annen valuta");
otherTextField = new JTextField();
nokTextField = new JTextField();
ImageIcon img = new ImageIcon("dlr.png");
frame.setIconImage(img.getImage());
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 * 0.109690;
break;
case "EUR":
result = nokValue * 0.093158;
break;
case "USD":
result = nokValue * 0.085537;
break;
case "SEK":
result = nokValue * 97.03 / 100;
break;
}
String resultString = Double.toString(result);
return resultString;
}
public String calculateToNok(String nokValueString, String toValue)
{
double result = 0;
double nokValue = Double.valueOf(nokValueString);
switch(toValue)
{
case "GBP":
result = nokValue * 0.109690;
break;
case "EUR":
result = nokValue * 0.093158;
break;
case "USD":
result = nokValue * 0.085537;
break;
case "SEK":
result = nokValue * 97.03 / 100;
break;
}
String resultString = Double.toString(result);
return resultString;
}
public void actionPerformed(ActionEvent event)
{
String text = event.getActionCommand();
String txt = nokTextField.getText();
switch(text)
{
case "Fjern NOK":
nokTextField.setText("");
break;
case "Fjern annen valuta":
otherTextField.setText("");
otherLabel.setText("Annen valuta");
break;
case "Fjern begge":
nokTextField.setText("");
otherTextField.setText("");
otherLabel.setText("Annen valuta");
break;
case "Avslutt":
System.exit(0);
break;
case "GBP":
if(!checkIfNokTextFieldIsEmpty() && checkIfOtherTextFieldIsEmpty()) {
otherTextField.setText(calculateFromNok(txt, "GBP"));
otherLabel.setText("GBP");
}
else if(checkIfNokTextFieldIsEmpty() && !checkIfOtherTextFieldIsEmpty()) {
nokTextField.setText(calculateToNok(txt, "GBP"));
}
else if(!checkIfNokTextFieldIsEmpty() && !checkIfOtherTextFieldIsEmpty()) {
optionPane.showMessageDialog(frame, "Du må skrive bare ett beløp!");
}
else if(checkIfNokTextFieldIsEmpty() && checkIfOtherTextFieldIsEmpty()) {
optionPane.showMessageDialog(frame, "Du må skrive ett beløp!");
}
break;
case "EUR":
if(!checkIfNokTextFieldIsEmpty() && checkIfOtherTextFieldIsEmpty()) {
otherTextField.setText(calculateFromNok(txt, "EUR"));
otherLabel.setText("EUR");
}
else if(checkIfNokTextFieldIsEmpty() && !checkIfOtherTextFieldIsEmpty()) {
nokTextField.setText(calculateToNok(txt, "EUR"));
}
else if(!checkIfNokTextFieldIsEmpty() && !checkIfOtherTextFieldIsEmpty()) {
optionPane.showMessageDialog(frame, "Du må skrive bare ett beløp!");
}
else if(checkIfNokTextFieldIsEmpty() && checkIfOtherTextFieldIsEmpty()) {
optionPane.showMessageDialog(frame, "Du må skrive ett beløp!");
}
break;
case "USD":
if(!checkIfNokTextFieldIsEmpty() && checkIfOtherTextFieldIsEmpty()) {
otherTextField.setText(calculateFromNok(txt, "USD"));
otherLabel.setText("USD");
}
else if(checkIfNokTextFieldIsEmpty() && !checkIfOtherTextFieldIsEmpty()) {
nokTextField.setText(calculateToNok(txt, "USD"));
}
else if(!checkIfNokTextFieldIsEmpty() && !checkIfOtherTextFieldIsEmpty()) {
optionPane.showMessageDialog(frame, "Du må skrive bare ett beløp!");
}
else if(checkIfNokTextFieldIsEmpty() && checkIfOtherTextFieldIsEmpty()) {
optionPane.showMessageDialog(frame, "Du må skrive ett beløp!");
}
break;
case "SEK":
if(!checkIfNokTextFieldIsEmpty() && checkIfOtherTextFieldIsEmpty()) {
otherTextField.setText(calculateFromNok(txt, "SEK"));
otherLabel.setText("SEK");
}
else if(checkIfNokTextFieldIsEmpty() && !checkIfOtherTextFieldIsEmpty()) {
nokTextField.setText(calculateToNok(txt, "SEK"));
}
else if(!checkIfNokTextFieldIsEmpty() && !checkIfOtherTextFieldIsEmpty()) {
optionPane.showMessageDialog(frame, "Du må skrive bare ett beløp!");
}
else if(checkIfNokTextFieldIsEmpty() && checkIfOtherTextFieldIsEmpty()) {
optionPane.showMessageDialog(frame, "Du må skrive ett beløp!");
}
break;
}
}
public boolean checkIfNokTextFieldIsEmpty()
{
if(nokTextField.getText().isEmpty())
{
return true;
}
return false;
}
public boolean checkIfOtherTextFieldIsEmpty()
{
if(otherTextField.getText().isEmpty()) {
return true;
}
return false;
}
}
我收到的错误消息是:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException:
empty String
这应该意味着它试图把一个空字符串格式化成一个双精度的?但是,从double到string的格式在另一个方法上仍然有效,没有错误
也许这解释得不好,但我希望你明白我的意思。
提前谢谢 您在这一部分犯了一个小错误:
case "GBP":
if(!checkIfNokTextFieldIsEmpty() && checkIfOtherTextFieldIsEmpty()) {
otherTextField.setText(calculateFromNok(txt, "GBP"));
otherLabel.setText("GBP");
}
else if(checkIfNokTextFieldIsEmpty() && !checkIfOtherTextFieldIsEmpty()) {
nokTextField.setText(calculateToNok(txt, "GBP"));
}
在这里的else if
条件中,假设nokTextField
为空,则在String txt=nokTextField.getText()中
,此处txt
存储空字符串。因此,在<代码>的执行中,如果条件第一函数<代码> CHECIFIFKTKEXTEXDUNIVE()/代码>将返回<代码> true,并考虑另一个函数返回<代码> Trase< /Cord>。因此,满足此条件后,将调用calculatenok(txt,“GBP”)
函数
现在在该函数中有一条语句double nokValue=double.valueOf(nokValueString)
将尝试将该空字符串
转换为双精度
,并导致异常
因此,我认为您已经在else if
部分的条件下进行了修改。条件应为:
else if(!checkIfNokTextFieldIsEmpty() && !checkIfOtherTextFieldIsEmpty())
谢谢你的回复。但我确实在你提到的else if语句下面有这一行。if语句的另一个意思是,如果两个字段都是空的,那么做一些事情,我的下一个语句就是这样做的;显示了一个JOptionPane,告诉用户这两个字段都是空的。我不知道适合这两个字段的正确条件是什么。这是您的逻辑,但我所做的是指出了导致代码中出现
异常的错误。你可以根据需要重新定义这个函数。对,我明白你的意思了!非常感谢,老兄,修好了!它一直就在我眼前!