0

I have an issue with my code below, the buttons and the Panel aren't displayed until mouseover Could you please help me to identify the reason of this issue ?

Thank you in advance for your help.

Kind regards

    JButton atelier;JButton examen;
    private JPanel contentPane;
        public void principe() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(50, 50,1000,700);
            contentPane = new JPanel();
            setContentPane(contentPane);
            contentPane.setLayout(null);
            JPanel Evenement = new JPanel();
            Evenement.setBounds(3, 200, 251, 62);
            contentPane.add(Evenement);
            Evenement.setLayout(null);
        Evenement.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent arg0) {
                choix();}
            });
            
            
            JLabel lblNewLabel_5 = new JLabel("Activité");
            lblNewLabel_5.setBounds(128, 13, 111, 36);
            Evenement.add(lblNewLabel_5);
        }
            
    public void choix() {
        JPanel m= new JPanel();
        m.setBackground(new Color(70, 130, 180));
        m.setBounds(260,20, 689, 75);
        
        m.setLayout(null);  
         atelier = new JButton("Atelier");
         atelier.setBounds(486,15, 137, 40);
         atelier.setBackground(new Color(200, 180, 150));
        m.add(atelier);
        
        examen = new JButton("Examen");
        examen.setBounds(344,15, 137, 40);
        examen.setBackground(new Color(200, 180, 150));
        m.add(examen);
        contentPane.add(m);
    }
camickr
  • 321,443
  • 19
  • 166
  • 288
tara boli
  • 1
  • 2
  • 2
    Could you post your code? All I see are random letters. – EricMPastore Jun 24 '20 at 14:56
  • 1
    Buttons are added after the mouse click. So they might not visible until you refresh the UI. Call `revalidate();` and `repaint();` this will refresh newly added components. See: https://stackoverflow.com/questions/1097366/java-swing-revalidate-vs-repaint – Tanimak Jun 24 '20 at 16:20
  • 1
    1) Variable names should not start with an upper case character 2). Don't use a null layout and setBounds(). Swing was designed to be used with layout managers. 3) What if the user clicks the button twice? You will keep adding more components to the frame. If you use a layout manager you would notice this problem. – camickr Jun 24 '20 at 18:32

0 Answers0