亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法使用 ActionListener 訪問 this.draw()

無法使用 ActionListener 訪問 this.draw()

侃侃無極 2022-01-12 16:58:24
我正在嘗試讓按鈕刷新窗口,但收到以下錯誤消息:Test.java:21: error: cannot find symbol                    this.draw();                        ^  symbol: method draw()1 error這是代碼:import javax.swing.*;import java.awt.event.*;public class Test {    JFrame frame;    public void createMainWindow() {        frame = new JFrame();        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        frame.setSize(800,600);        JButton refresh = new JButton("Refresh");        refresh.setBounds(620, 20, 100, 30);        refresh.addActionListener(new ActionListener() {                public void actionPerformed(ActionEvent e) {                    this.draw();                }            }            );        frame.setLayout(null);        frame.add(refresh);        frame.setVisible(true);        frame.setTitle("Title");    }    public void draw() {        // Code                                                                                                                                                                                                            frame.setVisible(true);    }}我顯然誤解了這一點。
查看完整描述

2 回答

?
溫溫醬

TA貢獻1752條經驗 獲得超4個贊

this.draw();

指ActionListner。

你要:

Test.this.draw();

引用 Test 類中的方法。


查看完整回答
反對 回復 2022-01-12
?
慕的地8271018

TA貢獻1796條經驗 獲得超4個贊

當您創建顯式匿名類時,this指的是ActionListener. 要繪制外部類,請使用Test.this.draw(),或者更簡單地說,用 lambda 替換整個偵聽器(從技術上講,它會創建一個匿名類,但不會接管this):

refresh.addActionListener(e -> this.draw());


查看完整回答
反對 回復 2022-01-12
  • 2 回答
  • 0 關注
  • 159 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號