如何访问已经运行的类的实例,而不创建实际的对象 [英] How to access already running instance of a class without creating an actual object

查看:162
本文介绍了如何访问已经运行的类的实例,而不创建实际的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java GUI的问题。这很难解释,但我会尽我所能。我有2个GUI类一个是类I和其他是类G.类I是从主要方法启动。在类I中有一个类G的字段(实例)。原因是类收集重要信息并将其传递给类G的实例。当类I中按下一个按钮时,将类I框架可见性设置为false,类G的实例为true(显示G接口)。这里的问题是,我想能够在G中创建一个侦听器,将I的可见性恢复为true,从而显示先前编辑的窗口。我有一个解决方案处置所有的框架和创建一个新的实例,但只显示一个新的清除I的实例。以下是一些代码片段:



类I: / p>

字段:

  private JFileChooser j; 
private FileFilter filter;
private GUI g; //< ---它被初始化后不久。
private Font masterFont;
private JFrame frame;
private JButton done;

private JButton browse1;
private JButton browse2; .....

设置G visible和I invisible: / p>

  class Done implements ActionListener {

@Override
public void actionPerformed(ActionEvent e){

for(int i = 0; i <9; i ++){
System.out.println(array [i]);
}
$ b $ g g.setArray(array);
System.out.println(array);
setText()
frames.setVisible(false);
g.setVisible(true);
g.setVisible2(false);
if(g.clear.isSelected()){
frame.setVisible(true);
}


}

注意,这里我不能做一个实例,因为我不断得到Stack Overflow错误。



硬重置:这只是创建了一个新的实例,我的旧实例未正确关闭)

  private class Reset implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0){
frame.dispose();
frame2.dispose();

Runnable runnable = new Runnable(){
@Override
public void run(){
Intro g = new Intro();
g.setVisible(true);

}
};
EventQueue.invokeLater(runnable);
}
}



我想要能够访问

解决方案

您可以从传递的ActionEvent对象获取当前顶级窗口添加到ActionListener的actionPerformed方法中。获取引起侦听器的源对象通过 getSource()调用,然后调用 SwingUtilities.getWindowAncestor()在源上获取窗口(JFrame,JDialog,或者它可能是什么)。


I have a problem with Java GUI. It is hard to explain but I will do my best. I have 2 GUI classes one is class I and other is class G. Class I is initiated from main method. In the class I there is a field (instance of) class G. Reason being that class I collects vital information and passes it to instance of class G. When a button pressed in class I, that sets the class I frame visibility to false and instance of class G to true (showing up the G interface). Problem here is that I want to be able to make a listener in G that sets visibility of I back to true thus displaying the previously edited window. I had a solution of disposing of all frames and creating a new instance but that only shows a new cleared instance of I. Here are some code snippets:

Class I:

Fields:

private JFileChooser j;
private FileFilter filter;
private GUI g;  //<--- it is initialized shortly after.
private Font masterFont;
private JFrame frame;
private JButton done;

private JButton browse1;
private JButton browse2;.....

Sets G visible and I invisible:

class Done implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {

        for (int i = 0; i < 9; i++) {
            System.out.println(array[i]);
        }

        g.setArray(array);
        System.out.println(array);
        setText();
        frame.setVisible(false);
        g.setVisible(true);
        g.setVisible2(false);
        if (g.clear.isSelected()) {
            frame.setVisible(true);
        }


    }

Class G: Note, here I cannot make an instance of I because I keep getting Stack Overflow error.

Hard Reset: This one just creates new instance while disposing the rest (possibly wasteful because the old instance of I is not properly closed)

private class Reset implements ActionListener {
    @Override
        public void actionPerformed(ActionEvent arg0) {
            frame.dispose();
            frame2.dispose();

            Runnable runnable = new Runnable() {
                @Override
            public void run() {
                Intro g = new Intro();
                g.setVisible(true);

            }
        };
        EventQueue.invokeLater(runnable);
    }
}

I want to be able to access the "already running" instance of I without creating any new ones.

解决方案

You can get the current top level window from the ActionEvent object that is passed into your ActionListener's actionPerformed method. Get the source object that caused the listener to be called via getSource() and then call the SwingUtilities.getWindowAncestor() on the source to obtain the Window (JFrame, JDialog, or whatever it may be).

这篇关于如何访问已经运行的类的实例,而不创建实际的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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