Cool Guy Cool Guy - 1 month ago 15
Java Question

How do I arrange JPanels from top to bottom?

I'm currently self-studying Java. I'm learning Graphical User Interface(GUI) programming.

I want

JPanel
s to be arranged from top to bottom in a
JFrame
.First of all,I have a
JLabel
added to the first
JPanel
. The second
JPanel
has 5
JRadioButtions
. The third
JPanel
has a
JButton
and a
JLabel
.

When the
JButton
is pressed,the
JLabel
in the 3rd
JPanel
shows some text.

I used
BoxLayout
(
BoxLayout.X_AXIS
) for all the
JPanels
and added all 3 of them into a
JFrame
which has
FlowLayout()
. Here is a small piece of code:

class GUI extends JFrame implements ActionListener {

JPanel pan1,pan2,pan3; //3 JPanels
JRadioButton rad1,rad2,rad3,rad4,rad5; //5 RadioButtons
JButton button; //A JButton
JLabel label; //A JLabel
public GUI(String header)
{
super(header);

setLayout(new FlowLayout()); //set FlowLayout to JFrame
setBounds(350,325,600,125);
setResizable(false);

creator();
adder();
commander();

add(pan1);
add(pan2);
add(pan3); //Add all 3 panels to JFrame

}


private void adder()
{
pan1.setLayout(new BoxLayout(pan1,BoxLayout.X_AXIS));
pan2.setLayout(new BoxLayout(pan2,BoxLayout.X_AXIS));
pan3.setLayout(new BoxLayout(pan3,BoxLayout.X_AXIS)); //Layout for all 3 JPanels

pan1.add(new JLabel("Choose a Security Level"));

ButtonGroup group=new ButtonGroup();

group.add(rad1);
group.add(rad2);
group.add(rad3);
group.add(rad4);
group.add(rad5);

pan2.add(rad1);
pan2.add(rad2);
pan2.add(rad3);
pan2.add(rad4);
pan2.add(rad5);

pan3.add(button);
pan3.add(label);
}

private void creator()
{
pan1=new JPanel();
pan2=new JPanel();
pan3=new JPanel();

rad1=new JRadioButton("Security Level 1");
rad2=new JRadioButton("Security Level 2");
rad3=new JRadioButton("Security Level 3");
rad4=new JRadioButton("Security Level 4");
rad5=new JRadioButton("Security Level 5");

button=new JButton("Move On");

label=new JLabel();
}

private void commander()
{
rad1.addActionListener(this);
rad2.addActionListener(this);
rad3.addActionListener(this);
rad4.addActionListener(this);
rad5.addActionListener(this);

rad1.setActionCommand("radio1");
rad2.setActionCommand("radio2");
rad3.setActionCommand("radio3");
rad4.setActionCommand("radio4");
rad5.setActionCommand("radio5");

button.addActionListener(this);
}

public void actionPerformed(ActionEvent evt)
{
//When button is pressed,the text in label changes

if(evt.getActionCommand().equals("radio1"))
label.setText("Very Easy to bypass");
else if(evt.getActionCommand().equals("radio2"))
label.setText("Easy to bypass");
else if(evt.getActionCommand().equals("radio3"))
label.setText("Can bypass Sometimes");
else if(evt.getActionCommand().equals("radio4"))
label.setText("Hard to bypass");
else if(evt.getActionCommand().equals("radio5"))
label.setText("Very Hard to bypass");
else
{ //Code here
}
repaint();
//More code here....
}


}


This is the output I'm getting when I select the first radiobutton(Forget the green colour):

Program

I want the "Very easy to Bypass" text to be placed above the "Move on" button and below all the
JRadioButtons
. I can increase the size of the
JFrame
so that there will be enough space. My questions are:


  • Which Layout should I use to achieve this?

  • Should this layout be applied just for the
    JFrame
    or all 3
    JPanels
    ?


Answer

Use GridLayout

GridLayout layout = new GridLayout(0, 1, 0, 5);
setLayout(layout);

What I would do to add 5 JPanels:

import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class PanelAdd extends JFrame {

    JPanel [] panels  ;

    public PanelAdd() {

        GridLayout layout = new GridLayout(0, 1, 0, 5);
        setLayout(layout);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        setSize(400, 350);
    }

    public static void main(String [] args) {

        PanelAdd add = new PanelAdd();
        add.addPanels();
        add.setVisible(true);

    }

    private void addPanels() {

        panels = new JPanel[5];
        for (int i = 0 ; i < panels.length ; i++) {

            panels[i] = new JPanel();
            panels[i].add(new JLabel("This Is Panel "+i));

            add(panels[i]);

        }

    }

}

In this example, I made an array of 5 JPanels and add them through a loop. I used GridLayout for the job.

This is just a hint for your answer