2012-04-07 104 views
0

我有一个JDialog,并希望该商店后关闭它确认文本框的数据......现在我已经没有问题,从存储箱中的数据,但之后关闭Swing对话框,进行的操作

操作后如何关闭此对话框?

似乎是一个简单的事情,但我还没有找到解决方案。

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

很好的答案+1 – mKorbel 2012-04-07 20:02:52

+0

我不能使用的Dispose()这个元素contentPanel.setVisible(假)上,如果我使用;隐藏对话框的所有内容,但窗口仍然打开...出了什么问题? – AndreaF 2012-04-07 20:28:57

+0

@AndreaF你正在调用'contentPanel'上的方法,而不是'JDialog'。由于你的类从'JDialog'扩展,调用'this.dispose()'或'this.setVisible(false)'。 – Jeffrey 2012-04-07 20:33:03

0

如果您打算与所有的字段值和组件状态离开相同,当你关闭它再次使用一个JDialog,使用调用setVisible(假)。

在任何其他情况下,呼叫处理(),以避免残留在所述存储器中的的JDialog当不再需要它。

+0

如何在此JDialog上调用dispose()? – AndreaF 2012-04-07 20:32:32

+0

如果你想从Test类中执行它,请执行'this.dispose()'。如果你有一个Test实例,请执行'instance.dispose()'。 – MarioDS 2012-04-07 20:37:25

+0

从-this-指听者不至的JDialog,所以如果我尝试使用监听this.dispose()得到一个编译错误 – AndreaF 2012-04-07 20:40:55

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(); 
      } 
     }); 
    } 
} 
+0

我还没有尝试过用设置可见(假),但只隐藏的是保持打开状态 – AndreaF 2012-04-07 20:24:16

+0

@AndreaF我阐述了窗口的内容,这应该是也许正确的方式,请参阅我的编辑 – mKorbel 2012-04-07 20:53:18

+0

我还没有解决与杰弗里建议,还是要谢谢你 – AndreaF 2012-04-07 21:03:50