Hello everyone...I have been learning Java in our school using BlueJ and there is a certain problem i need to work out...

I need to create a frame with four panels in it...like this....


Sorry if i can't provide the codes because it's really hard learning java...

Can anyone please post the simplest codes as much as possible for this one...

any help will do thanks.

Recommended Answers

All 9 Replies

no, we're not going to give you "the codes". It's your job to do your own homework.

You do get a B for honesty though, at least you admit it's homwwork and you don't intend to do it yourself.

Hint: use GridLayout
Hint: panels can contain other components and have a layoutmanager

Take those 2, think about how to use that knowledge in combination, and you should be able to figure things out.

tnx for ur help...it would help me a lot

import java.awt.*;
import java.awt.GridLayout.*;
import javax.swing.*;

public class jwenting{
    public void init() {
        setLayout(new GridLayout(2,2));
        add(new Panel());
        add(new Panel());
        add(new Panel());
        add(new Panel());
    }
}

i tried using these codes but there is something wrong in it...

sorry but I really have few knowledge in java and our prof. doesn't teach us that good either...

any help tnx...

i also used these codes but there's an error...nothing seems to work...

ermmm... i use these codes to create 4 boxes in 1 frame....any idea how can i make these buttons to unclickable panels???

any idea?? tnx

You've been hit by the automatic resizing of Swing components ;)

When you just add a panel somewhere its default size is the default size of the region you add it to, which in a GridLayout is defined by what's in it.
So if you don't put anything on the panel it will default to size 0x0 pixels.

Your application worked fine (probably), you just didn't see anything because the behaviour you programmed created 4 panels that each had size 0x0.

To see the panels, you need to make it so they will be forced to a specific size, and give them a raised edge (for example).
Your idea of putting buttons on them will also work, if the idea is just to show the structure of the design quickly.
Of course you won't actually SEE the panels at all, you'll see the buttons located on them.

gee tnx for your help....i really helped me a lot.!!

and i also have a last question...

import java.awt.*;
import javax.swing.*;
public class newidea
{
public static void main(String a[])
{
JFrame f = new JFrame ("Project Frame with GUI");
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
JPanel panel6 = new JPanel();
JPanel panel7 = new JPanel();
JPanel panel8 = new JPanel();
JPanel panel9 = new JPanel();
JPanel panel10 = new JPanel();
JPanel panel11 = new JPanel();
JPanel panel12 = new JPanel();
JPanel panel13 = new JPanel();
panel5.setBackground(Color.blue);
panel4.setBackground(Color.green);
panel3.setBackground(Color.orange);
panel2.setBackground(Color.gray);
panel1.setBackground(Color.gray);
panel9.setBackground(Color.black);
panel8.setBackground(Color.white);
panel7.setBackground(Color.yellow);
panel6.setBackground(Color.pink);
panel13.setBackground(Color.cyan);
panel12.setBackground(Color.blue);
panel11.setBackground(Color.green);
panel10.setBackground(Color.orange);

panel1.setLayout(new GridLayout(2,1));
panel1.add(panel5);
panel1.add(panel4);
panel1.add(panel3);
panel1.add(panel2);
panel2.setLayout(new GridLayout(2,1));
panel2.add(panel9);
panel2.add(panel8);
panel2.add(panel7);
panel2.add(panel6);
panel6.setLayout(new GridLayout(2,1));
panel6.add(panel13);
panel6.add(panel12);
panel6.add(panel11);
panel6.add(panel10);
f.setContentPane(panel1);
f.setSize(800,800);
f.pack();
f.show();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
}
}

these codes show this...

any idea on how to make it larger...cuz it's so small =x

tnx.

YAY! i finally know how to make it bigger...

i removed

f.pack();

and it turned out to be good ^_^

problem solved...tihihi

setting the preferred size on the components before calling pack would also resolve the issue without having to remove the call to pack (which you shouldn't do).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.