JPanel not visible? Am I stupid!?
Hi, I got this simple code:
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);
}
}
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??
TY!
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.