【发布时间】:2012-05-23 04:28:57
【问题描述】:
在开始之前,我是一名初级程序员。
如何在单击按钮时启用文本字段。
我有两个框架,一个具有 JFields,另一个用于异常。 当异常发生时 > setEditable(false) 但是,一旦用户单击确定按钮(我在异常中所做的),我应该做出什么声明来启用 JFields?
我尝试将静态布尔值添加到异常框架,并在此类执行的操作中将该布尔值初始化为 true。
在另一个类中,我添加了一个 if 语句,如果该布尔值为 true,则 setEditable(true)
-=========- 该程序的要点是,当异常发生时,用户在关闭异常窗口之前无法在字段中输入任何内容。
希望你能帮助我。
程序员们,全心全意。
为异常窗口框架执行的操作代码(具有确定按钮。)
public void actionPerformed(ActionEvent e){
{
allow=true; //static boolean
Container TheFrame = OKButton.getParent();
do TheFrame = TheFrame.getParent();
while (!(TheFrame instanceof JFrame));
((JFrame) TheFrame).dispose();
}
为 THE MAIN PROGRAM 执行的操作代码(包含三个字段,一旦用户输入非数字就会出现异常) 我添加了一些 cmets 来澄清一下。
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);
哇哦。感觉太棒了哈哈。谢谢大家。我应该删除我的问题还是将其留给可能面临与我相同问题的其他人? :P
【问题讨论】:
-
发布您拥有的代码和异常的堆栈跟踪,有人会很乐意帮助您。
-
如果你使用JDialog,它会自动禁用父窗口直到它消失。
-
非常感谢,我已经添加了代码。
标签: java swing jframe field jbutton