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

查看:36
本文介绍了在这种情况下,新的 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 当我们没有扩展我们的类时,我们如何访问这些帧及其 setDefaultCloseOperation()JFrames ?

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,则返回 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天全站免登陆