2017-12-03 13 views
0

私は宿題に取り組んできましたが、奇妙な問題に遭遇しました。私は2つのJMenuItemsをクリックしてメソッドを呼び出そうとしていますが、何らかの理由で添付されたactionListenersが機能しません。JMenuItemのこのActionListenerが実行されないのはなぜですか?

私はそれが面白い演技だったが、そのコードも実行されないメソッド呼び出しと想定して、System.out.printlnをactionListenersに追加しました。

他のアクションリスナーと同様の問題については、私はStack Overflowを見ていました。リスナーパートを正しく実行していると思います。

私のコードをこのようにモジュール化したのは初めてのことです。

どこが間違っていますか?

完全なコード

import java.awt.*; 
import java.awt.event.*; 
import java.io.IOException; 
import java.net.URL; 
import javax.imageio.ImageIO; 
import javax.swing.*; 
import javax.swing.event.*; 


class Test100 extends JFrame implements ActionListener 

{ 
    //Setup Globals 
    public static JPanel panelInnerFrame = new JPanel(); 
    public static JDesktopPane desktop = new JDesktopPane(); 
    public static int internalFrameCounter; 
    public static int xOffset = 15, yOffset = 15, offSetIncrease = 15;//Windows position offset 
    public static JMenuBar menuTop = new JMenuBar(); 
    public static JMenu apps = new JMenu(); 
    public static JMenuItem fontApp, imageApp = new JMenuItem(); 

    public Test100() 
    { 

     panelMaker(); //Make our panel 
     menuBuilder(); //Call menu builder to build our menu 

    } 

    public void panelMaker() 
    { 
     //Desktop Pane, this is like a virtual desktop within our pane 
     desktop = new JDesktopPane(); 

     //Set Desktop Pane to act as our Content Pane 
     setContentPane (desktop); 

     //Set properties of our window 
     setTitle ("Test100"); 
     setSize (1024, 768); 
     setVisible (true); 

     //Create a new JPanel,this will sit in the innerframe, we can add things here. 
     panelInnerFrame = new JPanel(); 

    } 

    //Method - menuBuilder - A method to build JMenus 
    public void menuBuilder() 
    { 
     //Create a new menu bar 
     JMenuBar menuTop = new JMenuBar(); 
     setJMenuBar (menuTop); //We need to use the setJMenuBar with a JDesktopPane() 

     //Add a JMenu 
     JMenu apps = new JMenu("Apps"); 
     menuTop.add (apps); //Add Jmenu to the menu bar 

     //Add JMenu Item 
     JMenuItem fontApp = new JMenuItem("Font App"); 
     apps.add (fontApp); //Add Menu items to our JMenu 

     //Add JMenu Item 
     JMenuItem imageApp = new JMenuItem("Image App"); 
     apps.add (imageApp); //Add Menu items to our JMenu 

     //Add listeners 
     fontApp.addActionListener (this); 
     imageApp.addActionListener (this); 

    } 

    //Method - frameFontMaker - A method used to build an internal frame displaying fonts 
    public void frameFontMaker() 
    { 

      //Internal Frame, this is our floating box, it sits on our virtual desktop and takes multiple arguments 
      JInternalFrame innerframe = new JInternalFrame 
      ( "Font App", //frame name 
       true, //resizable 
       true, //closable 
       true, //maximizable 
       true //iconifiable 
      ); 

      //Set properties of our innerframe 
      innerframe.setSize (300, 300); 
      innerframe.setVisible (true); 
      innerframe.setLocation (xOffset, yOffset); 

      //Add a JPanel to our innerframe 
      innerframe.add (panelInnerFrame); 

      //Add the innerframe to the desktop 
      desktop.add (innerframe); 


    } 

    //Method - frameImageMaker - A method used to build an internal frame displaying images 
    public void frameImageMaker() 
    { 

     //Internal Frame, this is our floating box, it sits on our virtual desktop and takes multiple arguments 
     JInternalFrame innerframe = new JInternalFrame 
     ( "Image App", //frame name 
      true, //resizable 
      true, //closable 
      true, //maximizable 
      true //iconifiable 
     ); 

     //Set properties of our innerframe 
     innerframe.setSize (300, 300); 
     innerframe.setVisible (true); 
     innerframe.setLocation (xOffset, yOffset); 

     //Add a JPanel to our innerframe 
     innerframe.add (panelInnerFrame); 

     //Add the innerframe to the desktop 
     desktop.add (innerframe); 


    } 

    public void actionPerformed (ActionEvent e) 
    { 
     //If fontApp menu item selected 
     if (e.getSource() == fontApp) 
     { 
      frameFontMaker(); 
      //System.out.println ("Test"); 
     } 

     //If imageApp menu item selected 
     if (e.getSource() == imageApp) 
     { 
      frameImageMaker(); 
      //System.out.println ("Test"); 
     } 
    } 

    public static void main (String[] args) 

    { 
     new Test100(); 
    }//End Main 


}//End Class 

答えて

1

あなたのクラスが2つの余分のJMenuItemはそれが必要以上にオブジェクトがあるようにするには、のJMenuItem変数をシャドウイングしています。 2つはメニューに追加し、2つはクラスフィールドに追加しません。 actionPerformed内では、クラスフィールドと等しいかどうかをチェックします。

class Test100 extends JFrame implements ActionListener { 

    //...  

    public static JMenuItem fontApp, imageApp = new JMenuItem(); // fields -- never added to GUI 

    //.... 


    //Method - menuBuilder - A method to build JMenus 
    public void menuBuilder() 
    { 
     // .... 

     // *** re-declaring the variables here!! *** these are different variables and hold 
     // different objects 
     JMenuItem fontApp = new JMenuItem("Font App"); 
     apps.add (fontApp); //Add Menu items to our JMenu 

     //Add JMenu Item 
     JMenuItem imageApp = new JMenuItem("Image App"); 
     apps.add (imageApp); //Add Menu items to our JMenu 

     // ...... 

    } 

解決策:変数をシャドー(または再宣言)しないでください。

変更この:これに

JMenuItem fontApp = new JMenuItem("Font App"); 
// ... 
JMenuItem imageApp = new JMenuItem("Image App"); 
//... 

fontApp = new JMenuItem("Font App"); 
//... 
imageApp = new JMenuItem("Image App"); 
//... 

は違いに注意してください?

+0

これは、なぜ私がnoobであるために頻繁にdownvotedになっても、スタックオーバーフローになる理由です。 – Ninja2k

+0

@ Ninja2k:うまくいきましたが、もっと重要なのは、元のコードがうまくいかなかった理由を今理解できますか?これをやり直すのを避けるためには、概念を理解する必要があります。 –

+0

はい、私は本質的に、グローバルとローカルの2つのオブジェクトを作成し、ローカルのブランクオブジェクトを参照していました。 – Ninja2k

関連する問題