跟踪窗口及其信息

时间:2014-05-21 18:37:33

标签: java user-interface

我对下面这两个类的看法是,当用户想要打开5个窗口(包含图像的帧)然后用户点击第2帧并想要有关该图像的信息时,我如何获得该帧信息。在我之前的问题中,有人建议做单例枚举,我有点理解它并实现但我无法理解当用户点击内部框架时管理器类将路径发送到的内部框架Info类。

我之前的问题:How to keep track of previous opened internal frames

这是Manager类:

public class Manager implements ActionListener{

private static int openFrameCount =0;
ImagePlus image;
private static String title;
final String SHOW ="show";
static ImageWindow m;
static JMenuItem showInfo;
static JMenuItem save;
static JDesktopPane desktop;
InfoGui in;
JMenuItem fft;

public Manager(ImagePlus img, String title, JDesktopPane desktop, JMenuItem save, JMenuItem fft, JMenuItem showInfo2){
    image = img;
    this.desktop = desktop;
    this.title = title;
    this.save = save;
    this.fft = fft;
    this.showInfo= showInfo;
}

public enum WindowManager implements InternalFrameListener, ComponentListener {

    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) {

        }
        frame.addInternalFrameListener(this);  
        frame.addComponentListener(this);
        return frame;

    }

    public List<MyInternalFrame> getImage() {

        JInternalFrame frame = null;
        return mapWindows.get(title);

    }

    @Override
    public void internalFrameActivated(InternalFrameEvent arg0) {
        // TODO Auto-generated method stub
        save.setEnabled(true);
        //showInfo.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);
        //showInfo.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);
        //showInfo.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 = frame.getBounds();

        m.getCanvas().fitToWindow(r);
        System.out.println("resized- the real one");
    }

    @Override
    public void componentShown(ComponentEvent arg0) {
        // TODO Auto-generated method stub

    }
}

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    SADAPP.Manager.WindowManager.MyInternalFrame m = (SADAPP.Manager.WindowManager.MyInternalFrame) WindowManager.INSTANCE.createWindowFor(image);
}

 }

这是我的Info类:

public class Info implements ActionListener{

private static int openFrameCount =0;
ImagePlus image;
private static String title;
final String SHOW ="show";
static ImageWindow m;
JMenuItem showInfo;
static JMenuItem save;
static JDesktopPane desktop;
InfoGui in;
JMenuItem fft;

public Info(ImagePlus img, String title, JDesktopPane desktop, JMenuItem save, JMenu fft){
    image = img;
    this.desktop = desktop;
    this.title = title;
    this.save = save;
    this.fft = fft;
}

public enum WindowManager implements InternalFrameListener, ComponentListener {

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

        frame = new MyInternalFrame(image.getTitle(), image, save);
        JPanel panel = new JPanel();
        JPanel panel2 = new JPanel();

        JTextArea text = new JTextArea();
        String info = "Image Information: \n\n";
        info += "Image Name: "+image.getTitle()+ "\n";
        info += "Pixel Size: ";
        info += (image.getHeight() +" x "+image.getWidth() +"\n");
        info += ("Bit Depth: " + image.getBitDepth() + "-bit"+"\n");
        info+= "Bytes/Pixel: "+image.getBytesPerPixel() + "\n";
        info += "Type of: "+image.getType()+ "\n";
        info += "Diplay range: " + image.getDisplayRangeMax() +"\n";
        info += "Display range(min): " + image.getDefault16bitRange() + "\n";
        info += image.getFileInfo();
        info += image.getInfoProperty();
        //System.out.println(info);

        text.setText(info);
        System.out.println(image.getHeight());
        text.setVisible(true);
        text.setEditable(false);
        //text.setBackground(new Color(0,0,0,0));
        panel.add(text);
        JScrollPane scroll = new JScrollPane(panel);

        ImageWindow m = new ImageWindow(image);
        panel2.add(new JLabel(new ImageIcon(image.getImage())));
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, scroll, panel2);

        splitPane.setOpaque(true);
        panel.setMinimumSize(new Dimension(500,100));
        panel.setBackground(new Color(0,0,0,0));
        panel2.setMinimumSize(new Dimension(200,30));
        splitPane.setOneTouchExpandable(true);
        splitPane.setDividerLocation(290);

        frame.add(splitPane);
        frame.setVisible(true); 
        desktop.add(frame);
        try {
                frame.setSelected(true);
        } catch (java.beans.PropertyVetoException e) {

        }
       frame.addInternalFrameListener(this);  
        return frame;

    }

    public List<MyInternalFrame> getFromFor(ImagePlus image) {

        JInternalFrame frame = null;
        return mapWindows.get(title);

    }

    @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 = frame.getBounds();

        m.getCanvas().fitToWindow(r);
        System.out.println("resized- the real one");
    }

    @Override
    public void componentShown(ComponentEvent arg0) {
        // TODO Auto-generated method stub

    }
}

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    JInternalFrame b = WindowManager.INSTANCE.createWindowFor(image);
}

 }

在主课程中,如果用户点击打开并选择图像,我会这样做:

 v= new Manager(img, s, desktop, save, fft,showInfo);
 ActionEvent e = null;
v.actionPerformed(e);

这将打开图像窗口,现在假设用户再打开4个图像,因此这被称为4次。现在我希望当用户点击一个框架然后点击showInfo时,我希望info类获取图像标题并显示信息。所以这就是我的工作:

List<MyInternalFrame> v =  WindowManager.INSTANCE.getImage();
ImagePlus i = new ImagePlus(v.get(0).getTitle());    //This is where the image is null
Info n = new Info(i,i.getTitle(), desktop, save, fft);
ActionEvent s = null;
n.actionPerformed(s);

这是我遇到麻烦的地方!!!!!!我希望有人能发现错误。提前致谢。

1 个答案:

答案 0 :(得分:0)

我看到的一件事是,通常会使用addActionListener()注册一个监听器,而不是直接在Java中调用actionPerformed。所以喜欢:

someComponent.addActionListener(new Manager());

因此,您的经理必须将一个动作监听器与实际组件

相关联