仅允许一个JInternalFrame实例

时间:2012-03-11 17:09:45

标签: java swing jinternalframe single-instance

我创建了一个包含多个 JInternalFrames 的Swing应用程序,它会在鼠标单击时添加到JDesktopPane。我想要只有一个实例 相同的内部框架 才能出现在DesktopPane上。我不希望同一个框架在用户出现两次时打开框架。如果框架已打开,则应显示错误消息。

非常感谢:)

3 个答案:

答案 0 :(得分:6)

  

我创建了一个包含多个JInternalFrames的Swing应用程序......

  

我只想要一个相同内部框架的实例...

因此,将Singleton Pattern应用于每个JInternalFrame子类。 如果类符合单例模式,您将只能使用一个类的实例。

答案 1 :(得分:5)

不要打扰单身反模式。相反,只需为您的类提供一个JInternalFrame字段,并在类的构造函数或变量声明中创建JInternalFrame的一个实例,并且不要在鼠标单击时创建一个新实例,而是显示一个已经创建了。例如,在mousePressed方法中,只需调用myInternalFrame.setVisible(true)即可。这样,如果它是不可见的,现在它是可见的,如果它已经可见,那么它仍然是可见的和不变的。简单明了。

答案 2 :(得分:1)

HovercraftFullOfEeels,一个跟随我自己的人,说不要使用Singleton模式,我将不同意。 Singleton可以是一种非常强大的方法来简化事物并避免样板代码,同时保持系统的强大和易于维护。此外,他建议你只是显示一个已经打开的JInternalFrame,这有两个方面存在缺陷:1)在代码级别难以管理,使得系统脆弱,将来很难更新2)最终用户关闭并重新打开屏幕,他们期望新实例,刷新数据和全新组件。预计关闭和重新开放将是相同的实例。

另一个回答者说要使用Singleton,但没有给出具体的例子。所以,我将为您提供我为我的应用程序开发的代码:

这是Singleton JInternalFrame的类(注意:它扩展JPanel的原因是我可以在GUI Builder中轻松使用它):

public abstract class VPanel extends JPanel {

    public static JDesktopPane desktopPane;

    public static void installDesktopPane(JDesktopPane desktopPane) {
        VPanel.desktopPane = desktopPane;
    }

    public VPanel(String name) {
        this.name = name;
        if(desktopPane == null)
            throw new IllegalStateException("VPanel is being used with a null desktop pane.");
    }
    static LinkedHashMap<Class, VPanel> self_panel_map;

    JInternalFrame self_jif;
    protected VPanel self_panel;
    boolean loading;
    boolean showing;

    public final String name;
    public abstract void init();

    public static VPanel showPanel(VPanel newInstance) {
        if(self_panel_map == null)
            self_panel_map = new LinkedHashMap<>();
        Class newInstanceClass = newInstance.getClass();
        if(self_panel_map.containsKey(newInstanceClass)) {
            VPanel oldInstance = self_panel_map.get(newInstanceClass);
            oldInstance.showing = oldInstance.self_jif.isVisible();
            if(!oldInstance.loading && !oldInstance.showing) {
                newInstance.loading = true;
                newInstance.self_panel = newInstance;
                newInstance.self_jif = new JInternalFrame(newInstance.name, true, true, true, true);
                newInstance.self_panel.init();
                self_panel_map.put(newInstanceClass, newInstance);
                return newInstance;
            } else if(oldInstance.showing) {
                try {
                    oldInstance.self_jif.setSelected(true);
                } catch (PropertyVetoException e) {
                    handleError(e);
                }
            }
            return oldInstance;
        } else {
            newInstance.loading = true;
            newInstance.self_panel = newInstance;
            newInstance.self_jif = new JInternalFrame(newInstance.name, true, true, true, true);
            newInstance.self_panel.init();
            self_panel_map.put(newInstanceClass, newInstance);
            return newInstance;
        }
    }

    public void setVisible() {

        self_jif.add(self_panel);
        self_jif.pack();
        self_jif.setVisible(true);
        desktopPane.add(self_jif);
        centerJIF();
        try {
            self_jif.setSelected(true);
        } catch (PropertyVetoException e) {
            handleError(e);
        }
        loading = false;
    }

    private static void handleError(Exception e) {
        e.printStackTrace();
    }

    public void centerJIF() {
        Dimension desktopSize = desktopPane.getSize();
        Dimension JInternalFrameSize = self_jif.getSize();
        int width = (desktopSize.width - JInternalFrameSize.width) / 2;
        int height = (desktopSize.height - JInternalFrameSize.height) / 2;
        self_jif.setLocation(width, height);
    }
}

以下是实现它的示例代码:

public static void main(String[] args) {
    JFrame jf = new JFrame("MainFrame");
    JDesktopPane jdp = new JDesktopPane();
    jf.setExtendedState( jf.getExtendedState()|JFrame.MAXIMIZED_BOTH );

    VPanel.installDesktopPane(jdp); // This only needs to happen once throughout the entire application lifecycle.

    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("Panels");
    JMenuItem menuItem = new JMenuItem("Open Test Panel");
    menuItem.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Test_VPanel.showPanel(new Test_VPanel()); // Every time you show the panel, you create a new instance.
            // But this new instance is only used if it is needed. The init() method is only called if it is going
            // To show a new instance.
        }
    });
    menu.add(menuItem);
    menuBar.add(menu);
    jf.setJMenuBar(menuBar);


    jf.setContentPane(jdp);

    jf.setVisible(true);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

static class Test_VPanel extends VPanel {

    public Test_VPanel() {
        super("Test Panel");
    }

    @Override
    public void init() {
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        JLabel label = new JLabel("JLabel");
        JTextField textField = new JTextField();

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1;
        gbc.gridwidth = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(4,4,4,4);
        add(label, gbc);

        gbc.gridy = 1;
        add(textField, gbc);

        setVisible(); // This needs to be called at the end of init()
    }
}

我从不需要调用方法对新实例执行任何操作,但只是注意,showPanel返回使用的实例,无论是旧实例还是新实例。因此,如果您需要对实例执行某些操作,则可以执行此操作:

Test_VPanel panel = Test_VPanel.showPanel(new Test_VPanel());
panel.something();
...

一旦我决定使用Singleton JIF这条路线,生活对我来说就容易多了。强烈推荐。

相关问题