2014-05-09 121 views
0

下面的代碼顯示了Gui類的內部幀監聽器。內部框架偵聽器應該將save(JMenuItem)設置爲啓用或禁用,但它什麼也不做。我不知道問題出在哪裏。我嘗試了幾件事,但沒有任何作用。內部幀監聽器不工作

下面是代碼:

public class Gui implements InternalFrameListener, ActionListener{ 

MyInternalFrame frame; 
JMenuItem save; 

public Gui(JDesktopPane desktop, final JMenuItem save) { 
    // TODO Auto-generated constructor stub 

    frame = new MyInternalFrame(title, img); 
    this.save = save; 
    frame.setVisible(true); 
    desktop.add(frame); 
    try { 
      frame.setSelected(true); 
    } catch (java.beans.PropertyVetoException e) { 

    } 
    frame.addInternalFrameListener(this); 

     // options(name, desktop); 

} 

class MyInternalFrame extends JInternalFrame { 

    static final int xPosition = 30, yPosition = 30; 
    public MyInternalFrame(String title, ImagePlus img) { 
     super(title, true,true, true, true); 
     setSize(img.getHeight(), img.getWidth()); 

     // Set the window's location. 
     setLocation(xPosition * openFrameCount, yPosition * openFrameCount); 
    } 
} 

public void actionPerformed(ActionEvent e){ 
    frame.addInternalFrameListener(this); 
    System.out.println("beingg called"); 
} 



@Override 
public void internalFrameActivated(InternalFrameEvent arg0) { 
    // TODO Auto-generated method stub 

    save.setEnabled(true); 
} 

@Override 
public void internalFrameClosed(InternalFrameEvent arg0) { 
    // TODO Auto-generated method stub 

    save.setEnabled(false); 
} 

@Override 
public void internalFrameClosing(InternalFrameEvent arg0) { 
    // TODO Auto-generated method stub 
    save.setEnabled(false); 

} 

@Override 
public void internalFrameDeactivated(InternalFrameEvent arg0) { 
    // TODO Auto-generated method stub 
    save.setEnabled(false); 

} 

@Override 
public void internalFrameDeiconified(InternalFrameEvent arg0) { 
    // TODO Auto-generated method stub 

    save.setEnabled(true); 
} 

@Override 
public void internalFrameIconified(InternalFrameEvent arg0) { 
    // TODO Auto-generated method stub 

    save.setEnabled(false); 

} 

@Override 
public void internalFrameOpened(InternalFrameEvent arg0) { 
    // TODO Auto-generated method stub 
    save.setEnabled(true); 

} 

} 
+0

這應該拋出一個NullPointerException,因爲從不分配保存。 – Soana

+0

你的MyInternalFrame類在哪裏我沒有看到代碼中的任何地方????? –

+0

如果可能,請發佈MyInternalFrame類的源代碼。另外super(imp)給出了編譯時錯誤,你還沒有定義什麼是imp? –

回答

1

MCVE示出,這是一般的工作。我的猜測是,您正在使用save菜單項的錯誤實例。有疑問,添加調試輸出

@Override 
    public void internalFrameIconified(InternalFrameEvent arg0) 
    { 
     System.out.println("Disabling "+save); 
     save.setEnabled(false); 
    } 

您的方法。

import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JDesktopPane; 
import javax.swing.JFrame; 
import javax.swing.JInternalFrame; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.SwingUtilities; 
import javax.swing.event.InternalFrameEvent; 
import javax.swing.event.InternalFrameListener; 

public class InternalFrameListenerTest 
{ 
    public static void main(String[] args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       createAndShowGUI(); 
      } 
     }); 
    } 

    private static void createAndShowGUI() 
    { 
     JFrame frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().setLayout(new GridLayout(1, 1)); 

     JMenuBar menubar = new JMenuBar(); 
     JMenu menu = new JMenu("Menu"); 
     menubar.add(menu); 
     JMenuItem menuItem = new JMenuItem("Save"); 
     menu.add(menuItem); 
     frame.setJMenuBar(menubar); 
     JDesktopPane d = new JDesktopPane(); 
     Gui gui = new Gui(d, menuItem); 

     frame.getContentPane().add(d); 
     frame.setSize(500,500); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 
} 

class Gui implements InternalFrameListener, ActionListener 
{ 

    MyInternalFrame frame; 
    JMenuItem save; 

    public Gui(JDesktopPane desktop, final JMenuItem save) 
    { 
     frame = new MyInternalFrame("Title"); 
     this.save = save; 
     frame.setVisible(true); 
     desktop.add(frame); 
     try 
     { 
      frame.setSelected(true); 
     } 
     catch (java.beans.PropertyVetoException e) 
     { 

     } 
     frame.addInternalFrameListener(this); 
    } 

    class MyInternalFrame extends JInternalFrame 
    { 

     static final int xPosition = 30, yPosition = 30; 

     public MyInternalFrame(String title) 
     { 
      super(title, true, true, true, true); 
      setSize(200,100); 
      setLocation(50,50); 
     } 
    } 

    public void actionPerformed(ActionEvent e) 
    { 
     frame.addInternalFrameListener(this); 
     System.out.println("beingg called"); 
    } 

    @Override 
    public void internalFrameActivated(InternalFrameEvent arg0) 
    { 
     save.setEnabled(true); 
    } 

    @Override 
    public void internalFrameClosed(InternalFrameEvent arg0) 
    { 
     save.setEnabled(false); 
    } 

    @Override 
    public void internalFrameClosing(InternalFrameEvent arg0) 
    { 
     save.setEnabled(false); 
    } 

    @Override 
    public void internalFrameDeactivated(InternalFrameEvent arg0) 
    { 
     save.setEnabled(false); 
    } 

    @Override 
    public void internalFrameDeiconified(InternalFrameEvent arg0) 
    { 
     save.setEnabled(true); 
    } 

    @Override 
    public void internalFrameIconified(InternalFrameEvent arg0) 
    { 
     save.setEnabled(false); 
    } 

    @Override 
    public void internalFrameOpened(InternalFrameEvent arg0) 
    { 
     save.setEnabled(true); 
    } 
}