Java CardLayout Show Problem
Ok me again i need help. For some reason when i click a button the button dissapears but not the panel
Here is the code:
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);
}
}
Here is the class that i tried to show when button is clicked:
import javax.swing.*;
import java.awt.*;
public class PlayMenu extends JPanel{
public PlayMenu() {}
}
It's probably working as it should. Your new PlayMenu is replacing the original MainMenu. Try adding setBackground(Color.RED) in the PlayMenu constructor to see if it does replace the previous MainMenu.
精彩评论