0

私はjdialogを持っていて、テキストボックスのデータを保存した後、確認時に閉じたいと思っています...今はボックスからのデータを保存するのに問題はありませんが、

操作後にこのダイアログを閉じるにはどうすればよいですか?

簡単なことのようですが、解決策が見つかりません。

public class test extends JDialog {

    private final JPanel contentPanel = new JPanel();

    public test() {
        setBounds(100, 100, 450, 300);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setLayout(new FlowLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        {
            JPanel buttonPane = new JPanel();
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(buttonPane, BorderLayout.SOUTH);
            {
                JButton okButton = new JButton("OK");
                okButton.setActionCommand("OK");
                buttonPane.add(okButton);
                getRootPane().setDefaultButton(okButton);
                             okButton.addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent e) {
                        try{

                            int x=Integer.parseInt(textField.getText());
                            saver.saveN(x);

                        }catch(Exception ecc){
                            JOptionPane.showMessageDialog(Test.this,"error");
                        }
                    }
                });
            }

        }
    }

}
4

3 に答える 3

3

Window#disposeまたはを使用しますWindow#setVisible(false)

于 2012-04-07T19:51:22.637 に答える
1

編集

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;
public class Test {

    private static final long serialVersionUID = 1L;
    private JDialog dialog = new JDialog();
    private final JPanel contentPanel = new JPanel();
    private Timer timer1;
    private JButton killkButton = new JButton("Kill JDialog");

    public Test() {
        contentPanel.setLayout(new FlowLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));

        JPanel buttonPane = new JPanel();
        JButton okButton = new JButton("OK");
        okButton.setActionCommand("OK");
        buttonPane.add(okButton);

        killkButton.setActionCommand("Kill JDialog");
        buttonPane.add(killkButton);

        dialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
        dialog.addWindowListener(new WindowListener() {

            public void windowOpened(WindowEvent e) {
            }

            public void windowClosing(WindowEvent e) {
                startTimer();
            }

            public void windowClosed(WindowEvent e) {
            }

            public void windowIconified(WindowEvent e) {
            }

            public void windowDeiconified(WindowEvent e) {
            }

            public void windowActivated(WindowEvent e) {
            }

            public void windowDeactivated(WindowEvent e) {
            }
        });
        dialog.setLayout(new BorderLayout());
        dialog.getRootPane().setDefaultButton(okButton);
        dialog.add(buttonPane, BorderLayout.SOUTH);
        dialog.add(contentPanel, BorderLayout.CENTER);
        dialog.pack();
        dialog.setLocation(100, 100);
        dialog.setVisible(true);
        setKeyBindings();
    }

    private void setKeyBindings() {
        killkButton.getInputMap(
                JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
                KeyStroke.getKeyStroke("ENTER"), "clickENTER");
        killkButton.getActionMap().put("clickENTER", new AbstractAction() {

            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
    }

    private void startTimer() {
        timer1 = new Timer(1000, new AbstractAction() {

            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        dialog.setVisible(true);
                    }
                });
            }
        });
        timer1.setDelay(500);
        timer1.setRepeats(false);
        timer1.start();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                Test test = new Test();
            }
        });
    }
}
于 2012-04-07T20:02:27.220 に答える
0

すべてのフィールド値とコンポーネントの状態を閉じたままにしてjDialogを再度使用する場合は、setVisible(false)を使用してください。

それ以外の場合は、dispose()を呼び出して、jDialogが不要になったときにメモリに残らないようにします。

于 2012-04-07T19:59:56.147 に答える