2011-05-30 16 views
11

मैं बहुत कुछ के लिए googled, और कोई समाधान नहीं मिला। मुझे लगता है कि मुझे बाहर मदद करने के लिए जावा स्वामी होगा ...किसी अन्य पैनल से कार्ड लेआउट पैनल कैसे बदलें?

यह मेरा इनिशियलाइज़ विधि है:


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

उचित पंजीकरण उदाहरण पर विधियों को कॉल करने का विषय नहीं है। उपर्युक्त कोड में आप एक नया पंजीकरण उदाहरण बना रहे हैं जो जीयूआई में प्रदर्शित होने वाले व्यक्ति से पूरी तरह से असंबंधित है। इसके बजाय आपको दृश्यमान पंजीकरण ऑब्जेक्ट का संदर्भ प्राप्त करने की आवश्यकता है। कृपया नीचे मेरा जवाब देखें ... –

उत्तर

19

कक्षा के दृश्यों को स्वैप करने की अनुमति देने के लिए बाहरी विधियों और निरंतर स्ट्रिंग्स को बाहरी दुनिया को उजागर करने के बारे में यह सब कुछ है। उदाहरण के लिए, अपनी पहली कक्षा को कार्डलेआउट नामक एक निजी कार्डलाउट फ़ील्ड दें और कार्ड (कार्ड धारक जेपीनल) नामक एक निजी जेपीनल फ़ील्ड, और कुछ सार्वजनिक स्ट्रिंग स्थिरांक जिन्हें कार्ड कार्ड कंटेनर में अपना कार्ड जेपीनल जोड़ने के लिए उपयोग किया जाता है। इसके अलावा यह एक सार्वजनिक विधि देते हैं, 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 उपयोगकर्ता जानकारी जेपीनल दिखाने के लिए।

अब आप कोड के इस बिट के साथ एक समस्या है जहाँ मैं टिप्पणी से संकेत मिलता है:

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"); 
     } 
    }); 

कि लाइन आप एक नया पंजीकरण वस्तु बना रहे हैं पर, शायद एक है कि पूरी तरह से एक यह है कि से संबंधित नहीं है जीयूआई पर कल्पना की गई, और इस नए ऑब्जेक्ट पर कॉलिंग विधियों का वर्तमान में देखे गए गुई पर बिल्कुल असर नहीं पड़ेगा। आप देखी पंजीकरण ऑब्जेक्ट के संदर्भ इसलिए की तरह, शायद इस वर्ग के एक 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; 
    } 
} 

और मेनक्लास जो यह सभी

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

यह बहुत उपयोगी है! आपको बहुत - बहुत धन्यवाद ! इस समस्या ने मुझे पहले से ही कई दिनों के लिए संदेह किया था ... हाहा ... –

+1

@aladdin: कुंजी मेरे बेटे सही है * संदर्भ *। एक बार जब आप इसे समझ गए, तो बाकी आसान है। खुशी हुई यह मदद की। –

+0

मेरी इच्छा है कि कोई इस पोस्ट को और स्कोर दे सके – Zavior

संबंधित मुद्दे