我想从nrZamowieniaJTF(TextField)获得int值。通过以下代码在另一个窗口(OknoListaZamowien)中对其进行初始化:
if (e.getSource() == zapiszJB) {
OknoEdycjaZamowienia ob = new OknoEdycjaZamowienia();
ob.setVisible(true);
try {
nrZamowieniaLista = Integer.parseInt(idJTF.getText());
} catch (NumberFormatException xxx) {
nrZamowieniaLista = 1;
idJTF.setText("1");
}
ob.nrZamowieniaJTF.setText(Integer.toString(nrZamowieniaLista));
this.setVisible(false);
}
因此,这就是窗口的外观(如您所见,现在将3号分配给nrZamowieniaJTF):
window where nrZamowieniaJTF shown
但是,当我试图将这个值转换为另一个名为OknoEdycjaZamowienia的窗口时遇到错误:
nrZamowieniaJTF = new JTextField();
nrZamowieniaJTF.setBounds(460, 30, 100, 20);
nrZamowieniaJTF.setEditable(false);
getContentPane().add(nrZamowieniaJTF);
int nrZamowienia = Integer.parseInt(nrZamowieniaJTF.getText());
那就是我面临的错误(当我这样做时,(显示的)窗口不显示):
Exception in thread "AWT-EventQueue-0"
java.lang.NumberFormatException: For input string: "" at
java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:592) at
java.lang.Integer.parseInt(Integer.java:615) at
com.tasia2931.okna.OknoEdycjaZamowienia.<init>(OknoEdycjaZamowienia.java:135)
at
com.tasia2931.okna.OknoListaZamowien.actionPerformed(OknoListaZamowien.java:125) at
javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at
javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at
javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at
javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at
javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6539) at
javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at
java.awt.Component.processEvent(Component.java:6304) at
java.awt.Container.processEvent(Container.java:2239) at
java.awt.Component.dispatchEventImpl(Component.java:4889) at
java.awt.Container.dispatchEventImpl(Container.java:2297) at
java.awt.Component.dispatchEvent(Component.java:4711) at
java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
at
java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
at java.awt.Container.dispatchEventImpl(Container.java:2283) at
java.awt.Window.dispatchEventImpl(Window.java:2746) at
java.awt.Component.dispatchEvent(Component.java:4711) at
java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760) at
java.awt.EventQueue.access$500(EventQueue.java:97) at
java.awt.EventQueue$3.run(EventQueue.java:709) at
java.awt.EventQueue$3.run(EventQueue.java:703) at
java.security.AccessController.doPrivileged(Native Method) at
java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at
java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
at java.awt.EventQueue$4.run(EventQueue.java:733) at
java.awt.EventQueue$4.run(EventQueue.java:731) at
java.security.AccessController.doPrivileged(Native Method) at
java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:730) at
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
at
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
这让我想到
nrZamowieniaJTF.getText()
的值什么都不是,这就是为什么它不能分配给我的int变量的原因:/您的帮助将不胜感激!感谢您的时间 :)
最佳答案
我认为可能有两种可能性:
将nrZamowieniaJTF添加到ContentPane时,它可能不是同一实例。因此,您可以在ob.nrZamowieniaJTF中拥有不同的实例,并且实际上已将不同的实例添加到ContentPane中。在那种情况下,您需要确保它们是相同的实例,并且该字段仅实例化一次,为此,您应该只调用一次nrZamowieniaJTF = new JTextField();。
第二种可能性是您的代码尝试在实际上没有设置任何值之前读取并解析该字段的值。在这种情况下,您应该在真正设置完该字段后解析该值。