0

を拡張するクラスを作成しJDialog、保存を押したときに表示されるようにしました (保存はJMenuItemオブジェクトです)。ただし、保存を押すと、ファイル名が 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)
4

2 に答える 2

1

setVisible()クラスでメソッドを呼び出すSaveImageViewと、ダイアログが表示されますが、現在のスレッドはアクションを待たずに先に進みます。最も簡単な方法は、 を使用することJOptionPane.showInputDialogです。スレッドの実行をブロックします。

于 2013-04-27T13:18:58.227 に答える
1

コードのように見えます:filename = txtFilename.getText();保存する前に [OK] ボタンをクリックしていないため、呼び出されることはありません。

これは、JDialog を開いた後もメインの GUI クラスが一時停止せず、実行し続けるためです。

この動作を停止する方法は、ウィンドウのモダリティを に設定するtrueか、魔法の値を避けたい場合は に設定することですDialog.ModalityType.DEFAULT_MODAL

したがって、コンストラクターで、行を追加しますsetModal(true)


[OK] ボタンと [キャンセル] ボタンを押したら、 を終了する必要がありますJDialog。これを行うには、次の行を追加します。

dispose();

あなたの ActionListener コードに。

于 2013-04-27T13:25:56.673 に答える