"How to give spaces between jlabel?"
setBounds and null layouts
: no; Use EmptyBorders
: YES!; Use LayoutManagers
: YES!
"how can i use this can u explain little bit "
Use can use the default FlowLayout
of the containing JPanel
(or in this case set the gaps of the FlowLayout)
FlowLayout constructor:
public FlowLayout(int align,
int hgap,
int vgap)
align - the alignment value
hgap - the horizontal gap between components and between the components and the borders of the Container
vgap - the vertical gap between components and between the components and the borders of the Container
Example
JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 5);
for (int i = 0; i < 10; i++) {
panel.add(new JLabel("Label" + i));
}
You could use EmptyBorder
EmptyBorder constructor
public EmptyBorder(int top,
int left,
int bottom,
int right)
top - the top inset of the border
left - the left inset of the border
bottom - the bottom inset of the border
right - the right inset of the border
Example
JPanel panel = new JPanel();
for (int i = 0; i < 10; i++) {
JLabel label = new JLabel("Label" + i);
EmptyBorder border = new EmptyBorder(5, 20, 5, 20);
label.setBorder(border);
panel.add(label);
}
If you want to use a LineBorder
and an EmptyBorder
for margins, you can use a CompoundBorder
A composite Border class used to compose two Border objects into a single border by nesting an inside Border object within the insets of an outside Border object. For example, this class may be used to add blank margin space to a component with an existing decorative border:
CompoundBorder Example
JPanel panel = new JPanel();
EmptyBorder border = new EmptyBorder(5, 20, 5, 20);
LineBorder line = new LineBorder(Color.blue, 2, true);
CompoundBorder compound = new CompoundBorder(line, border);
for (int i = 0; i < 10; i++) {
JLabel label = new JLabel("Label" + i);
label.setBorder(compound);
panel.add(label);
}
There is an abundant number possibilities. Choose your flavor. The whole point it no not try and set size and position to everything, and make use of layout managers and borders and gaps for sizing, spacing etc.
Learn to use the different layout managers at Laying out Components Within a Container
Full example using CompoundBorder and GridLayout(int rows, int cols, int hgap, int vgap)
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
public class CompoundBorderDemo {
public CompoundBorderDemo() {
JFrame frame = new JFrame();
JPanel panel = createPanel();
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private JPanel createPanel() {
JPanel panel = new JPanel(new GridLayout(5, 5, 10, 10));
EmptyBorder panelBorder = new EmptyBorder(10, 10, 10, 10);
panel.setBorder(panelBorder);
EmptyBorder border = new EmptyBorder(5, 20, 5, 20);
LineBorder line = new LineBorder(Color.blue, 2, true);
CompoundBorder compound = new CompoundBorder(line, border);
for (int i = 0; i < 25; i++) {
JLabel label = new JLabel("Label" + i);
label.setBorder(compound);
panel.add(label);
}
return panel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new CompoundBorderDemo();
}
});
}
}