我很难在货币转换器中找到问题的根源。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Adam_Markros_Valutaomvandlare extends JFrame implements ActionListener {
JPanel left, middle, right;
JFrame ram;
JButton buttonOK;
JTextField fieldfrom, fieldto, extrafield;
JLabel labelfrom, labelto, labeldollar, labeleuro, labelpund, labelkrona, extralabel, nothing;
JRadioButton buttonDollar, buttonEuro, buttonPund, buttonKrona;
JRadioButton buttonDollar2, buttonEuro2, buttonPund2, buttonKrona2;
ButtonGroup GroupFrom = new ButtonGroup();
ButtonGroup GroupTo = new ButtonGroup();
int currencyFrom;
int currencyTo;
int taljare = 0;
int USD = 6;
int Pund = 10;
int Euro = 9;
int other;
int namnare = 0;
public Adam_Markros_Valutaomvandlare(){
left = new JPanel();
middle = new JPanel();
right = new JPanel();
setSize(600,250);
setTitle("Adam Markros - Valutaomvandlare");
setLayout(new GridLayout(1,3));
left.setLayout(new GridLayout(6,1));
middle.setLayout(new GridLayout(6,1));
right.setLayout(new GridLayout(6,1));
this.add(left);
this.add(middle);
this.add(right);
buttonOK = new JButton("OK");
fieldfrom = new JTextField();
fieldto = new JTextField();
extrafield = new JTextField();
labelfrom = new JLabel("Från:");
labelto = new JLabel("Till:");
extralabel = new JLabel("Annan valuta:");
nothing = new JLabel("");
buttonDollar = new JRadioButton("USD");
buttonEuro = new JRadioButton("Euro");
buttonPund = new JRadioButton("Pund");
buttonKrona = new JRadioButton("SEK");
buttonDollar2 = new JRadioButton("USD");
buttonEuro2 = new JRadioButton("Euro");
buttonPund2 = new JRadioButton("Pund");
buttonKrona2 = new JRadioButton("SEK");
GroupFrom.add(buttonDollar);
GroupFrom.add(buttonEuro);
GroupFrom.add(buttonPund);
GroupFrom.add(buttonKrona);
GroupTo.add(buttonDollar2);
GroupTo.add(buttonEuro2);
GroupTo.add(buttonPund2);
GroupTo.add(buttonKrona2);
left.add(labelfrom);
left.add(buttonDollar);
left.add(buttonEuro);
left.add(buttonPund);
left.add(buttonKrona);
left.add(fieldfrom);
middle.add(extralabel);
middle.add(extrafield);
middle.add(buttonOK);
right.add(labelto);
right.add(buttonDollar2);
right.add(buttonEuro2);
right.add(buttonPund2);
right.add(buttonKrona2);
right.add(fieldto);
buttonOK.addActionListener(this);
fieldfrom.addActionListener(this);
fieldto.addActionListener(this);
extrafield.addActionListener(this);
buttonDollar.addActionListener(this);
buttonDollar2.addActionListener(this);
buttonEuro.addActionListener(this);
buttonEuro2.addActionListener(this);
buttonPund.addActionListener(this);
buttonPund2.addActionListener(this);
buttonKrona.addActionListener(this);
buttonKrona2.addActionListener(this);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main (String[] args){
new Adam_Markros_Valutaomvandlare();
}
public void actionPerformed(ActionEvent e) {
if((fieldfrom.getText().toString()) != ""){
currencyFrom = Integer.parseInt(fieldfrom.getText());
}
if((fieldto.getText().toString()) != ""){
currencyTo = Integer.parseInt(fieldto.getText());
}
if((extrafield.getText().toString()) != ""){
taljare = Integer.parseInt(extrafield.getText());
}
/*
if(e.getSource() == buttonDollar){
currencyFrom = USD;
}
if(e.getSource() == buttonDollar2){
currencyTo = USD;
}
if(e.getSource() == buttonPund){
currencyFrom = Pund;
}
if(e.getSource() == buttonPund2){
currencyTo = Pund;
}
if(e.getSource() == buttonEuro){
currencyFrom = Euro;
}
if(e.getSource() == buttonEuro2){
currencyTo = Euro;
}
if(e.getSource() == buttonKrona){
currencyFrom = 1;
}
if(e.getSource() == buttonKrona2){
currencyTo = 1;
}
else{
taljare = other;
}
*/
if(e.getSource() == buttonOK){
if(buttonPund.isSelected()){
taljare = 10;
}
if(buttonDollar.isSelected()){
taljare = 6;
}
if(buttonEuro.isSelected()){
taljare = 9;
}
if(buttonKrona.isSelected()){
taljare = 1;
}
if(buttonPund2.isSelected()){
namnare = 10;
}
if(buttonDollar2.isSelected()){
namnare = 6;
}
if(buttonEuro2.isSelected()){
namnare = 9;
}
if(buttonKrona2.isSelected()){
namnare = 1;
}
currencyTo = (currencyFrom * (taljare / namnare));
fieldto.setText(Integer.toString(currencyTo));
}
}
}
以下是错误:
线程中的异常" AWT-EventQueue-0" java.lang.NumberFormatException:对于输入字符串:"" at java.lang.NumberFormatException.forInputString(Unknown Source) 在java.lang.Integer.parseInt(未知来源) 在java.lang.Integer.parseInt(未知来源) 在Adam_Markros_Valutaomvandlare.actionPerformed(Adam_Markros_Valutaomvandlare.java:116) 在javax.swing.AbstractButton.fireActionPerformed(未知来源) at javax.swing.AbstractButton $ Handler.actionPerformed(Unknown Source) 在javax.swing.DefaultButtonModel.fireActionPerformed(未知来源) 在javax.swing.JToggleButton $ ToggleButtonModel.setPressed(未知来源) 在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(未知来源) at java.awt.Component.processMouseEvent(Unknown Source) 在javax.swing.JComponent.processMouseEvent(未知来源) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 在java.awt.EventQueue.access $ 000(未知来源) 在java.awt.EventQueue $ 1.run(未知来源) 在java.awt.EventQueue $ 1.run(未知来源) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext $ 1.doIntersectionPrivilege(Unknown Source) at java.security.AccessControlContext $ 1.doIntersectionPrivilege(Unknown Source) 在java.awt.EventQueue $ 2.run(未知来源) 在java.awt.EventQueue $ 2.run(未知来源) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext $ 1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) 线程中的异常" AWT-EventQueue-0" java.lang.NumberFormatException:对于输入字符串:"" at java.lang.NumberFormatException.forInputString(Unknown Source) 在java.lang.Integer.parseInt(未知来源) 在java.lang.Integer.parseInt(未知来源) 在Adam_Markros_Valutaomvandlare.actionPerformed(Adam_Markros_Valutaomvandlare.java:119) 在javax.swing.AbstractButton.fireActionPerformed(未知来源) at javax.swing.AbstractButton $ Handler.actionPerformed(Unknown Source) 在javax.swing.DefaultButtonModel.fireActionPerformed(未知来源) 在javax.swing.JToggleButton $ ToggleButtonModel.setPressed(未知来源) 在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(未知来源) at java.awt.Component.processMouseEvent(Unknown Source) 在javax.swing.JComponent.processMouseEvent(未知来源) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 在java.awt.EventQueue.access $ 000(未知来源) 在java.awt.EventQueue $ 1.run(未知来源) 在java.awt.EventQueue $ 1.run(未知来源) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext $ 1.doIntersectionPrivilege(Unknown Source) at java.security.AccessControlContext $ 1.doIntersectionPrivilege(Unknown Source) 在java.awt.EventQueue $ 2.run(未知来源) 在java.awt.EventQueue $ 2.run(未知来源) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext $ 1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) 线程中的异常" AWT-EventQueue-0" java.lang.NumberFormatException:对于输入字符串:"" at java.lang.NumberFormatException.forInputString(Unknown Source) 在java.lang.Integer.parseInt(未知来源) 在java.lang.Integer.parseInt(未知来源) 在Adam_Markros_Valutaomvandlare.actionPerformed(Adam_Markros_Valutaomvandlare.java:119) 在javax.swing.AbstractButton.fireActionPerformed(未知来源) at javax.swing.AbstractButton $ Handler.actionPerformed(Unknown Source) 在javax.swing.DefaultButtonModel.fireActionPerformed(未知来源) 在javax.swing.DefaultButtonModel.setPressed(未知来源) 在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(未知来源) at java.awt.Component.processMouseEvent(Unknown Source) 在javax.swing.JComponent.processMouseEvent(未知来源) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 在java.awt.EventQueue.access $ 000(未知来源) 在java.awt.EventQueue $ 1.run(未知来源) 在java.awt.EventQueue $ 1.run(未知来源) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext $ 1.doIntersectionPrivilege(Unknown Source) at java.security.AccessControlContext $ 1.doIntersectionPrivilege(Unknown Source) 在java.awt.EventQueue $ 2.run(未知来源) 在java.awt.EventQueue $ 2.run(未知来源) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext $ 1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 在java.awt.EventDispatchThread.run(未知来源)
答案 0 :(得分:2)
不要使用==
或!=
来比较字符串,因为这些会检查一个对象引用是否与另一个对象引用相同,这是您不感兴趣的内容。
使用equals(...)或者在你的情况isEmpty()
中,因为这些检查是否String的内容与另一个相同或者String是否为空。
if (!myTextField.getText().isEmpty()) {
}