在我開始之前,我是一名初學者程序員。單擊按鈕時如何啓用文本字段?
如何在單擊按鈕時啓用文本字段。
我有兩個框架,一個是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
發佈你有什麼代碼和異常的堆棧跟蹤,有人會很樂意幫助你。 – ChadNC
如果您使用的是JDialog,它會自動禁用父窗口,直到它消失。 – Tharwen
非常感謝,我添加了代碼。 – Lamia