簡體   English   中英

更改卡片布局和重點

[英]Change Frame and Focus in Card Layout

我已經接近第一款游戲的最終開發階段,但是遇到了問題。 我已經構造了4個不同的級別,並允許用戶從Card Layout系統的中央JPanel中選擇他們想要播放的級別。 我的問題是,完成某個級別后,由於我不知道如何訪問充當其他面板驅動程序的原始JPanel,因此無法切換顯示的JPanel來啟動下一個級別。 任何幫助表示贊賞,我的代碼發布如下。

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

   public class MainFrame extends JFrame implements ActionListener
      {
         JPanel bodyPanel;
         JPanel panel1,panel2,panel3,panel4,panel5;
         JButton button1,button2,button3,button4,button5;
         JLabel title;
         Container con;
         CardLayout clayout;


      public MainFrame() 
   {
    clayout=new CardLayout();
    bodyPanel=new JPanel(clayout);

    button1=new JButton("Level 1");
    button3= new JButton("Level 2");
    button4 = new JButton("Level 3");
    button2=new JButton("Exit");
    button5 = new JButton("Level 4");


    button1.addActionListener(this);
    button2.addActionListener(this);
    button3.addActionListener(this);
    button4.addActionListener(this);
    button5.addActionListener(this);


    panel1=new JPanel();
        title = new JLabel("Space Adventure");
        panel1.add(title);
        title.setFont(new Font("Serif", Font.PLAIN, 110));
        panel1.add(button1);
        panel1.add(button3);
        panel1.add(button4);
        panel1.add(button5);
        panel1.add(button2);



    panel1.setBackground(Color.pink);
    panel2=new PrizePanel();
    panel2.setBackground(Color.gray);



    bodyPanel.add(panel1,"one");    
    bodyPanel.add(panel2,"two");    


    setLayout(new BorderLayout());
    setSize(800,400);
    add(bodyPanel,BorderLayout.CENTER);
    bodyPanel.setBounds(0,100, 600, 500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);

}

      public static void main(String args[])
      {
          new MainFrame();
      }

      public void actionPerformed(ActionEvent e) {

          if(e.getSource()==button1)
          {
           clayout.show(bodyPanel, "two");
              panel2.requestFocusInWindow();
           }else if(e.getSource()==button3)
          {
          panel3=new PrizePanel2();
          panel3.setBackground(Color.gray);
          bodyPanel.add(panel3,"three");
          clayout.show(bodyPanel, "three");
          panel3.requestFocusInWindow();
           }
           else if(e.getSource()==button4)
          {
          panel4=new PrizePanel3();
          panel4.setBackground(Color.gray);
          bodyPanel.add(panel4,"four");
          clayout.show(bodyPanel, "four");
          panel4.requestFocusInWindow();
           }else if(e.getSource()==button5)
          {
          panel5=new PrizePanel4();
          panel5.setBackground(Color.gray);
          bodyPanel.add(panel5,"five");
          clayout.show(bodyPanel, "five");
          panel5.requestFocusInWindow();
           }


          else if(e.getSource()==button2)
          {
             System.exit(0);
          }

      }

}

首先在開始時將所有“游戲”面板添加到bodyPanel ,然后簡單地使用clayout.show在它們之間進行切換。 如果需要,請提供一種reset方法來重置“游戲”面板,然后再切換到該面板。

使用將傳遞給每個“游戲”面板的回調接口。 當他們完成其級別時,它將調用此接口,告知實現該級別已完成,然后實現將決定如何切換下一個面板。

感謝所有花時間研究此問題的人。 我最終只是制作了一個靜態函數來交換面板,以便可以從任何類中調用它。

public static void swap(int x){
  switch(x){
     case 1:
       clayout.show(bodyPanel, "two");
          panel2.requestFocusInWindow();
          break;
       case 2:
      clayout.show(bodyPanel, "three");
      panel3.requestFocusInWindow();
       break;
       case 3:
       panel3 = null;
      clayout.show(bodyPanel, "four");
      panel4.requestFocusInWindow();
      break;
       case 4:
       panel4=null;
      clayout.show(bodyPanel, "five");
      panel5.requestFocusInWindow();
       break;
     }
  }

暫無
暫無

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

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