• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java AWT question

 
Adam Chalkley
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I'm not new to GUI but I never really got into it anyway I was just messing around with AWT I've never really used it much but I've got a few questions


How come I cannot use the add method to add f to b b.add(f) but b to f will work f.add(b) I know it's logical but how come it does not work when adding f to b it will not work

second question is how come I get a Java.illegal arquement exception adding a window to a container when I try too add a frame to a panel one.add(f);

here's the short code





BQ: could I still use the methods from JFrame if I don't extend JFrame by just importing the package and if so why do we need to extend the JFrame class and why not JButton also etc.

Thanks guys =)
 
Liutauras Vilda
Marshal
Posts: 3222
127
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:How come I cannot use the add method to add f to b b.add(f) but b to f will work f.add(b) I know it's logical but how come it does not work when adding f to b it will not work

If you have TV and TV packaging. It is quite logical, that you can put TV into his packaging, but not other way round.

Adam Chalkley wrote:second question is how come I get a Java.illegal arquement exception adding a window to a container when I try too add a frame to a panel one.add(f);

Exception message is quite descriptive. Illegal argument.. Panel class extends Container class, so "add" method is inherited from Container and Component classes. There are about 5 overloaded "add" methods int Container class, and one in Component class. What kind of arguments each of these overloaded method accepts you should be able to find in Java API here (<- link).

Don't use import on demand, instead, use single type imports (line 1). About different import types you could find detailed information in JLS (<- link). It could be difficult to read at some point, but it is very detailed. That section about import statements is quite clear I think.

By the way, class names suppose to start with an upper case.
 
Randall Twede
Ranch Hand
Posts: 4520
6
Java Python Scala
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
indeed class names should start with a capital letter.
I don't extend JFrame anymore. use "has a" instead of "is a"
don't extend anything if you don't have to.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!