我下面这两个类的观点是,当用户假设打开5个窗口(包含图像的帧),然后用户点击第二帧并想要关于该图像的信息,我如何获得该帧信息。在我之前的问题中,有人建议做单例枚举,我有点理解并实现了它,但我就是无法理解当用户单击内部框架时,管理器类将如何将路径发送给Info类。
我之前的问题:如何跟踪以前打开的内部帧
这是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: nn";
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);
这是我有麻烦!!!!!!我希望任何人都能发现这个错误。提前感谢。
我看到的一件事是,通常会使用addActionListener()
来注册侦听器,而不是在Java中直接调用actionPerformed
。就像:
someComponent.addActionListener(new Manager());
操作监听器,也就是Manager,必须与一个实际的组件相关联