• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I make things in my JFrame?

 
Hans vogn
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

I'm very very new to java swing, and java at all. But I have now maneged to make a JFrame and now I'm trying to put some things in it, and I have been watching very many tutorials, but I can't make it work

I have only got this far:



And I would ask, how I can make something like a rectangle, or string?

Tanks in advance.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A frame is generally used to as a container for other GUI components. If you want to perform custom drawing, use a JPanel. The steps you'd need to do are:
  • write a class that extends JPanel
  • go wild with drawing in that classes's paintComponent method
  • add an instance of that class to the JFrame

  • This is covered in the Sun Java Tutorial in more detail including example code: http://java.sun.com/docs/books/tutorial/uiswing/painting/index.html
     
    Hans vogn
    Ranch Hand
    Posts: 46
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks!

     
    John M Morrison
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You can make your class executable by adding a main method as follows



    You should capitalize your class name (frame -> Frame); this is a universal convention of Java programmers. You can then see your empty frame displayed on the screen. As you add stuff to the content pane, you can run your program and watch it appear.

    JMM
     
    Campbell Ritchie
    Marshal
    Posts: 52516
    118
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    John M Morrison wrote:You should capitalize your class name (frame -> Frame)
    Correct . . .

    But there already is a Frame class, so a different name would be more appropriate.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!