开发者社区> 问答> 正文

Java报错f cannot be resolved ?报错

Java新手 菜鸟一枚,请问大神 报错f cannot be resolved,怎么修改图片说明

展开
收起
爱吃鱼的程序员 2020-06-24 13:19:21 577 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    f在wrw类中的main方法定义,其可见范围只在main方法中,所以后面addaction类中使用f就会报错:fcannotberesolved。

    解决方法是通过匿名类的方式在main方法中添加Button的监听,示例代码如下:

    importjava.awt.*;importjava.awt.event.*;publicclassExample{publicstaticvoidmain(String[]args){Framef=newFrame("ActionListenerExample");finalTextFieldtf=newTextField();tf.setBounds(50,50,150,20);Buttonb=newButton("ClickHere");b.setBounds(50,100,60,30);b.addActionListener(newActionListener(){//这是关键代码publicvoidactionPerformed(ActionEvente){tf.setText("WelcometoHere.");}});f.add(b);f.add(tf);f.setSize(400,400);f.setLayout(null);f.setVisible(true);}}

    结果如下图:

    如果对您有帮助,请采纳答案好吗,谢谢!

    回答下你问楼上的问题,注意传递参数就可以了,用匿名内部类只是更简洁更方便,另外,最好去学一下匿名内部类。

    2020-06-24 13:19:37
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载