0

好吧,我再次需要帮助。出于某种原因,当我单击按钮时,按钮消失但面板没有消失

这是代码:

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() {}


}
4

1 回答 1

0

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

于 2011-08-13T00:22:58.873 回答