我有以下代码,我创建了singleton / enum管理器,因此它跟踪已打开的内部帧的数量,当用户点击它时,我可以从任何帧中获取信息(激活帧)。但我不知道如何使这项工作,因为我想当用户点击第一帧(已经打开的5帧)中的假设时,我想要启用保存按钮。当没有窗口被激活时,我想要禁用该按钮。我放了一个内部框架监听器和一个组件监听器,但它们都没有工作。我希望有人能告诉我哪里出错了。
以下是代码:
public class Manager implements ActionListener, InternalFrameListener, ComponentListener{
private static int openFrameCount =0;
ImagePlus image;
private String tile;
final String SHOW ="show";
static ImageWindow m;
JMenuItem showInfo;
static JMenuItem save;
static JDesktopPane desktop;
InfoGui in;
public Manager(ImagePlus img, String title, JDesktopPane desktop, JMenuItem save){
image = img;
this.desktop = desktop;
this.tile = title;
this.save = save;
}
public enum WindowManager {
INSTANCE;
public MyInternalFrame frame;
private Map<ImagePlus, List<MyInternalFrame>> mapWindows;
private WindowManager(){
mapWindows = new HashMap<>(25);
}
public class MyInternalFrame extends JInternalFrame {
static final int xPosition = 30, yPosition = 30;
public MyInternalFrame(String title, ImagePlus img, JMenuItem save) {
super(title, true,true, true, true);
setSize(img.getWidth(),img.getHeight());
// Set the window's location.
setLocation(xPosition * openFrameCount, yPosition * openFrameCount);
save.setEnabled(true);
}
}
public JInternalFrame createWindowFor(ImagePlus image) {
List<MyInternalFrame> frames = mapWindows.get(image);
if (frames == null) {
frames = new ArrayList<>(25);
mapWindows.put(image, frames);
}
JPanel panel = new JPanel();
ImageCanvas c = new ImageCanvas(image);
c.getImage();
//panel2.add(new JLabel(new ImageIcon(c.getImage())));
m = new ImageWindow(image);
Image n = new Image();
//frame = new MyInternalFrame(title, img, save,m);
//ImageCanvas c = m.getCanvas();
ImagePlus im = new ImagePlus();
im.setImage(image);
frame = new MyInternalFrame(image.getTitle(), image, save);
m.centerNextImage();
image.getCanvas().setScaleToFit(true);
panel.add(m.getCanvas());
panel.setBackground(Color.white);
frame.add(panel);
frames.add(frame);
frame.setVisible(true);
frame.setAutoscrolls(true);
frame.setAutoscrolls(true);
frame.setOpaque(true);
desktop.add(frame);
try {
frame.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
return frame;
}
public List<MyInternalFrame> getFromFor(ImagePlus image) {
JInternalFrame frame = null;
return mapWindows.get(image.getTitle());
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
SADAPP.Manager.WindowManager.MyInternalFrame m = (SADAPP.Manager.WindowManager.MyInternalFrame) WindowManager.INSTANCE.createWindowFor(image);
}
@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);
}
@Override
public void componentHidden(ComponentEvent arg0) {
// TODO Auto-generated method stub
save.setEnabled(false);
}
@Override
public void componentMoved(ComponentEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void componentResized(ComponentEvent arg0) {
// TODO Auto-generated method stub
Rectangle r = WindowManager.INSTANCE.frame.getBounds();
m.getCanvas().fitToWindow(r);
System.out.println("resized- the real one");
}
@Override
public void componentShown(ComponentEvent arg0) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:0)
InternalFrameListener和ComponentListener接口的方法不会仅仅通过现有方法调用。您需要在框架上调用addInternalFrameListener
或addComponentListener
来告诉您有关您的听众的信息。
例如,在MyInternalFrame的构造函数中,您需要类似以下内容:
this.addInternalFrameListener(someInstanceOfTheManagerObject);