Results 1 to 5 of 5
- 07-30-2010, 07:49 AM #1
Member
- Join Date
- Feb 2010
- Posts
- 80
- Rep Power
- 0
how to add 2 panels in a panel inside a frame
i have to make a jframe which contains a jpanel "try" which would contain some components, and 2 jpanels, "green" and "blue". here is my code
the problem is, i can't see the 2 jpanels!!huhuJava Code:import java.awt.*; import javax.swing.*; public class Try extends JPanel { GreenPanel green = new GreenPanel(); BluePanel blue = new BluePanel(); public Try() { setLayout(null); green.setBounds(10, 10, 250, 200); blue.setBounds(10, 280, 250, 200); add(green); add(blue); } /*public void paint(Graphics g) { g.setColor(Color.yellow); g.fillRect(getX(), getY(), getWidth(), getHeight()); }*/ public static void main(String args[]) { Try t = new Try(); JFrame j = new JFrame(); j.setSize(300, 700); j.setVisible(true); j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); j.getContentPane().add(t); } } class GreenPanel extends JPanel { @Override public void paintComponents(Graphics i) { i.setColor(Color.green); i.fillRect(0, 0, getWidth(), getHeight()); } } class BluePanel extends JPanel { @Override public void paintComponents(Graphics s) { s.setColor(Color.blue); s.fillRect(0, 0, getWidth(), getHeight()); } }[why are you annoyed with my sig?]
- 07-30-2010, 03:51 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Try adding the component to the content pane prior to making the JFrame visible, or otherwise call validate on the JFrame or revalidate on the JPanel
- 07-30-2010, 08:27 PM #3
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
You should NOT be overriding the paintComponents() method. To change the background color of a panel you just use:
Generally it is NOT a good idea to use a null layout. Give you panels a "preferred size" and then add then to a panel that uses a layout manager.Java Code:panel.setBackground( Color.GREEN );
- 07-31-2010, 04:25 AM #4
Member
- Join Date
- Feb 2010
- Posts
- 80
- Rep Power
- 0
okay, gotta try these later..thanks BTW
[why are you annoyed with my sig?]
- 07-31-2010, 09:11 AM #5
Member
- Join Date
- Feb 2010
- Posts
- 80
- Rep Power
- 0
Similar Threads
-
Adding panels into a panel
By kcakir in forum AWT / SwingReplies: 7Last Post: 12-07-2009, 05:19 PM -
Spliting the Panel to three panels
By suraw in forum New To JavaReplies: 0Last Post: 03-25-2009, 06:05 PM -
Can I add scrollPane to panel with not access to the frame
By itaipee in forum AWT / SwingReplies: 8Last Post: 01-13-2009, 10:30 AM -
Problem in adding Multiple Panels at the Specific positon on frame
By SANDY_INDIA in forum AWT / SwingReplies: 7Last Post: 07-09-2008, 12:06 AM -
How to place panel into frame
By vivek_9912 in forum AWT / SwingReplies: 2Last Post: 11-19-2007, 11:21 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks