簡體   English   中英

如何從另一個面板更改卡片布局面板?

[英]How to change card layout panels from another panel?

我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;
}


這是另一個步驟 0 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");
            }
        });

根本沒有反應,我嘗試重新驗證,重新粉刷和其他工作人員......不起作用......任何人都在這里得到任何解決方案!

這一切都是為了向外界公開正確的方法和常量字符串,以允許 class 自身交換視圖。 例如,為您的第一個 class 提供一個稱為 cardlayout 的私有 CardLayout 字段和一個稱為卡(持卡人 JPanel)的私有 JPanel 字段,以及一些用於將您的卡 JPanel 添加到卡容器的公共字符串常量。 還給它一個公共方法,比如稱為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);
   }

}

然后外部 class 可以通過調用此 class 的可視化實例上的 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");
        }
    });

在該行上,您正在創建一個新的注冊 object,它可能與在 GUI 上可視化的那個完全無關,因此在這個新的 object 上調用方法對當前查看的 gui 絕對沒有影響。 相反,您需要獲取對查看的注冊 object 的引用,也許通過給這個 class 一個 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();
         }
      });
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM