1

我需要创建一个具有单个窗口且没有任何弹出窗口的应用程序。一切都使用相同的窗口,只改变内容。我将使用 swing 创建应用程序。

我读过Java: single frame vs multiple frames但我认为这不符合我的要求,因为当应用程序增长时,它会变得更难维护。

我想要 MVC 设计。我正在考虑创建某种控制器堆栈,其中控制器将加载视图和模型。因此,当我需要返回时,只需弹出当前控制器并使用前一个控制器。我的设计可能是错误的,如果有人对如何实现此类应用程序有任何建议,请随时发表评论。

谢谢你的时间。

我已经尝试创建原型类,它可以容纳一堆容器以在它们之间切换。

public class WindowManager 
{
protected Stack<Container> frames;// contains frames stack to navigate
protected JFrame wnd; // frame to show

public WindowManager()
{
    this.frames = new Stack<>();
    this.wnd = new JFrame();
    wnd.setSize(640, 480);
    //wnd.setVisible(true);
}

public void addFrame(Container c)
{
    this.frames.push(this.wnd.getContentPane());
    this.wnd.setContentPane(c);
}

public WindowManager removeFrame()
{
    Container c = this.frames.pop();
    wnd.setContentPane(c);
    return this;
}

public void showWindow()
{
    wnd.setVisible(true);
}

}

代替容器堆栈的是控制器堆栈,它将加载视图和模型。

4

1 回答 1

2

您可能应该创建多个JPanel对象并将它们放入CardLayout中,因此一次只能看到一页。请参见以下示例:

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class CardLayoutExample extends JFrame
{
    public CardLayoutExample ()
    {
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        getContentPane ().setLayout (new CardLayout ());
        getContentPane ().add (new Fruits (), "Fruits");
        getContentPane ().add (new Seasons (), "Seasons");
        getContentPane ().add (new Colors (), "Colors");
        getContentPane ().add (new Towns (), "Towns");
    }

    public void switchTo (String page)
    {
        ((CardLayout)getContentPane ().getLayout ()).show (getContentPane (), page);
    }

    public static void main (String [] args)
    {
        SwingUtilities.invokeLater (new Runnable()
        {
            @Override
            public void run ()
            {
                CardLayoutExample example = new CardLayoutExample ();
                example.pack ();
                example.setVisible (true);
            }
        });
    }

    private class Colors extends JPanel
    {
        public Colors ()
        {
            Box box = Box.createHorizontalBox ();
            box.add (new JButton (new AbstractAction("Red")
            {
                @Override
                public void actionPerformed (ActionEvent e)
                {
                    switchTo ("Fruits");
                }
            }));
            box.add (Box.createHorizontalStrut (8));
            box.add (new JButton (new AbstractAction("Green")
            {
                @Override
                public void actionPerformed (ActionEvent e)
                {
                    switchTo ("Seasons");
                }
            }));
            box.add (Box.createHorizontalStrut (8));
            box.add (new JButton (new AbstractAction("Blue")
            {
                @Override
                public void actionPerformed (ActionEvent e)
                {
                    switchTo ("Towns");
                }
            }));

            setLayout (new BorderLayout ());
            add (box, BorderLayout.CENTER);
        }
    }

    private class Fruits extends JPanel
    {
        public Fruits ()
        {
            Box box = Box.createVerticalBox ();
            box.add (new JButton (new AbstractAction("Apple")
            {
                @Override
                public void actionPerformed (ActionEvent e)
                {
                    switchTo ("Colors");
                }
            }));
            box.add (Box.createVerticalStrut (8));
            box.add (new JButton (new AbstractAction("Orange")
            {
                @Override
                public void actionPerformed (ActionEvent e)
                {
                    switchTo ("Seasons");
                }
            }));
            box.add (Box.createVerticalStrut (8));
            box.add (new JButton (new AbstractAction("Pear")
            {
                @Override
                public void actionPerformed (ActionEvent e)
                {
                    switchTo ("Towns");
                }
            }));

            setLayout (new BorderLayout ());
            add (box, BorderLayout.CENTER);
        }
    }

    private class Seasons extends JPanel
    {
        public Seasons ()
        {
            Box box = Box.createHorizontalBox ();
            box.add (new JButton (new AbstractAction("Winter")
            {
                @Override
                public void actionPerformed (ActionEvent e)
                {
                    switchTo ("Colors");
                }
            }));
            box.add (Box.createHorizontalStrut (8));
            box.add (new JButton (new AbstractAction("Spring")
            {
                @Override
                public void actionPerformed (ActionEvent e)
                {
                    switchTo ("Fruits");
                }
            }));
            box.add (Box.createHorizontalStrut (8));
            box.add (new JButton (new AbstractAction("Summer")
            {
                @Override
                public void actionPerformed (ActionEvent e)
                {
                    switchTo ("Towns");
                }
            }));

            setLayout (new BorderLayout ());
            add (box, BorderLayout.CENTER);
        }
    }

    private class Towns extends JPanel
    {
        public Towns ()
        {
            Box box = Box.createVerticalBox ();
            box.add (new JButton (new AbstractAction("Moscow")
            {
                @Override
                public void actionPerformed (ActionEvent e)
                {
                    switchTo ("Colors");
                }
            }));
            box.add (Box.createVerticalStrut (8));
            box.add (new JButton (new AbstractAction("London")
            {
                @Override
                public void actionPerformed (ActionEvent e)
                {
                    switchTo ("Fruits");
                }
            }));
            box.add (Box.createVerticalStrut (8));
            box.add (new JButton (new AbstractAction("Tokyo")
            {
                @Override
                public void actionPerformed (ActionEvent e)
                {
                    switchTo ("Seasons");
                }
            }));

            setLayout (new BorderLayout ());
            add (box, BorderLayout.CENTER);
        }
    }
}
于 2013-02-17T17:17:38.573 回答