Java 使用JMenuItem actionListener关注一个JInternalFrame对象

Java 使用JMenuItem actionListener关注一个JInternalFrame对象,java,swing,focus,jinternalframe,jmenuitem,Java,Swing,Focus,Jinternalframe,Jmenuitem,我正在尝试编写一个多文档界面的绘图应用程序。 图形应用程序的每个实例都显示在JDesktopane中的JInternalFrame对象中。 用户可以通过“文件新窗口”(JMenu)创建多个子窗口绘图应用程序 对于创建的每个内部框架,都会创建一个相应的描述性JMenuitem,并将其添加到JMenu(windows)中 然后,JMenu Window命令显示打开窗口的列表,并允许用户选择一个窗口 这就是我遇到的心理障碍,有没有办法将JMenuitem链接到创建的窗口 2013年7月6日 谢谢你的建

我正在尝试编写一个多文档界面的绘图应用程序。 图形应用程序的每个实例都显示在JDesktopane中的JInternalFrame对象中。 用户可以通过“文件新窗口”(JMenu)创建多个子窗口绘图应用程序

对于创建的每个内部框架,都会创建一个相应的描述性JMenuitem,并将其添加到JMenu(windows)中

然后,JMenu Window命令显示打开窗口的列表,并允许用户选择一个窗口

这就是我遇到的心理障碍,有没有办法将JMenuitem链接到创建的窗口

2013年7月6日 谢谢你的建议。 每次实例化一个ArrayList时,我都会将一个internalFrame添加到ArrayList中,然后通过操纵字符串标题来获取帧号(请参见下面的代码),从数组中检索它,但是ArrayList大小始终为1,并且始终是要实例化的最后一个帧……有什么想法吗

windowList = new ArrayList();
            windowList.add(internalFrame);
            //windowMenuItem = new ArrayList(200);
            windowMenuItem.add(newWindow);

            //picNumber = new int[200];
            //allow suer to select internalFrame according to menuItem selceted
            newDrawFrame.addActionListener(new ActionListener(){

          @Override
          public void actionPerformed(ActionEvent menuEvent) {



                  //call method to handle this event
                  winItemActionPreformed(menuEvent);
                  //frames[openFrameCount].setSelected(true);


          }//end actionPerformed

            }//end annon actionListener class

        );//end method calal to actionListener 
然后WinItemActionPerformed方法

 //handle menu selction for a internalFrame and set to front/on focus
public void winItemActionPreformed(ActionEvent menuEvent){
    //get selected menuItem via event and casdt to JMenuItem type
    //manipulate returned String object to retrive frame number

    JMenuItem tempItem = (JMenuItem) menuEvent.getSource();
    String tempItemString = tempItem.getText();
    int getFrameNumber = tempItemString.lastIndexOf("#");
    String frameNumberSelected = tempItemString.substring(getFrameNumber+1); 

    //convert String to int representong frameNumber in ArrayList
    int frameNumber;
    try 
    {
        frameNumber = Integer.parseInt(frameNumberSelected);
        JInternalFrame tempFrame = (JInternalFrame) windowList.get(frameNumber-1);
        tempFrame.toFront();
    }//end try
    catch (NumberFormatException ex) {
        System.err.println("No Such Number.");


    }//end catch 




}//end winActionPreformed
我会利用API

例如

public class SelectWindowAction extends AbstractAction {

    private JInternalFrame frame;

    public SelectWindowAction(JInternalFrame frame) {
        putValue(NAME, frame.getTitle());
    }

    public void actionPerformed(ActionEvent evt) {
        frame.setSelected(true);
    }
}
然后,您可以使用类似于

JMenuItem mi = new JMenuItem(new SelectWindowAction(frame));
更新:
没问题,ArrayList是在一个方法中创建的,所以我在其他地方进行了初始化,并将其设置为全局…ta的帮助:)

要更快地获得更好的帮助,请发布一个。+1同意;显示了一个
setSelected()
示例,并引用了一个菜单操作示例。。