Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Here’s my code below: 

public class humev extends JFrame implements ActionListener{

    //Dichiarazione variabili e costanti

    private static final int larghezza = 1300;

    private static final int altezza = 1000;

    private static final String nome = "Human Evolution";

    private JLabel lab;

    private JButton gioca;

    private JPanel pang;

        public humev(){

            try{

                pang = new JPanel();

                gioca = new JButton("Gioca!");

                gioca.addActionListener(this);

                lab = new JLabel();

                gioca.add(gioca);

                lab.add(lab);

                pang.setLayout(null);

                }

            catch(Exception e1){

                System.err.println(e1);

                System.err.println("Impossibile caricare il frame di gioco!");

                }

        }

    public static void main(String[] args) {

        //Finestra

        try{

            humev h = new humev();

            JFrame finestra = new JFrame(nome);

            Dimension dim_finestra = new Dimension(larghezza, altezza);

            finestra.setPreferredSize(dim_finestra);

            finestra.setMaximumSize(dim_finestra);

            finestra.setResizable(false);

            finestra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            finestra.pack();

            finestra.setVisible(true);

        }

        catch(Exception e2){

            System.err.println(e2);

            System.err.println("Impossibile caricare la finestra. Frame non caricato");

        }

    }

    @Override

    public void actionPerformed(ActionEvent e) {

        if(e.getSource() == gioca){

            lab.setText("Gioco avviato con successo!");

        }

    }

}

But I get this error “java.lang.IllegalArgumentException: adding container's parent to itself.” Can anyone tell me where I’m wrong? 

1 Answer

0 votes
by (19.7k points)

Try to remove these lines of code below from your code :


gioca.add(gioca); //don’t add a button on button

pang.setLayout(null); // layout cannot be null

 If you want to learn more about Javathen go through this Java tutorial by Intellipaat for more insights. 

Related questions

0 votes
1 answer
asked Mar 13, 2021 in Java by Jake (7k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...