2014-11-15 34 views
0

擴展到JDialog的我的類在單擊好後不會返回值。任何人都可以證明我做錯了什麼?我希望它返回一個雙倍的值。單擊確定按鈕後,JDialog上的值不會返回

這裏是我的代碼:

import javax.swing.*; 
import javax.swing.border.*; 
import java.awt.*; 
import java.awt.BorderLayout; 
import java.awt.event.*; 

public class DepositWindow extends JDialog 
{ 
    private JButton jbtOk; 
    private JButton jbtCancel; 
    private JLabel jlbAccountNumber; 
    private JTextField jtfAmount; 

    public DepositWindow() { 
     setModal(true); 
     // Create panel p1 for the buttons and set GridLayout 
     // Set BorderLayout with horizontal gap 5 and vertical gap 10 
     setLayout(new BorderLayout(5, 10)); 
     JPanel p1 = new JPanel(); 
     p1.setBorder(new TitledBorder("Actions")); 
     p1.setLayout(new FlowLayout(FlowLayout.RIGHT, 1, 1)); 
     jbtOk = new JButton("OK"); 
     jbtCancel = new JButton("Cancel"); 
     p1.add(jbtOk); 
     p1.add(jbtCancel); 

     this.add(p1,BorderLayout.SOUTH); 

     JPanel p2 = new JPanel(new GridLayout(2,2)); 
     p2.setBorder(new TitledBorder("Account")); 
     p2.add(new JLabel("Account Number")); 
     jlbAccountNumber = new JLabel("*****-56"); 
     p2.add(jlbAccountNumber); 
     p2.add(new JLabel("Amount")); 
     jtfAmount = new JTextField(10); 
     p2.add(jtfAmount); 

     // add contents into the frame 
     add(p2, BorderLayout.CENTER); 

     jbtOk.addActionListener(new ActionListener(){ 
      @Override 
      public void actionPerformed(ActionEvent e){ 
       setVisible(false); 
      } 
     }); 
    } 

    public double getAmount(){ 
     return Double.parseDouble(jtfAmount.getText()); 
    } 

    /** Main method */ 
    public static void main(String[] args) { 
    DepositWindow frame = new DepositWindow(); 
    frame.setTitle("Deposit Window"); 
    frame.pack(); 
    frame.setLocationRelativeTo(null); // Center the frame 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setVisible(true); 
    double amount = frame.getAmount(); 
    } 
} 

它給我,當我跑我的主要 錯誤,這些錯誤:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String 
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1011) 
    at java.lang.Double.parseDouble(Double.java:540) 
    at DepositWindow.getAmount(DepositWindow.java:51) 
    at AccountWindow.<init>(AccountWindow.java:48) 
    at LoginWindow.login(LoginWindow.java:67) 
    at LoginWindow.access$000(LoginWindow.java:16) 
    at LoginWindow$1.actionPerformed(LoginWindow.java:55) 
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) 
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341) 
    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:6505) 
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3320) 
    at java.awt.Component.processEvent(Component.java:6270) 
    at java.awt.Container.processEvent(Container.java:2229) 
    at java.awt.Component.dispatchEventImpl(Component.java:4861) 
    at java.awt.Container.dispatchEventImpl(Container.java:2287) 
    at java.awt.Component.dispatchEvent(Component.java:4687) 
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) 
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) 
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) 
    at java.awt.Container.dispatchEventImpl(Container.java:2273) 
    at java.awt.Window.dispatchEventImpl(Window.java:2719) 
    at java.awt.Component.dispatchEvent(Component.java:4687) 
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735) 
    at java.awt.EventQueue.access$200(EventQueue.java:103) 
    at java.awt.EventQueue$3.run(EventQueue.java:694) 
    at java.awt.EventQueue$3.run(EventQueue.java:692) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) 
    at java.awt.EventQueue$4.run(EventQueue.java:708) 
    at java.awt.EventQueue$4.run(EventQueue.java:706) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:705) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) 
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91) 
+1

那麼,你有沒有在文本字段中輸入的東西?看起來不是。在刪除'frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);' –

+0

@JBNizet後,它可以很好地工作,當我用Dispose替換它時,它應該替換爲dispose,否則程序將在關閉時在後臺運行,如果只刪除它。請參閱我的答案http://stackoverflow.com/a/26944883/1966247 – Muhammad

回答

0

你的堆棧跟蹤了一切 - 你得到一個NumberFormatException當試圖解析一個字符串到一個雙 - Double.parseDouble(jtfAmount.getText())

Javadoc說:

Throws:
NullPointerException - if the string is null
NumberFormatException - if the string does not contain a parsable double.

所以使用try/catch塊來處理這樣的情況。

另外,如果您不想等到對話框關閉以查看輸入值是否正確,您可以在對話框中進行某種驗證。

0

實際的問題不在於數字格式,而在於關閉操作,請將您的DefaultCloseOperation聲明替換爲以下適用於我的聲明。因爲它是一個JDialog它應該配置:)

frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
相關問題