2013-05-21 105 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的鏈接,這很有趣。 –