好吧,我再次需要帮助。出于某种原因,当我单击按钮时,按钮消失但面板没有消失
这是代码:
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() {}
}