JDialog获取输入,而不是获取错误

时间:2013-04-27 13:05:40

标签: java swing file jdialog

我编写了一个扩展JDialog的类,当我按下save时保存(save是JMenuItem个对象)。但是,当我按下save时,我得到一个文件名为null的错误对话框,我没有机会输入任何内容。

我在这里做错了什么?任何帮助将不胜感激。

感谢。

以下是我的JDialog扩展类的代码:

package ui;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class SaveImageView extends JDialog {
    private final JPanel contentPanel = new JPanel ();
    private JTextField txtFilename;

    public int type; // 0 -> png, 1 -> jpg, 2 -> gif
    public String filename;

    public SaveImageView () {
        setTitle("Save Image");
        setBounds(100, 100, 450, 230);
        getContentPane().setLayout(null);
        contentPanel.setBounds(0, 0, 434, 229);
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel);
        contentPanel.setLayout(null);

        {
            JLabel lblFilename = new JLabel("Filename: ");
            lblFilename.setBounds(10, 95, 58, 20);
            contentPanel.add(lblFilename);
        }

        {
            txtFilename = new JTextField();
            txtFilename.setBounds(78, 95, 123, 20);
            contentPanel.add(txtFilename);
            txtFilename.setColumns(10);
        }

        {
            JRadioButton rdbtnGif = new JRadioButton("GIF");
            rdbtnGif.setBounds(123, 11, 43, 23);
            contentPanel.add(rdbtnGif);
            rdbtnGif.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    type = 2;
                }
            });
        }

        {
            JRadioButton rdbtnJpg = new JRadioButton("JPG");
            rdbtnJpg.setBounds(123, 37, 43, 23);
            contentPanel.add(rdbtnJpg);
            rdbtnJpg.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    type = 1;
                }
            });
        }

        {
            JRadioButton rdbtnPng = new JRadioButton("PNG");
            rdbtnPng.setBounds(123, 63, 45, 23);
            contentPanel.add(rdbtnPng);
            rdbtnPng.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    type = 0;
                }
            });
        }

        JLabel lblImageTypeTo = new JLabel("Image type to save: ");
        lblImageTypeTo.setBounds(10, 11, 144, 23);
        contentPanel.add(lblImageTypeTo);

        JLabel lblNote = new JLabel("NOTE: Images will be saved in \"output\" folder.");
        lblNote.setBounds(10, 132, 273, 22);
        contentPanel.add(lblNote);

        {
            JPanel buttonPane = new JPanel();
            buttonPane.setBounds(0, 160, 434, 33);
            contentPanel.add(buttonPane);
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));

            {
                JButton okButton = new JButton("OK");
                okButton.setActionCommand("OK");
                buttonPane.add(okButton);
                getRootPane().setDefaultButton(okButton);
                okButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent arg0) {
                        filename = txtFilename.getText();
                    }
                });
            }

            {
                JButton cancelButton = new JButton("Cancel");
                cancelButton.setActionCommand("Cancel");
                buttonPane.add(cancelButton);
            }
        }
    }
}

这是在我的主GUI类中,这是我向项目添加ActionListener的时候:

//////////////////////////////////////////////////////////
//                  Save - Menu Item                    //
//////////////////////////////////////////////////////////
JMenuItem saveItem = new JMenuItem("Save");
saveItem.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        SaveImageView siv = new SaveImageView();
        siv.setVisible(true);
        String typeStr = (siv.type == 0) ? "png" : ((siv.type == 1) ? "jpg" : "gif");
        try {
            ImageIO.write(img, typeStr, new File("output/" + siv.filename + "." + typeStr));
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
});
menu.add(saveItem);

这是我得到的错误:

java.io.FileNotFoundException: output\null.png (The system cannot find the path specified)
    at java.io.RandomAccessFile.open(Native Method)
    at java.io.RandomAccessFile.<init>(Unknown Source)
    at javax.imageio.stream.FileImageOutputStream.<init>(Unknown Source)
    at com.sun.imageio.spi.FileImageOutputStreamSpi.createOutputStreamInstance(Unknown Source)
    at javax.imageio.ImageIO.createImageOutputStream(Unknown Source)
    at javax.imageio.ImageIO.write(Unknown Source)
    at ui.PPMViewer$1.actionPerformed(PPMViewer.java:40)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.AbstractButton.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(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.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)
    at java.awt.EventQueue.access$400(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(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.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)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.imageio.ImageIO.write(Unknown Source)
    at ui.PPMViewer$1.actionPerformed(PPMViewer.java:40)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.AbstractButton.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(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.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)
    at java.awt.EventQueue.access$400(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(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.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)

2 个答案:

答案 0 :(得分:1)

当您在setVisible()类上调用SaveImageView方法时,会显示对话框,但当前线程不再等待您的操作。最简单的方法是使用JOptionPane.showInputDialog。它将阻止线程执行。

答案 1 :(得分:1)

看起来代码:filename = txtFilename.getText();永远不会被调用,因为您在保存之前没有单击“确定”按钮。

这是因为您的主GUI类在打开JDialog后没有暂停,而是继续运行。

停止此行为的一种方法是将窗口模态设置为true,或者,如果您想避免使用魔术值Dialog.ModalityType.DEFAULT_MODAL

因此,在构造函数中,添加行setModal(true)


按下“确定”和“取消”按钮后,您需要退出JDialog。 您可以通过添加以下行来执行此操作:

dispose();

到您的ActionListener代码。