2017-03-03 138 views
0

我的問題是,我有一個類,當用戶鍵入顯示的文本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()' –

回答

1

你的文本字段是靜態的,所以他將只有一個應用程序實例。 所以在方法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

0

你C Ode有很多需要閱讀的內容,但我認爲你的問題在於你正在調用dispose()的錯誤對象。你可能總是爲第一個對象調用它,並且你正在創建一個新對象,並且處理這個新對話將永遠不會被調用,檢查你的代碼可能是你創建了許多對象,並且dispose()根本沒有被調用。確保你完全控制你的對象實例,以知道你是否正在尋找想要的對話框。

+0

但自在子類中調用方法getInputTxtField,這是否意味着將在該類上調用dispose,而不是父類? – Bradeurs

+0

做一些打擊或打印一些消息,看看代碼是否應該試圖setvisble(false)看看對話框是否會隱藏 – Cherif

+0

是的,我試了兩次,結果仍然是相同的 – Bradeurs