How to organize JComponents on top and bottom of JPanel

I have three JButton

s, two JLabel

and one JTextField

in my program and JPanel

s FlowLayout

. I want to arrange three buttons at the beginning JPanel

and the rest at the endJPanel

. I need space between the three buttons and the last three components, but the code doesn't work. Can anyone help me?

This is the code:

JPanel panelMenu=new Jpanel(new FlowLayout())
panelMenu.add ( btnOpen );
panelMenu.add ( lblPage );
panelMenu.add ( txtCurrentPage );
panelMenu.add ( lblTotalPage );
panelMenu.add ( btnBackword );
panelMenu.add ( btnNext );
getContentPanel.add(panelMenu);

      

+3


source to share


2 answers


The class FlowLayout

puts components on a string. To get what you want you can use GridBagLayout

.

Sample code:

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

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

public class LayoutExamle {
    JFrame frame;
    JPanel panel;
    JButton b1, b2, b3;
    JLabel l1, l2;
    JTextField t1;

    GridBagConstraints constraints;

    public LayoutExamle(){
        initComp();
    }

    public void initComp(){
        frame = new JFrame("Example");
        frame.setLayout(new CardLayout());

        panel = new JPanel();

        panel.setSize(400, 350);
        panel.setLayout(new GridBagLayout());
        panel.setBackground(Color.WHITE);

        constraints = new GridBagConstraints();

        b1 = new JButton("<<");
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.gridwidth = 2;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.insets = new Insets(12, 43, 0, 0);
        panel.add(b1, constraints);

        b2 = new JButton("Open");
        constraints.gridx = 2;
        constraints.gridy = 0;
        constraints.gridwidth = 2;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.insets = new Insets(12, 18, 0, 0);
        panel.add(b2, constraints);

        b3 = new JButton(">>");
        constraints.gridx = 4;
        constraints.gridy = 0;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.insets = new Insets(12, 12, 0, 0);
        panel.add(b3, constraints);

        l1 = new JLabel("jLabel1");
        constraints.gridx = 0;
        constraints.gridy = 1;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.insets = new Insets(234, 43, 12, 0);
        panel.add(l1, constraints);

        l2 = new JLabel("jLabel1");
        l2.setText("jLabel2");
        constraints.gridx = 2;
        constraints.gridy = 1;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.insets = new Insets(234, 18, 12, 0);
        panel.add(l2, constraints);

        t1 = new JTextField();
        constraints.gridx = 4;
        constraints.gridy = 1;
        constraints.gridwidth = 2;
        constraints.ipadx = 130;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.insets = new Insets(230, 12, 12, 31);
        panel.add(t1, constraints);

        frame.add(panel);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new LayoutExamle();
    }
}

      



Screenshot:

enter image description here

+2


source


One way is to create two panels. Then you can add panels to the frame's content area, which uses BorderLayout by default:



frame.add(topPanel, BorderLayout.PAGE_START);
frame.add(bottomPanel, BorderLayout.PAGE_END);

      

+1


source







All Articles