Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Swing probelm

 
Namit Puri
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.awt.*;
import javax.swing.*;

class MyDrawPanel extends JPanel
{
public void paintComponent(Graphics g)
{
g.setColor(Color.orange);
g.fillRect(20,50,100,100);
}
}

The above code compiles successfully. But how to run it, i meant where to specify the main method, especially as we can't call the paintComponenet() method ourselves.

Thanks!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

You need a "top-level component" like a JFrame to display a gui. You would then add an instance of this component to that JFrame, and display it. It doesn't matter where the main() is -- putting it into this class is perfectly fine. So main() would look something like

 
Namit Puri
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic