2013-05-21 39 views
1


我不喜欢什么我下面的代码是:需要为每一个JButton用于在GUI页面之间切换的建议设计模式是什么?

  1. 干将每一页
  2. actionPerformed方法很快就会变得臃肿if-else语句

那么,有没有更好的方法来控制一个类的所有GUI操作?

如果我定义每个相应页面(JPanel)内的actionPerformed方法,每一页将需要访问的页面(一个或多个)的情况下切换到,和我使用每个页面的Singleton图案试图避免。 ..


下面是代码:

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

/** 
* 
* @author Ian A. Campbell 
* 
*/ 
public class Controller implements ActionListener { 

    /** 
    * instance variables: 
    */ 
    private Frame frame; 
    private OptionPage firstPage; 
    private FirstOptionPage firstOption; 
    private SecondOptionPage secondOption; 

    /** 
    * 
    */ 
    public Controller() { 

     // instantiating the frame here: 
     this.frame = new Frame(); 

     /* 
     * instantiating all pages here: 
     * 
     * NOTE: passing "this" because this class 
     * handles the events from these pages 
     */ 
     this.firstPage = new OptionPage(this); 
     this.firstOption = new FirstOptionPage(this); 
     this.secondOption = new SecondOptionPage(this); 
    } 

    /** 
    * 
    */ 
    public void start() { 
     this.frame.add(this.firstPage); // adding the first page 

     // NOTE: these lines prevent blank loading and flickering pages! 
     this.frame.validate(); 
     this.frame.repaint(); 
     this.frame.setVisible(true); 
    } 

    /** 
    * 
    * @return the JFrame instantiated from the class Frame 
    */ 
    public Frame getFrame() { 
     return this.frame; 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 

     // the "first option" button from the OptionPage: 
     if (e.getSource() == this.firstPage.getFirstButton()) { 
      this.frame.getContentPane().removeAll(); 
      this.frame.getContentPane().add(this.firstOption); 

     // the "second option" button from the OptionPage: 
     } else if (e.getSource() == this.firstPage.getSecondButton()) { 
      this.frame.getContentPane().removeAll(); 
      this.frame.getContentPane().add(this.secondOption); 
     } 

     // NOTE: these lines prevent blank loading and flickering pages! 
     this.frame.validate(); 
     this.frame.repaint(); 
     this.frame.setVisible(true); 
    } 
} // end of Controller 
+1

你正在尝试构建的东西叫做向导。也许这会有所帮助。 http://www.oracle.com/technetwork/articles/javase/wizard-136789.html –

回答

2

使用Card LayoutCard Layout Actions增加了一些额外的功能,您可能会发现有帮助。

+0

谢谢@camickr。 'CardLayout'看起来很混乱,可以区分页面导航的层次结构......例如 - 如果页面上有两个按钮转到不同的页面,并且每个页面的按钮都转到不同的页面,那么使用' CardLayout'方法,如'next()'和'previous()'?页面导航的每个“分支”是否都有不同的'CardLayout'? –

+1

Next/Previous按钮用于简单导航,不应在您的情况下使用。但是,CardLayout仍然是管理您的需求的最佳方式。您需要为每个面板添加单独的按钮,以指定单击该按钮时应显示哪个面板。 CardLayout将管理当前面板与指定面板的交换。 – camickr

+0

谢谢@camickr,'RXCardLayout'很有趣...我想从头开始实现这个,但是实践中。所以,我现在在我的Controller类中有一个'CardLayout'。在使用'MVC'设计模式的方法时,'CardLayout'应该放置在模型,视图还是控制器中?如果我把它放在控制器中,似乎我必须将整个视图包含到控制器中...... –

1

您可以使用卡片布局,或者您可以创意并删除元素。例如:

panel.remove((JButton)myButton1)); // Remove all of the elements... 
panel.add((JButton)myButton2)); // Add the new elements 

当然,我不会处理所有的GUI内置的Java,布局设计是可怕的。我宁愿使用像“新外观和感觉”的东西 - http://www.javootoo.com/

+0

感谢@Dillon的链接,这很有趣。 –