Dispose()只能工作一次

问题描述:

我的问题是,我有一个类,当用户键入显示的文本dispose()被调用,这是第一次工作,但如果你不关闭程序并再次打开它,dispose()被调用,但不会做任何破坏程序的事情。Dispose()只能工作一次

public class TypeMenu extends JDialog { 

    protected final JPanel contentPanel = new JPanel(); 
    protected static JTextField inputTxtField; 
    protected static JTextField textField; 
    protected static JTextField introTxtField; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     try { 
      Easy dialog = new Easy(); 
      dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 
      dialog.setVisible(true); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    /** 
    * Create the dialog. 
    * @param introTxtField2 
    * @param textField2 
    * @param inputTxtField2 
    */ 
    public TypeMenu(JTextField inputTxtField2, JTextField introTxtField2, JTextField textField2) { 
     setBounds(100, 100, 450, 300); 
     getContentPane().setLayout(new BorderLayout()); 
     contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     getContentPane().add(contentPanel, BorderLayout.CENTER); 
     contentPanel.setLayout(null); 
     contentPanel.add(getInputTxtField()); 
     contentPanel.add(getTextField()); 
     contentPanel.add(getIntroTxtField()); 
     { 
      JPanel buttonPane = new JPanel(); 
      buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT)); 
      getContentPane().add(buttonPane, BorderLayout.SOUTH); 
     } 
    } 
    protected JTextField getInputTxtField() { 
     if (inputTxtField == null) { 
      inputTxtField = new JTextField(); 
      inputTxtField.setHorizontalAlignment(SwingConstants.CENTER); 
      inputTxtField.addKeyListener(new KeyAdapter() { 
       @Override 
       public void keyReleased(KeyEvent arg0) { 
        String strField = textField.getText(); 
        char key = arg0.getKeyChar(); 
        int length = strField.length(); 
        if (Character.toLowerCase(strField.charAt(0)) == Character.toLowerCase(key)) { 
         inputTxtField.setText(" "); 
         textField.setText(strField.substring(1)); 
         System.out.println(length); 
         System.out.println(strField); 
         if (length - 1 <= 0) { 
          dispose(); 
         } 
        } else { 
         inputTxtField.setText(" "); 
        } 
       } 
      }); 
      inputTxtField.setBounds(56, 177, 314, 40); 
      inputTxtField.setColumns(10); 
     } 
     return inputTxtField; 
    } 
    protected JTextField getIntroTxtField() { 
     if (introTxtField == null) { 
      introTxtField = new JTextField(); 
      introTxtField.setHorizontalAlignment(SwingConstants.CENTER); 
      introTxtField.setFont(new Font("Tahoma", Font.BOLD, 15)); 
      introTxtField.setText("Easy Mode"); 
      introTxtField.setEditable(false); 
      introTxtField.setBounds(56, 11, 314, 29); 
      introTxtField.setColumns(10); 
     } 
     return introTxtField; 
    } 
    private JTextField getTextField() { 
     if (textField == null) { 
      textField = new JTextField(); 
      textField.setHorizontalAlignment(SwingConstants.CENTER); 
      textField.setFont(new Font("Monospaced", Font.BOLD, 20)); 
      textField.setBounds(10, 51, 414, 40); 
     } 
     return textField; 
    } 
} 

这是子类

public class Easy extends TypeMenu { 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     try { 
      Easy dialog = new Easy(); 
      dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 
      dialog.setVisible(true); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    /** 
    * Create the dialog. 
    */ 
    public Easy() { 
     super(inputTxtField, introTxtField, textField); 
     setBounds(100, 100, 450, 300); 
     getContentPane().setLayout(new BorderLayout()); 
     contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     getContentPane().add(contentPanel, BorderLayout.CENTER); 
     contentPanel.setLayout(null); 
     contentPanel.add(getInputTxtField()); 
     contentPanel.add(getIntroTxtField()); 
     getString(); 
     { 
      JPanel buttonPane = new JPanel(); 
      buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT)); 
      getContentPane().add(buttonPane, BorderLayout.SOUTH); 
      textField.selectAll(); 
     } 
    } 
    private void getString() { 
     String str = textField.getText(); 
     if (str.equals("")) { 
      String generator = StringGenerator.medium(); 
      String nStr = "" + generator; 
      textField.setText(nStr); 
     } 
    } 
} 

调用这个类的代码的一个

