我刚开始学习Java Swings。我实际上打算制作一个货币转换器,其中有3个文本字段美元,卢比和日元。当我编辑美元的文本字段时,我希望其他两个文本字段动态更改为适当的转换货币值。我在开始时尝试使用DocumentListener但是我失败了。所以我尝试了addKeyListener。当我更改第一个文本字段时,它工作得非常好,但是当我首先更改第二个或第三个字段时,我会得到泵事件和不熟悉的线程错误。请看这里的代码。提前谢谢。
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import net.miginfocom.swing.MigLayout;
import javax.swing.JTextField;
import java.awt.GridBagLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
public class Conv {
private JFrame frame;
private JTextField textField1;
private JTextField textField2;
private JTextField textField3;
private JLabel lblNum;
private JLabel lblNum_1;
private JLabel lblNum_2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Conv window = new Conv();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Conv() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textField1 = new JTextField();
textField1.setBounds(170, 69, 86, 20);
frame.getContentPane().add(textField1);
textField1.setColumns(10);
textField2 = new JTextField();
textField2.setBounds(170, 111, 86, 20);
frame.getContentPane().add(textField2);
textField2.setColumns(10);
textField3 = new JTextField();
textField3.setBounds(170, 155, 86, 20);
frame.getContentPane().add(textField3);
textField3.setColumns(10);
lblNum = new JLabel("Dollar");
lblNum.setBounds(114, 72, 46, 14);
frame.getContentPane().add(lblNum);
lblNum_1 = new JLabel("Rupees");
lblNum_1.setBounds(114, 114, 46, 14);
frame.getContentPane().add(lblNum_1);
lblNum_2 = new JLabel("Yen");
lblNum_2.setBounds(114, 158, 46, 14);
frame.getContentPane().add(lblNum_2);
textField1.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e1) {
textField2.setText("");
textField3.setText("");
Double d1,d2,d3;
d1 = Double.parseDouble(textField1.getText());
d2 = d1 + 1;
d3 = d1 + 2;
textField2.setText(Double.toString(d2));
textField3.setText(Double.toString(d3));
}
});
textField2.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e2) {
textField1.setText("");
textField3.setText("");
Double d1,d2,d3;
d2 = Double.parseDouble(textField2.getText());
d1 = d2 - 1;
d3 = d2 + 1;
textField1.setText(Double.toString(d1));
textField3.setText(Double.toString(d3));
}
});
textField3.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e3) {
textField1.setText("");
textField2.setText("");
Double d1,d2,d3;
d3 = Double.parseDouble(textField3.getText());
d1 = d3 - 2;
d2 = d3 - 1;
textField1.setText(Double.toString(d1));
textField2.setText(Double.toString(d2));
}
});
}
}
输出/错误:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException:empty String
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at Conv$3.keyTyped(Conv.java:101)
at java.awt.Component.processKeyEvent(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
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.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(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)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.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)
答案 0 :(得分:1)
错误消息中的第一行包含:var slider = d3.select('#year');
slider.on('change', function() {
//IF STATEMENT TO GO HERE
grabber =[];
drawVis('2009');
});
这意味着您尝试将空字符串<form>
<label for="year">Please select a year: </label>
<label for="year">Please select a year: </label>
<input type="range" min=2009 max=2010 step=1 id="year" value=2010 oninput="selected_year.value = year.value">
<output name="selected_year" id="selected_year">2010</output>
</form>
转换为您从未填充的文本字段中获得的数字。
堆栈跟踪中提及代码的第一行是:
java.lang.NumberFormatException:empty String
我猜它在这里:
""
您需要将at Conv$3.keyTyped(Conv.java:101)
提取到本地变量并检查它是否真的包含数字。