Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend JFrame and JPanel in a main class?

So I want my main class to extend both JFrame and JPanel. How would I do this?

For example:

public class mainClass extends JFrame {
//code here
}

How would I also extend JPanel for that same class?

Thanks in advance!

like image 923
knorberg Avatar asked Jan 28 '26 12:01

knorberg


2 Answers

How to extend JFrame and JPanel in a main class?

Let me answer you by asking a question:

Can a son have two fathers in a the same time? NO, because we don't know who is the real father, but after DNA testing you know who is the real one.

In java, there is no DNA testing, sorry for that, therefore, multiple inheritance not allowed.

In your case, if the class extended from both JFrame and JPanel, what will be the behavior of this class, panel or frame?

like image 131
Azad Avatar answered Jan 31 '26 01:01

Azad


Java does not support multiple inheritance. Possible solutions include coding an inner class in your main class that extends JPanel and then another inner class that extends JFrame. You can also just code two separate (not-inner) classes that extend the respective classes as that is the common approach I would use.

like image 34
Kon Avatar answered Jan 31 '26 02:01

Kon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!