1
Hello, I’m having problems using Cardlayout to display the two-class graphics. I intended to show each graph at once and display the other at the push of a button, however when I press the button the first graph remains drawn and the second appears on writing the first, causing the two charts to appear at the same time.
Main class
public class MainFrame extends JFrame{
JPanel mainCard;
JButton jb1, jb2;
CardLayout cards;
NRZL card1 = new NRZL();
NRZI card2 = new NRZI();
public MainFrame(){
setSize(900,900);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setTitle("Tela de escolha");
jb1 = new JButton("Vá para NRZI");
jb2 = new JButton("Vá para NRZL");
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cards = (CardLayout) mainCard.getLayout();
cards.show(mainCard, "card 2");
}
});
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cards = (CardLayout) mainCard.getLayout();
cards.show(mainCard, "card 1");
}
});
card1.add(jb1);
card2.add(jb2);
mainCard = new JPanel(new CardLayout());
mainCard.add(card1, "card 1");
mainCard.add(card2, "card 2");
getContentPane().add(mainCard);
setVisible(true);
}
public static void main(String[] args){
MainFrame mainFrame = new MainFrame();
}
}
Graphic class 1
public class NRZL extends JPanel{
@Override
public void paintComponent(Graphics g){
super.repaint();
g.drawOval(200, 200, 200, 200);
}
}
Graphic class 2
public class NRZI extends JPanel{
@Override
public void paintComponent(Graphics g){
super.repaint();
g.drawRect(300, 200, 200, 200);
}
}
Thanks in advance for any help.
Thank you very much, that was the missing detail.
– Jean Willian S. J.