ทำการเปลี่ยนแปลง JPanel หลังจาก ActionListener

ฉันต้องทำเกมสำหรับโครงการโรงเรียนในระบบ CS ฉันต้องการสร้างมันเพื่อให้บุคคลสามารถป้อนชื่อของตนได้ และแผงต่อๆ ไปทั้งหมดจะมีชื่อของพวกเขาอยู่บนนั้น ฉันกำลังใช้เค้าโครงการ์ด ซึ่งใช้งานได้เป็นส่วนใหญ่ เมื่อฉันรันเกมและป้อนชื่อ มันจะทำงานบนการ์ดใบนั้นโดยเฉพาะ อย่างไรก็ตาม เมื่อฉันไปที่การ์ดใบถัดไป ซึ่งชื่อสตริงควรปรากฏ มันจะปรากฏเป็นค่าว่าง ความสงสัยของฉันคือโปรแกรมสร้างการ์ดทั้งหมดก่อนที่จะได้รับโอกาสในการยอมรับการป้อนชื่อและดังนั้นจึงล้มเหลว ครูของฉันก็ไม่รู้เหมือนกันว่าต้องทำอย่างไร

นี่คือส่วนของรหัส:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class NovelCards extends JFrame {
   private String name;
   private CardLayout cards;
   private JTextField txt = new JTextField(10);
   private JLabel label = new JLabel();
   public NovelCards() {
      super("Novel");
      masterPanel = new JPanel();
      cards = new CardLayout();
      txt.addActionListener( new ActionListener(){
          public void actionPerformed(ActionEvent e){
            name = txt.getText();
            label.setText(name);
          }
      });      
      button2 = new JButton("Next");
      button2.addActionListener(new NextListener());
      JPanel card2 = new JPanel();
      card2.add(button2);
      card2.add(txt);
      card2.add(label);
      card2.add(new JLabel(" Enter your name "));

      JButton button3 = new JButton("Next");
      button3.addActionListener(new NextListener());
      JPanel card3 = new JPanel();
      card3.add(new JLabel("Hello there, " + name + "!"));
      card3.add(button3);

      masterPanel.setLayout(cards); 
      masterPanel.add("2",card2);
      masterPanel.add("3",card3);
      this.setContentPane(masterPanel);
      cards.show(masterPanel, "1");
 }

 public class NextListener implements ActionListener{
  public void actionPerformed(ActionEvent event){
     cards.next(masterPanel);
 }

นี่คือรูปถ่ายบางส่วนของโปรแกรมขณะทำงาน

นี่คือหน้าจอป้อนชื่อหลังจากเลือกชื่อแล้วกด Enter:

http://i.stack.imgur.com/AC446.png

และนี่คือการ์ดใบต่อไป:

http://i.stack.imgur.com/Z1Jin.png

ขอบคุณล่วงหน้าสำหรับความช่วยเหลือ! (ฉันจะโพสต์ภาพโดยตรง แต่ฉันมีชื่อเสียงไม่เพียงพอ)


person BananaCreamDream    schedule 03.06.2015    source แหล่งที่มา


คำตอบ (1)


คำถามที่คุณต้องถามตัวเองคือ ไพ่/แผงอื่นๆ ทั้งหมดจะรู้ได้อย่างไรว่าชื่อนั้นมีค่าเท่าไหร่?

วิธีแก้ไขง่ายๆ คือส่งชื่อไปยังพาเนลทั้งหมด สิ่งนี้จะกลายเป็นปัญหาเมื่อคุณต้องการเพิ่มการ์ด/พาเนลใหม่ เนื่องจากคุณต้องอัปเดตโค้ดอย่างต่อเนื่อง แน่นอนว่าคุณสามารถใช้ List หรืออาร์เรย์และให้การ์ด/พาเนลทั้งหมดใช้อินเทอร์เฟซทั่วไป...

หรือคุณสามารถใช้ "โมเดล" บางอย่าง ซึ่งการ์ด/แผงทั้งหมดใช้การอ้างอิงร่วมกัน สิ่งนี้จะช่วยให้คุณแบ่งปันข้อมูลระหว่างกันได้ง่ายขึ้นโดยไม่ต้องยุ่งวุ่นวายมากนัก

ดังนั้นหากคุณสามารถชี้แนะฉันไปยังสิ่งที่ฉันต้องเรียนรู้เพื่อส่งชื่อไปยังแผงอื่นได้?

สร้างคลาส โดยควรเริ่มต้นด้วยอินเทอร์เฟซซึ่งทำหน้าที่เป็นที่เก็บข้อมูล ซึ่งจะกลายเป็นโมเดลของคุณ...

public interface NovelCardsModel {
    public String getName();
    public void setName(String name);

    public void addPropertyChangeListener(PropertyChangeListener listener);
    public void removePropertyChangeListener(PropertyChangeListener listener);

    // Add accessors to any other data you might think
    // you need to share
}

ตอนนี้ สร้างการนำโมเดลไปใช้

public class DefaultNovelCardsModel implements NovelCardsModel {
    private PropertyChangeSupport propertyChangeSupport;
    private String name;

    public DefaultNovelCardsModel() {
        propertyChangeSupport = new PropertyChangeSupport(this);
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setName(String value) {
        if (value == null || !value.equals(name)) {
            String old = name;
            name = value;
            propertyChangeSupport.firePropertyChange("name", old, name);
        }
    }

    @Override
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.addPropertyChangeListener(listener);
    }

    @Override
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(listener);
    }


}

สำหรับแต่ละพาเนลที่ต้องการทราบชื่อ คุณจะต้องส่งอินสแตนซ์เดียวกันของโมเดลไปให้...

DefaultNovelCardsModel model = new DefaultNovelCardsModel();
masterPanel = new MasterPane(model);
// All the other cards.

แต่ละพาเนลที่ต้องการทราบเมื่อเปลี่ยนชื่อ พวกเขาจำเป็นต้องลงทะเบียน PropertyChangeListener และติดตามการเปลี่ยนแปลงคุณสมบัติ name

public class MasterPanel extends JPanel {

    private NovelCardsModel model;

    public MasterPanel(NovelCardsModel model) {
        this.model = model;
        this.model.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if ("name".equals(evt.getPropertyName())) {
                    // Name has changed
                }
            }
        });
    }

}
person MadProgrammer    schedule 03.06.2015
comment
ขอบคุณสำหรับความเข้าใจ! ฉันเป็นมือใหม่เรื่องโค้ด ดังนั้นหากคุณช่วยแนะนำฉันเกี่ยวกับสิ่งที่ฉันจำเป็นต้องเรียนรู้เพื่อส่งชื่อไปยังแผงอื่นได้ไหม - person BananaCreamDream; 04.06.2015