2013-10-28 23 views
0

我在一個更大的形式張貼了這個問題前面,並希望能在一個方式,讓社會來幫助我更好地再重新發布,通過對您的所有傾倒的信息較少。ActionListener的失敗 - Java進行,每個任務給的JMenuItem

從根本上說,我有這樣的代碼,解釋如下:

for(JMenuItem x : chem3_x_y){ 
    x.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent k) { 
     new SwingImplementation(1, tt+1); 
    } 
    }); 
    gEleven[6].add(x); 
    tt++; 
} 
tt=0; 

首先,我循環通過所有的JMenuItem-S陣列中的chem3_x_y

然後,我添加一個ActionListener爲 'X'或陣列,從而產生具有1的參數的新SwingImplementation的每個項目,並且變量「TT」

在此之後,JMenu的gEleven接收的JMenuItem中的問題,我然後增加1 TT。

上述代碼的目的是自動添加到JMenuItems gEleven,和的ActionListener到每個菜單項的過程。

問題是,爲變量'tt'傳遞的參數始終是相同的,1,就好像將'tt'爲1的參數應用於每個JMenuItem的動作偵聽器 - 爲什麼會這樣?我可以使用不斷增加的TT創造每 JMenuItem的一個的ActionListener ,用(1,TT)的說法,而不TT始終爲1

+0

你的代碼格式是非常糟糕使其很難爲我們理解你的代碼。我儘可能地修復了它。 –

+0

爲了儘快提供更好的幫助,請發佈[SSCCE](http://sscce.org/)。我相信你已經被告知,並且使用更明智的屬性名稱。如果你忽略了我們的答案和建議,也許我們應該忽略你的問題。 –

+0

我懷疑你的問題在別處,你需要做一些調試。 –

回答

3

您的問題是tt不會增加,直到偵聽器調用。雖然你的動作監聽者的代碼是內聯,事實上,它確實不是所有的同步運行 - actionPerformed()不叫,直到動作監聽被調用。因此tt在循環時永不改變。

你需要的是這樣的:

class MyListener implements ActionListener { 
    private final int tt; 

    MyListener (int tt) { 
    this.tt = tt; 
    } 

    public void actionPerformed(ActionEvent k) { 
    new SwingImplementation(1, tt+1); 
    } 
} 

for(JMenuItem x : chem3_x_y){ 
    x.addActionListener(new MyListener(tt++)); 
    gEleven[6].add(x); 
} 

讓你遞增TT和結果存儲在一個final現場綁需要該值的監聽器。

+0

好的猜測,沒有'tt'的聲明。 – trashgod

+1

它必須在包含該方法或某個父類或包含該類的類的類上,或者它必須是最終才能在內部類中可見的,在這種情況下,它永遠不會增加。 –

2

編輯: 而不是 tt + 1,您可能的意思是 tt++ 。我從發佈的代碼中看不出來。也許,尋找一個tt該陰影預期的變量。在此基礎上example,相應的代碼下面使用++i。也可以考慮使用Action封裝監聽器。

image

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.awt.event.ActionEvent; 
import java.io.File; 
import javax.swing.AbstractAction; 
import javax.swing.Action; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.JToolBar; 

/** 
* @see https://stackoverflow.com/a/19626219/230513 
* @see https://stackoverflow.com/questions/4038605 
*/ 
public class FileMenu { 

    public static void main(String[] args) { 

     EventQueue.invokeLater(new Runnable() { 

      public void run() { 
       new FileMenu().create(); 
      } 
     }); 
    } 

    void create() { 
     File userDir = new File(System.getProperty("user.dir")); 
     File[] files = userDir.listFiles(); 

     JMenu menu = new JMenu("Recent Files"); 
     JToolBar toolBar = new JToolBar(JToolBar.VERTICAL); 
     JLabel label = new JLabel(" ", JLabel.CENTER); 
     int i = 0; 
     for (File f : files) { 
      if (f.isFile() && !f.isHidden()) { 
       RecentFile rf = new RecentFile(f, label, ++i); 
       menu.add(new JMenuItem(rf.getAction())); 
       toolBar.add(rf); 
      } 
     } 
     JMenuBar menuBar = new JMenuBar(); 
     menuBar.add(menu); 

     JFrame f = new JFrame("FileMenu"); 
     f.setJMenuBar(menuBar); 
     f.add(toolBar, BorderLayout.CENTER); 
     f.add(label, BorderLayout.SOUTH); 
     f.pack(); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    } 
} 

class RecentFile extends AbstractAction { 

    private final File file; 
    private final JLabel label; 

    public RecentFile(final File file, final JLabel label, int i) { 
     this.file = file; 
     this.label = label; 
     this.putValue(Action.NAME, String.valueOf(i) + " " + file.getName()); 
     this.putValue(Action.SHORT_DESCRIPTION, file.getAbsolutePath()); 
    } 

    public void actionPerformed(ActionEvent e) { 
     label.setText(file.getName()); 

    } 

    public Action getAction() { 
     return this; 
    } 
} 
相關問題