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#disposeWindow#setVisible(false)

于 2012-04-07T19:51:22.637 回答
1
  • 如果您只使用一次此对话框,则可以使用相同dispose()setVisible(false)

  • 如果您多次调用此方法,则可以使用HIDE_ON_CLOSE or setVisible(false)最好是 re_use this JDialog

编辑

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 回答