在这种情况下,新的JVM实例或反射会有所帮助吗? [英] Will new instance of JVM or reflection help in this case

查看:89
本文介绍了在这种情况下,新的JVM实例或反射会有所帮助吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前曾发布过一个问题,但没有明确的解决方案

I had a problem that I posted before but got no clear solution

如何防止JFrame关闭.

所以我要发布一个SSCCE,也许这可能有助于更好地了解所面临的问题

So I am posting a SSCCE may be this might help in better understanding the problem being faced

package myApp;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.swing.JFrame;

import App2.Applic2;

public class MYApp {

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static void main(String arg[]){
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setTitle("Application frame 1");
        f.setSize(200,200);
        f.setVisible(true);
        Class cls = Applic2.class;
        Object[] actuals = { new String[] { "" } };


        Method m = null;
        try {
            m=cls.getMethod("main", new Class[] { String[].class } );
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
            try {
                m.invoke(null,actuals);
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }

}

第二包

package App2;

import javax.swing.JFrame;

public class Applic2  {

    @SuppressWarnings("unused")
    public static void main(String args[]){

        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(200,200);
        f.setVisible(true);
        f.setTitle("This needs not to be changed");
        NewFrame3 Frame3 = new  NewFrame3();
    }

}

App2包的第二类.

Second class of App2 package.

package App2;

import javax.swing.JFrame;

public class NewFrame3 {

    public NewFrame3(){

        JFrame f = new JFrame();
            f.setTitle("f3");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setSize(200,200);
            f.setLocation(200, 200);
            f.setVisible(true);
    }

}

MYAPP调用Applic2的实例,该实例进一步调用NewFrame3的实例.就像我们看到我关闭'NewFrame3'实例还是Applic2实例一样,整个程序都关闭了(由于EXIT_ON_CLOSE)语句.

MYAPP invokes instance of Applic2 which further invokes instance of NewFrame3. Like we can see if I close instance of 'NewFrame3' or instance of Applic2 the entire program closes (due to EXIT_ON_CLOSE) statement.

我想要一个解决方案,其中MYAPP不应在关闭Applic2NewFrame3时关闭.

I want a solution in which MYAPP should not close on closing Applic2 or NewFrame3.

我无法对APPlic2或NewFrame3进行任何更改.通过反射如果我们尝试将EXIT_ON_CLOSE转换为DISPOSE_ON_CLOSE,而当我们没有扩展类JFrames时,如何访问这些框架及其setDefaultCloseOperation()?

I cannot make any changes to APPlic2 or NewFrame3. via reflection If we try to turn EXIT_ON_CLOSE to DISPOSE_ON_CLOSE How do we access these frames and their setDefaultCloseOperation() when we do not have our classes extended JFrames ?

在上述另一种解决方案中,应创建一个新的JVM实例,并应在此新JVM实例的新进程中执行Applic2.但是然后我遇到了runtime.exec将Java命令作为输入,而不是像method.invoke()这样的Java语句作为输入.

In another solution as mentioned a new JVM instance should be created and Applic2 should be executed in a new process on this new JVM instance. but then I encountered that runtime.exec takes Java commands as input and not Java statements like method.invoke().

我可以通过加载Applic2的加载程序访问Applic2,我只能访问内存中的Applic2的类文件,因此无法使用jar在runtime.exec()中运行.现在我该如何解决?

I have access to the Applic2 via loader that loaded Applic2 I can only access the class files of Applic2 in memory so no way I can use jar to run in runtime.exec(). Now how do I solve it?

将这些语句添加到MYApp类中可确保在单击框架的关闭按钮时没有任何反应,但事实并非如此

Adding these statements to MYApp classensures that on clicking close button of a frame nothing happens but this does not seem the be the case

Frame[] f2 = JFrame.getFrames();

            for(Frame fx: f2){
                System.out.println(fx.getTitle());
                fx.addWindowListener(new WindowAdapter(){
                      public void windowClosing(WindowEvent we){


                      }
                      });

,此代码需要添加到实例化的最后一帧,否则它会返回所有帧.即,如果将此帧添加到JFrame3类,则返回所有实例化的帧;如果返回添加到MyApp中的JFrame,则返回所有实例化的帧;如果添加了Applic2,则返回在MYApp和Applic2中实例化的帧.为什么会这样?

and this code needs to be added to the last frame that was instantiated else it does returns all frames. i.e, if this frame is added to JFrame3 class all instantiated frames are returned if added to MyApp JFrame in MyApp is returned and if added Applic2 then frames instantiated in MYApp and Applic2 are returned. Why this behaviour ??

推荐答案

您可以使用JFrame.getFrames(),它返回Frame的数组(您也可以getWindows()获得在当前的应用程序上下文).

You could use JFrame.getFrames() which returns an array of Frame (you could also getWindows() for a much lower level list of those windows created within the current application context).

然后,您需要遍历,检查每个框架是否符合您的要求.之后,您不需要反射,就可以直接访问框架

Then, you need to walk through, checking each frame to see if it meets your requirements. After, that, you don't need reflection, you gain direct access to the frames

与其他JVM进行通信的唯一方法是通过套接字通信(例如RMI).

The only only way to communicate with other JVM's is via socket comms (such as RMI).

示例更新

Frame[] listOfFrames = JFrame.getFrames();
for (Frame : listOfFrames) {
  if (frame instanceof JFrame) {

      JFrame aFrame = (JFrame)frame;

  }
}

这篇关于在这种情况下,新的JVM实例或反射会有所帮助吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