Will new instance of JVM or reflection help in this case

前端 未结 3 1265
无人及你
无人及你 2021-01-14 18:57

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

How to prevent JFrame from closing.

So I am posting a SSCCE may be this might help in better u

3条回答
  •  攒了一身酷
    2021-01-14 19:22

    So have completed the solution with the help provided by MadProgrammer amd mKorbel and here is the updated MYApp class

    package myApp;
    
    import java.awt.Frame;
    import java.awt.Window;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    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 } );
    
    
                Method[] m1  =Frame.class.getDeclaredMethods();
    
    
    
    
    
    
    
            } 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();
                }
    
    
                Frame[] f2 = JFrame.getFrames();
                //  Window[] f23=  JFrame.getFrames();
    
                    for(Frame fx: f2){
                        System.out.println(fx.getTitle());
                        //  fx.setVisible(false);
    
                          if (fx instanceof JFrame) {
    
                              JFrame aFrame = (JFrame)fx;
                              aFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
                          }
                        }
    
        }
    
    }
    

提交回复
热议问题