内部框架侦听器无法正常工作

时间:2014-05-09 09:01:08

标签: java swing user-interface jinternalframe

下面的代码显示了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);

}

 }

1 个答案:

答案 0 :(得分:1)

A 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);
    }
}