2012-05-15 41 views
0

在我開始之前,我是一名初學者程序員。單擊按鈕時如何啓用文本字段?

如何在單擊按鈕時啓用文本字段。

我有兩個框架,一個是JFields,另一個是異常。 發生異常時>setEditable(false) 但是我應該做些什麼聲明來啓用JFields,一旦用戶點擊好的按鈕 - 這是我在異常中所做的 - ?

我試着將靜態布爾值添加到異常幀,並且在此類的執行動作中,我初始化了布爾值true

在其他類

,我添加一個if statment,如果布爾值爲true,那麼setEditable(真)

- ====== - 本方案的點,即當在用戶關閉異常窗口之前,不能在字段中輸入任何內容。

我希望你能幫助我。

與所有的愛,程序員。

動作爲異常窗框進行(具有好按鈕。)代碼

public void actionPerformed(ActionEvent e){ 
     { 
     allow=true; //static boolean 
     Container TheFrame = OKButton.getParent(); 
     do TheFrame = TheFrame.getParent(); 
       while (!(TheFrame instanceof JFrame)); 

     ((JFrame) TheFrame).dispose(); 

    } 

動作的主程序(具有三個字段執行的代碼,將出現一次的一個異常用戶輸入非數字) 我添加了一些意見,以澄清。

public void actionPerformed(ActionEvent event) { 

    try{ 
     r =Double.parseDouble(RField.getText()); 
     s=Double.parseDouble(SField.getText()); 
     h=Double.parseDouble(HField.getText()); 


     Cone C = new Cone(r,s,h);//class cone 
     if (event.getSource() instanceof JButton) { 
      JButton clickedButton = (JButton) event.getSource(); 
      if (clickedButton == VolumeButton) { 
       Result.append("VOLUME = "+C.volume()+ "\n"); 
       ifV= true;//this's for clearing the fields for new entries. 


      } 
      if (clickedButton == AreaButton) { 
       Result.append("SURFACE AREA = "+C.surfaceArea()+ "\n"); 
       ifA= true;//this's for clearing the fields for new entries. 


      } 

      if(ifA&&ifV){ // clearing the fields for new entries. 
       SField.setText(CLEAR); 
       HField.setText(CLEAR); 
       RField.setText(CLEAR); 
       ifV=false; ifA= false;} 



     } 


     SList.addShape(C); 

    } 

     catch(NumberFormatException e){ 

     //Object of type "Exception__" already created 

      Ex.setVisible(true);//class "Exception__" is the one i've made for Exception window 

      SField.setText(CLEAR); 
      HField.setText(CLEAR); 
      RField.setText(CLEAR); 
      SField.setEditable(false); 
      HField.setEditable(false); 
      RField.setEditable(false); 


     }/*here, if the user clicked on -that okay in Exception window- 
and variable allow initialized to "true" those statements should extend. I guess? 
- everything worked correctly except for this ?*/ 
    if(Ex.allow){  
     SField.setEditable(true); 
     HField.setEditable(true); 
     RField.setEditable(true); } 


    } 

謝謝你所有的最終工作。

我加

Ex.allow(SField,HField,RField); 

的漁獲物。

,並添加類Exception__此方法:

 public void allow(JTextField js,JTextField jh,JTextField jr){ 

    HField =jh; 
    SField =js; 
    RField =jr; 
    } 

最後,向班Exception__的執行的動作:

SField.setEditable(true); 
      HField.setEditable(true); 
      RField.setEditable(true); 

WOHOOOO。感覺真棒。謝謝大家。我應該刪除我的問題還是將其留給可能面臨與我一樣的問題的其他人? :P

+0

發佈你有什麼代碼和異常的堆棧跟蹤,有人會很樂意幫助你。 – ChadNC

+0

如果您使用的是JDialog,它會自動禁用父窗口,直到它消失。 – Tharwen

+0

非常感謝,我添加了代碼。 – Lamia

回答

1

您的問題需要更多的細節。但是,如果你想展示一個「例外窗口,並允許用戶做任何事情,她不過這個窗口只以後,我想你需要的是一個MessageDialog: 見JOptionPane

如果您需要更多的細節被顯示,你可以創建你自己的模式JDialog

How to Make Dialogs

+0

對不起,不夠清楚。 我試過JOptionPane,它成功的工作,但我只是想根據異常窗口禁用和啓用該字段的想法。 非常感謝您的回答。 – Lamia

0

請通過書面隱伏文本字段:

jTextfield.setVisible(FASLE);

在表單代碼的構造函數中。比使用按鈕事件「Action - > Action Performed」並編寫代碼:

jTextfield.setVisible(true);

因此,只有在按鈕被點擊後,您的文本字段纔會顯示。

+0

我認爲setEditable在我的情況下更有用。 我會嘗試使用可編輯方法的建議。非常感謝。 – Lamia

相關問題