公共类的StartMenu扩展的JDialog {

private final JPanel contentPanel = new JPanel(); 
private JTextField introTxt; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    try { 
     StartMenu dialog = new StartMenu(); 
     dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 
     dialog.setVisible(true); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

/** 
* Creates the dialog and creates the buttons that take the user to each variation of the game when pressed. 
*/ 
public StartMenu() { 
    setBounds(100, 100, 450, 300); 
    getContentPane().setLayout(new BorderLayout()); 
    contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    getContentPane().add(contentPanel, BorderLayout.CENTER); 
    contentPanel.setLayout(null); 
    { 
     introTxt = new JTextField(); 
     introTxt.setFont(new Font("Tahoma", Font.BOLD, 12)); 
     introTxt.setHorizontalAlignment(SwingConstants.CENTER); 
     introTxt.setText("Start Menu\r\n"); 
     introTxt.setEditable(false); 
     introTxt.setBounds(75, 11, 276, 20); 
     contentPanel.add(introTxt); 
     introTxt.setColumns(10); 
    } 
    { 
     JButton btnEasyButton = new JButton("Easy Mode"); 
     btnEasyButton.addMouseListener(new MouseAdapter() { 
      @Override 
      public void mouseClicked(MouseEvent arg0) { 
       new Easy().setVisible(true); 
      } 
     }); 
     btnEasyButton.setBounds(141, 42, 140, 23); 
     contentPanel.add(btnEasyButton);  
    } 
    { 
     JButton btnMediumButton = new JButton("Medium Mode"); 
     btnMediumButton.addMouseListener(new MouseAdapter() { 
      @Override 
      public void mouseClicked(MouseEvent e) { 
       new Medium().setVisible(true); 
      } 
     }); 
     btnMediumButton.setBounds(141, 81, 140, 23); 
     contentPanel.add(btnMediumButton); 
    } 
    { 
     JButton btnHardButton = new JButton("Hard Mode"); 
     btnHardButton.addMouseListener(new MouseAdapter() { 
      @Override 
      public void mouseClicked(MouseEvent e) { 
       new Hard().setVisible(true); 
      } 
     }); 
     btnHardButton.setBounds(141, 120, 140, 23); 
     contentPanel.add(btnHardButton); 
    } 
    { 
     JPanel buttonPane = new JPanel(); 
     buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT)); 
     getContentPane().add(buttonPane, BorderLayout.SOUTH); 
     { 
      JButton okButton = new JButton("OK"); 
      okButton.addActionListener(new ActionListener() { 
       public void actionPerformed(ActionEvent e) { 
        dispose(); 
       } 
      }); 
      okButton.setActionCommand("OK"); 
      buttonPane.add(okButton); 
      getRootPane().setDefaultButton(okButton); 
     } 
    } 
} 

}

+0

这是很多几乎没有评论的代码,没有太多的解释它应该做什么。 –

+0

是的,我知道对不起,我一直在这里移动它很多 – Bradeurs

+0

我看不到你在哪里调用'dispose()' –

你的文本字段是静态的,所以他将只有一个应用程序实例。 所以在方法getIntroTxtField()你有if语句,说:

if (introTxtField == null) 
在第一次遇到这种情况是真实的,但是当你创建新实例这个条件为假

因为静态字段的实例是所有准备创建在第一个中,您将在条件 内添加关键侦听器,以便仅在第一次创建时添加动作侦听器。 如果你需要保持static,因为你在其他类需要你需要删除== null

protected JTextField getInputTxtField() { 
    inputTxtField = null; 
    { 
     inputTxtField = new JTextField(); 
     inputTxtField.setHorizontalAlignment(SwingConstants.CENTER); 
     inputTxtField.addKeyListener(new KeyAdapter() { 
      @Override 
      public void keyReleased(KeyEvent arg0) { 
       String strField = textField.getText(); 
       char key = arg0.getKeyChar(); 
       int length = strField.length(); 
       if (Character.toLowerCase(strField.charAt(0)) == Character.toLowerCase(key)) { 
        inputTxtField.setText(" "); 
        textField.setText(strField.substring(1)); 
        System.out.println(length); 
        System.out.println(strField); 
        if (length - 1 <= 0) { 
         dispose(); 
        } 
       } else { 
        inputTxtField.setText(" "); 
       } 
      } 
     }); 
     inputTxtField.setBounds(56, 177, 314, 40); 
     inputTxtField.setColumns(10); 
    } 
    return inputTxtField; 
} 

或删除了static字段声明静态在只有当你只使用 共享instace使用一个应用程序像sessionFactory或任何需要创建 一次的应用程序。

+0

非常感谢,这解决了我的问题! – Bradeurs

你C Ode有很多需要阅读的内容,但我认为你的问题在于你正在调用dispose()的错误对象。你可能总是为第一个对象调用它,并且你正在创建一个新对象,并且处理这个新对话将永远不会被调用,检查你的代码可能是你创建了许多对象,并且dispose()根本没有被调用。确保你完全控制你的对象实例,以知道你是否正在寻找想要的对话框。

+0

但自在子类中调用方法getInputTxtField,这是否意味着将在该类上调用dispose,而不是父类? – Bradeurs

+0

做一些打击或打印一些消息,看看代码是否应该试图setvisble(false)看看对话框是否会隐藏 – Cherif

+0

是的,我试了两次,结果仍然是相同的 – Bradeurs