2017-08-07 21 views
0

注:我的英語不是最好的所以請不要介意太多的語法錯誤。java;如何獲取Action Listener之外的變量?

嘿那裏,java初學者在這裏,無論如何,我編寫了我的CPS測試程序,就像第一個小程序一樣。不管怎麼說,這個問題可能已經問過,但我 需要得到一個變量的一個外面的ActionListener代碼:

public static void startB() { 
Font f = new Font(null, Font.BOLD , 0); 

    Font size = f.deriveFont(20f); 

    JLabel text = new JLabel(""); 
    text.setPreferredSize(new Dimension(250,250)); 
    text.setFont(size); 

    JButton b = new JButton(); 
    JFrame cps = new JFrame("CLICK"); 
    cps.setPreferredSize(new Dimension(400,400)); 
    cps.setLocationRelativeTo(null); 
    cps.setLayout(new FlowLayout()); 
    b.setText("<html> CLICK ME <br> As much as you can! <html> "); 
    cps.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
    cps.getContentPane().add(b , BorderLayout.CENTER); 
    cps.getContentPane().add(text, BorderLayout.CENTER); 
    cps.pack(); 
    cps.setVisible(true); 

    text.setText("<html> Starting in... <br> 3<html>"); 
    try { 
     TimeUnit.SECONDS.sleep((long)1.0); 
    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    text.setText("<html> Starting in... <br> 2<html>"); 
    try { 
     TimeUnit.SECONDS.sleep((long)1.0); 
    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    text.setText("<html> Starting in... <br> 1<html>"); 
    try { 
     TimeUnit.SECONDS.sleep((long)1.0); 
    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    text.setText("<html> CLICK! <html>"); 
    b.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      double clicks = 0; 
      clicks++; 
      // How to get Clicks variable out of the actionListener? 
     } 
    }); 
    try { 
     TimeUnit.SECONDS.sleep((long)10.0); 
    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    //I need get the Clicks Variable to here. 
} 

如果你能幫助我請的帖子作出迴應。謝謝。

回答

3

在這裏,您創建一個匿名類的ActionListener

b.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
    double clicks = 0; 
    clicks++; 
     // How to get Clicks variable out of the actionListener? 

    } 
    }); 

,您宣佈它裏面的clicks變量。
您可以在外部類中聲明clicks,但它不起作用,因爲編譯器期望clicksfinal。這與您的使用不兼容:您在actionPerformed()內進行變異。

的替代來實現你需要的是創造一個非匿名的實施ActionListener在那裏你將存儲clicks作爲一個字段,並提供一個getter檢索它的價值。

這可能是一個內部類:

private static class MyActionListener implements ActionListener { 

    private double clicks; 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     clicks++; 
    } 

    public double getClicks() { 
     return clicks; 
    } 

} 

而且你可以以這種方式使用它:

MyActionListener actionListener = new MyActionListener(); 
myComponent.addActionListener(actionListener); 
... 
// later retrieve clicks 
double clicks = actionListener.getClicks(); 
1

你只能訪問外部類的final變量的匿名內部類。但是如修改器final所示,一旦賦值,這個變量的值就不能再被改變。

爲了規避這一點,你可以使用一個AtomicInteger,而不是一個簡單的intdouble存儲的點擊,並宣佈這final Action監聽的變量外:

final AtomicInteger clicks = new AtomicInteger(0); 
b.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
     clicks.incrementAndGet(); 
    } 
} 
clicks.get(); // will return the desired number of invocations of actionPerformed 

這樣,你仍然可以使用匿名類爲你的動作聽衆。

從代碼風格的角度來看,它會更好 - 定義一個命名類,併爲點擊數提供一個getter,如其他答案中的建議。

+0

@davidxxx我剛剛提供了一個問題的答案,並已經提到,它不是最好的方式。 – dpr

+0

對不起,我不好讀。我明白了相反的意思。作爲這種警告的替代方法,這很好。 – davidxxx

相關問題