I am using Jframe and Jpanels to display 6 blocks one after another. However I am getting a small gap ob the left side next to the blocks (not the grey border i added)but I want the boxes to be flush with the side of the screen.
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
public class JavaTask1CW
{
public static JLabel createLabel(String text)
{
JLabel label = new JLabel(text);
label.setFont(new Font ("Ariel",Font.BOLD,30));
return label;
}
public void go()
{
JFrame frame = new JFrame();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
frame.setLayout(new BorderLayout(0,0));
panel1.setLayout(new BorderLayout(0,0)); // 0 pixel gaps
panel3.setLayout(new GridLayout(6,0)); //Column panel 2 west
frame.add(panel1);
panel1.add(panel2, BorderLayout.WEST);
panel2.add(panel3, BorderLayout.WEST);
for ( int i = 0 ; i < 5 ; i++ ) {
panel3.add( new ColorLabel( 100, 100, Color.GREEN,1,Color.GRAY) );
}
panel3.add( new ColorLabel( 100, 100, Color.YELLOW,1,Color.GRAY) );
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
JavaTask1CW ob = new JavaTask1CW();
ob.go();
}
}
You forgot to setLayout
to panel2
, where you are add other panels to. panel2
has by default FlowLayout
. So you can create one with no gaps, or getLayout
from the panel cast it and set the gaps to 0. One possible solution:
FlowLayout fl = new FlowLayout(FlowLayout.LEFT, 0, 0);
panel2.setLayout(fl); // 0 pixel gaps
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.