Results 1 to 3 of 3
- 03-23-2012, 09:32 AM #1
Member
- Join Date
- Feb 2010
- Posts
- 12
- Rep Power
- 0
JPanel not visible? Am I stupid!?
Hi, I got this simple code:
Now why the hell can't i see the JPanel?! I have dont almost the exact same thing in another program, and it works perfectly =( I can see the drawn "roundrect", but there is no background. Also, why does setBackground, and setForeground never work on JFrame??Java Code:import java.awt.Color; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; public class paint extends JFrame{ JPanel panel = new JPanel(); public void paint(Graphics g){ g.drawRoundRect(20, 20, 50, 50, 10, 10); } public static void main (String[] args){ paint p = new paint(); } public paint(){ getContentPane().setLayout(null); setSize(300, 200); setBackground(Color.WHITE); setForeground(Color.BLACK); panel.setBackground(Color.RED); panel.setSize(300,200); panel.setVisible(true); add(panel); setVisible(true); } }
TY!
- 03-23-2012, 08:19 PM #2
Re: JPanel not visible? Am I stupid!?
try these bounds:
g.drawRoundRect(20, 50, 50, 50, 10, 10);
-
Re: JPanel not visible? Am I stupid!?
Please read the Swing tutorials first. You are doing so many things wrong I have to wonder if you haven't done this most basic step yet.
For example, you are- Drawing right on the JFrame itself instead of in a JPanel's paintComponent method
- Covering up you're what you're drawing by putting a component on top of it
- Using wrong Java naming conventions
- trying to set component's sizes directly rather than preferred size and calling pack() on the JFrame
- setting background and foreground colors of a JFrame (this does nothing at all useful)
- ...
Also please search this forum for sample code that shows drawing in Swing applications. There are many such examples around here, I know, because I've written a few.Last edited by Fubarable; 03-23-2012 at 10:28 PM.
Similar Threads
-
Stupid mistake, maybe?
By knox in forum New To JavaReplies: 1Last Post: 11-10-2011, 08:39 AM -
How to reverse something really stupid I did... HELP PLEASE
By efebatistaarda in forum NetBeansReplies: 0Last Post: 02-15-2011, 11:16 PM -
I need an idea with something really stupid
By blf_titi in forum Threads and SynchronizationReplies: 2Last Post: 10-27-2010, 03:24 AM -
Stupid error
By dewitrydan in forum Java AppletsReplies: 3Last Post: 08-09-2010, 01:29 PM -
Problems with setting the menubar and a JPanel visible
By leitbug06 in forum AWT / SwingReplies: 1Last Post: 03-06-2009, 11:46 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks