Java CardLayout显示问题

时间:2011-08-12 23:49:49

标签: java swing show cardlayout

好的我再次需要帮助。出于某种原因,当我点击一个按钮时按钮消失但不是面板

以下是代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MainMenu extends JPanel implements ActionListener{

ImageDirectory ID = new ImageDirectory();
PlayMenu PM = new PlayMenu();
JButton Play;
CardLayout CL;
Image BG;

    public MainMenu(){

    CL = new CardLayout(180,220);
    setLayout(CL);
    Play = new JButton("play");
    Play.addActionListener(this);
    ImageIcon BGSource = new ImageIcon(ID.Background);
    BG = BGSource.getImage();
    add(Play, "Play");
    add(PM, "PMenu");
    }

    public void actionPerformed(ActionEvent e){

        if(e.getSource() == Play){

            CL.show(this, "PMenu");
            }
        }

        public void paintComponent(Graphics g){

            g.drawImage(BG,0,0,500,500,this);
            }
 }

这是我点击按钮时试图显示的类:

import javax.swing.*;
import java.awt.*;

public class PlayMenu extends JPanel{

public PlayMenu() {}


}

1 个答案:

答案 0 :(得分:0)

它可能正常运作。您的新PlayMenu正在取代原来的MainMenu。尝试在PlayMenu构造函数中添加setBackground(Color.RED)以查看它是否确实替换了以前的MainMenu。