【问题标题】:How can I enable a text field when a button is clicked?单击按钮时如何启用文本字段?
【发布时间】: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


【解决方案1】:

您的问题需要更多细节。但是,如果您只想显示一个“异常窗口”并仅在用户关闭此窗口后才允许用户执行其他任何操作,我认为您只需要一个 MessageDialog: 见JOptionPane

如果您需要显示更多详细信息,您可以创建自己的模态JDialog。

见How to Make Dialogs

【讨论】:

  • 抱歉不够清楚。我尝试了 JOptionPane 并成功运行,但我只是尝试根据异常窗口禁用和启用该字段的想法。非常感谢您的回答。
【解决方案2】:

通过写入隐藏文本字段:

jTextfield.setVisible(fasle);

在表单代码的构造函数中。而不是使用按钮事件“Action -> Action Performed”并编写代码:

jTextfield.setVisible(true);

因此,只有在单击按钮后,您的文本字段才会可见。

【讨论】:

  • 我认为 setEditable 在我的情况下更有用。我将使用 Editable 方法尝试您的建议。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2020-10-20
  • 1970-01-01
  • 2018-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多