java单帧应用程序

时间:2013-02-17 17:11:41

标签: java swing frame

我需要使用单个窗口创建一个没有任何弹出窗口的应用程序。一切都使用相同的窗口,只改变内容。我将使用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);
}

}

而不是容器堆栈会有控制器堆栈,它会加载视图和模型。

1 个答案:

答案 0 :(得分: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);
        }
    }
}