单击“确定”按钮后,JDialog上没有返回值

时间:2014-11-15 05:42:28

标签: java return jdialog

延伸到JDialog的我的类在单击okay后不会返回值。谁能表明我做错了什么?我希望它返回一个双倍的值。

这是我的代码:

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)

2 个答案:

答案 0 :(得分:0)

您的堆栈跟踪说明了这一切 - 当您尝试将字符串解析为双精度时,您获得了NumberFormatException - Double.parseDouble(jtfAmount.getText())

Javadoc说:

  

抛出:
      NullPointerException - 如果字符串为null
      NumberFormatException - 如果字符串不包含可解析的double。

因此,使用try/catch块来处理此类情况。

此外,如果您不想等到对话框关闭以查看输入值是否正常,您可以在对话框中进行某种验证。

答案 1 :(得分:0)

实际问题不在于数字格式,而在于关闭操作,请将DefaultCloseOperation语句替换为以下对我有用的语句。因为它是一个JDialog它应该被处置:)

frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);