如何在单击按钮时启用文本字段?

时间:2012-05-15 15:56:33

标签: java swing jframe field jbutton

在开始之前,我是初学程序员。

如何在单击按钮时启用文本字段。

我有两个框架,一个有JFields,另一个有异常。 当发生异常时>的 setEditable(假) 但是,一旦用户点击okay按钮,我应该做什么声明来启用JFields - 我在异常中做了什么?

我试图将静态布尔值添加到异常帧中,并且在此类执行的操作中,我将该布尔值初始化为 true

在另一个类中,我添加了一个if语句,如果该布尔值为true,则setEditable(true)

- ====== - 该程序的要点是,当异常发生时,用户在关闭异常窗口之前不能在字段中输入任何内容。

我希望你能帮帮我。

拥有所有的爱,程序员。

为EXCEPTION WINDOW FRAME执行的操作代码(有Okay按钮。)

  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

2 个答案:

答案 0 :(得分:1)

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

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

请参阅How to Make Dialogs

答案 1 :(得分:0)

通过写:

使文本字段隐藏

jTextfield.setVisible(FASLE);

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

jTextfield.setVisible(真);

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