完成操作后关闭挥杆对话框

时间:2012-04-07 19:49:03

标签: java swing

我有一个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");
                        }
                    }
                });
            }

        }
    }

}

3 个答案:

答案 0 :(得分:3)

答案 1 :(得分:1)

  • 如果您只使用此对话一次,那么使用dispose()作为setVisible(false)

  • 是相同的
  • 如果您多次调用此方法,则可以使用HIDE_ON_CLOSEsetVisible(false)better would be 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();
            }
        });
    }
}

答案 2 :(得分:0)

如果您计划再次使用jDialog时所有字段值和组件状态在关闭时保持不变,请使用setVisible(false)。

在任何其他情况下,调用dispose(),以避免jDialog在不再需要时保留在内存中。