2011-05-30 196 views
11

我谷歌搜索了很多,並沒有找到解決辦法。我認爲應該是java高手幫我出...如何從另一個面板更換卡布局面板?

這是我的初始化方法:


private void initialize() { 
    this.setSize(750, 480); 
    this.setContentPane(getJContentPane()); 
    this.setTitle("Registration"); 
    JPanel topPane = new TopPane(); 
    this.getContentPane().add(topPane,BorderLayout.PAGE_START); 
    cards=new JPanel(new CardLayout()); 
    cards.add(step0(),"step0"); 
    cards.add(step1(),"step1"); 
    cards.add(step2(),"step2"); 
    this.getContentPane().add(cards,BorderLayout.CENTER); 
} 

public JPanel step2(){ 
    EnumMap<DPFPFingerIndex,DPFPTemplate> template = new EnumMap<DPFPFingerIndex, DPFPTemplate>(DPFPFingerIndex.class); 
    JPanel enrol = new Enrollment(template,2); 
    return enrol; 
} 

public JPanel step0(){ 
    JPanel userAgree = new UserAgreement(); 
    return userAgree; 
} 

public JPanel step1(){ 
    JPanel userInfo = new UserInformation(); 
    return userInfo; 
} 

public JPanel getCards(){ 
    return cards; 
} 


這是另一個step0的JPanel中的方法:

jButtonAgree.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent e) { 
       Registration reg = new Registration(); 
       LayoutManager cards = reg.getCards().getLayout(); 
       ((CardLayout) cards).show(reg.getCards(),"step1"); 
      } 
     }); 

根本沒有反應,我試過重新驗證,重繪和其他工作人員...不工作...任何人都有任何的飲料在這裏!

+1

這是一個不正確的註冊實例調用方法的問題。在上面的代碼中,您正在創建一個與GUI中顯示的完全無關的新註冊實例。相反,您需要獲取對可見註冊對象的引用。請參閱下面的答案... – 2011-05-30 12:18:43

回答

19

這一切都暴露了正確的方法和常量字符串到外面的世界,讓全班同學交換意見本身。例如,給你的第一個班級一個名爲cardlayout的私人CardLayout字段和一個名爲卡片(卡片持有者JPanel)的私人JPanel字段,以及一些用於將卡片JPanels添加到卡片容器的公共字符串常量。也給它的公共方法,說叫public void swapView(String key),允許類外換牌......像這樣:

// code corrected 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class Registration extends JPanel { 
    // use these same constants as button texts later 
    private static final Dimension PREF_SIZE = new Dimension(450, 300); 
    public static final String USER_AGREEMENT = "User Agreement"; 
    public static final String USER_INFO = "User Information"; 
    public static final String ENROLLMENT = "Enrollment"; 
    // we'll extract them from this array 
    public static final String[] KEY_TEXTS = {USER_AGREEMENT, USER_INFO, ENROLLMENT}; 
    private CardLayout cardlayout = new CardLayout(); 
    private JPanel cards = new JPanel(cardlayout); 

    public Registration() { 
     cards.add(createUserAgreePanel(), USER_AGREEMENT); 
     cards.add(createUserInfoPanel(), USER_INFO); 
     cards.add(createEnrollmentPanel(), ENROLLMENT); 
     setLayout(new BorderLayout()); 
     add(cards, BorderLayout.CENTER); 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     return PREF_SIZE; 
    } 

    private JPanel createEnrollmentPanel() { 
     JPanel enrol = new JPanel(); 
     enrol.add(new JLabel("Enrollment")); 
     return enrol; 
    } 

    private JPanel createUserAgreePanel() { 
     JPanel userAgree = new JPanel(); 
     userAgree.add(new JLabel("User Agreement")); 
     return userAgree; 
    } 

    private JPanel createUserInfoPanel() { 
     JPanel userInfo = new JPanel(); 
     userInfo.add(new JLabel("User Information")); 
     return userInfo; 
    } 

    public void swapView(String key) { 
     cardlayout.show(cards, key); 
    } 

} 

然後外部類可以通過調用這個類的可視化實例的swapView簡單地交換意見,傳入適當的鍵字符串(例如在此情況下爲CardTest.USER_INFO)以顯示用戶信息JPanel。

現在你有這段代碼的問題,我表示按註釋:

jButtonAgree.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent e) { 
      Registration reg = new Registration(); // **** HERE ***** 
      LayoutManager cards = reg.getCards().getLayout(); 
      ((CardLayout) cards).show(reg.getCards(),"step1"); 
     } 
    }); 

您要創建一個新的登記對象,行,可能一個是完全無關的一個是在GUI上可視化,所以調用這個新對象的方法對當前查看的gui沒有任何影響。你需要,而不是去觀看的登記對象的引用,也許通過放棄此一等級getRegistration方法,然後調用它的方法,就像這樣:

class OutsideClass { 
    private Registration registration; 
    private JButton jButtonAgree = new JButton("Agree"); 

    public OutsideClass() { 
     jButtonAgree.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      // make sure registration reference has been obtained first! 
      if (registration != null) { 
       registration.swapView(Registration.USER_AGREEMENT); 
      } 
     } 
     }); 
    } 

    // here I allow the calling class to pass a reference to the visualized 
    // Registration instance. 
    public void setRegistration(Registration registration) { 
     this.registration = registration; 
    } 
} 

例如:

@SuppressWarnings("serial") 
class ButtonPanel extends JPanel { 
    private Registration registration; 

    public ButtonPanel() { 
     setLayout(new GridLayout(1, 0, 10, 0)); 
     // go through String array making buttons 
     for (final String keyText : Registration.KEY_TEXTS) { 
     JButton btn = new JButton(keyText); 
     btn.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       if (registration != null) { 
        registration.swapView(keyText); 
       } 
      } 
     }); 
     add(btn); 
     } 
    } 

    public void setRegistration(Registration registration) { 
     this.registration = registration; 
    } 
} 

和MainClass驅動這一切

class MainClass extends JPanel { 
    public MainClass() { 
     Registration registration = new Registration(); 
     ButtonPanel buttonPanel = new ButtonPanel(); 
     buttonPanel.setRegistration(registration); 

     buttonPanel.setBorder(BorderFactory.createTitledBorder("Button Panel")); 
     registration.setBorder(BorderFactory.createTitledBorder("Registration Panel")); 

     setLayout(new BorderLayout()); 
     add(registration, BorderLayout.CENTER); 
     add(buttonPanel, BorderLayout.SOUTH); 
    } 

    private static void createAndShowUI() { 
     JFrame frame = new JFrame("Registration"); 
     frame.getContentPane().add(new MainClass()); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowUI(); 
     } 
     }); 
    } 
} 
+0

這非常有幫助!非常感謝 !這個問題已經懷疑我已經很多天了......哈哈... – 2011-05-30 12:52:10

+1

@aladdin:我的兒子的關鍵是正確的*參考文獻*。一旦你明白了,剩下的就很容易。很高興幫助。 – 2011-05-30 13:01:40

+0

我希望可以給這個帖子更多的分數 – Zavior 2012-03-28 22:38:19