更改卡片布局中的框架和焦点

时间:2014-02-17 03:23:47

标签: java swing jframe jpanel cardlayout

我已接近第一场比赛的最后开发阶段,但遇到了问题。我构建了4个不同的关卡,允许用户从卡片布局系统的中央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);
          }

      }

}

2 个答案:

答案 0 :(得分:1)

首先在开始时将所有“游戏”面板添加到bodyPanel,然后只需使用clayout.show在它们之间切换。如果需要,请在切换到“游戏”面板之前提供reset方法来重置“游戏”面板。

使用将传递给每个“游戏”面板的回调接口。当他们完成他们的级别时,它将调用此接口,告诉实现该级别已完成,然后实现将决定如何切换下一个面板。

答案 1 :(得分:0)

感谢所有花时间看这个的人。我最后只是创建一个静态函数来交换面板,以便可以从任何类调用它。

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