我用Google搜索了很多东西,但没有找到解决方案。我认为应该有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; }
这是JPanel另一个步骤0的方法:
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"); } });
完全没有任何回应,我尝试过重新验证,重新粉刷和其他工作人员…不起作用…任何人在这里都可以得到意见!
这就是将正确的方法和常量String暴露给外界,以允许类交换视图本身。例如,给您的第一堂课一个私有的CardLayout字段,称为cardlayout,一个私有的JPanel字段称为cards(持卡人JPanel),以及一些用于将您的Card JPanels添加到cards容器的公共String常量。还给它一个公共方法,比如所谓的public void swapView(String key),它允许外部类交换卡…类似这样:
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来交换视图,并传入适当的键String(例如,在本例中为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"); } });
在这一行上,您正在创建一个新的Registration对象,该对象可能与在GUI上可视化的对象完全不相关,因此,对该新对象的调用方法对当前查看的gui绝对没有影响。相反,您可能需要获取对已查看的Registration对象的引用,这可能是通过给此类提供一个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(); } }); } }