Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Swing / AWT / SWT
draw panel and jbutton not showing
Carolin Sha
Ranch Hand
Posts: 83
1
posted 8 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
hi,
Please see code below, Not sure why the draw panel and jbutton are not showing. any help would be appreciated.
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; import java.awt.BorderLayout; import javax.swing.JButton; public class MovingCircleGUI2{ JFrame frame; public int x,y; public int diameter=200; CircleDrawPanel drawPanel; Color color = Color.orange; public static void main (String[] args){ MovingCircleGUI gui = new MovingCircleGUI(); gui.go(); } public void go(){ frame = new JFrame(); JButton button = new JButton("click me"); JButton btnNewButton = new JButton("New button"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); drawPanel = new CircleDrawPanel(); drawPanel.setPreferredSize(new Dimension(100,100)); add(drawPanel, BorderLayout.SOUTH); frame.getContentPane().add(BorderLayout.SOUTH, drawPanel); frame.setSize(500,500); frame.setVisible(true); frame.getContentPane().add(button); frame.getContentPane().add(BorderLayout.SOUTH, button); //frame.getContentPane().setLayout(new BorderLayout(0, 0)); //frame.getContentPane().add(btnNewButton, BorderLayout.SOUTH); } class CircleDrawPanel extends JPanel{ public void paintComponent (Graphics g){ super.paintComponent(g); Graphics2D g2=(Graphics2D)g; g2.setColor(color); g2.fillOval(x,y,diameter,diameter); } } }
Campbell Ritchie
Marshal
Posts: 79177
377
posted 8 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Moving to our GUIs forum.
You called setVisible before you added the panel and button.
You should
call setVisible last (or use revalidate).
Carolin Sha
Ranch Hand
Posts: 83
1
posted 8 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
hi,
I moved setVisible however I am still not seeing the draw panel and jbutton.
Carolin Sha
Ranch Hand
Posts: 83
1
posted 8 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
hi,
Thanks, I finally got it to work.
Don't get me started about those stupid
light bulbs
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
repaint paints the wrong component
need help with my code
GUI Example (HFJ)
Adding drawpanels to a frame
why does this code fail?
More...