I have a panel derived from JPanel
. I have a custom control derived from JLabel
. I am attempting to center this custom JLabel
on my panel.
The only way I know to do this that will work is to use the a null layout (setLayout(null)
) and then calculate the custom JLabel's setLocation()
point so that it's in the right spot.
The custom JLabel
is physically moved from one panel to this panel in this app and I believe the location previously set in setLocation
is affecting things. However when I set it to (0,0) the component goes up into the upper left corner.
BorderLayout
doesn't work because when only 1 component is provided and placed into BorderLayout.CENTER
, the central section expands to fill all of the space.
An example I cut and pasted from another site used BoxLayout
and component.setAlignmentX(Component.CENTER_ALIGNMENT)
. This didn't work either.
Another answer mentioned overriding the panel's getInset()
function (I think that's what it was called), but that proved to be a dead end.
So far I'm working with a panel with a GridBagLayout
layout and I include a GridBagConstraints
object when I insert the custom JLabel
into my panel. This is inefficient, though. Is there a better way to center the JLabel
in my JPanel
?
Set GridBagLayout for JPanel, put JLabel without any GridBagConstraints to the JPanel, JLabel will be centered
example
import java.awt.*;
import javax.swing.*;
public class CenteredJLabel {
private JFrame frame = new JFrame("Test");
private JPanel panel = new JPanel();
private JLabel label = new JLabel("CenteredJLabel");
public CenteredJLabel() {
panel.setLayout(new GridBagLayout());
panel.add(label);
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(panel);
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
CenteredJLabel centeredJLabel = new CenteredJLabel();
}
});
}
}
Supose your JLabel
is called label
, then use:
label.setHorizontalAlignment(JLabel.CENTER);
BoxLayout is the way to go. If you set up a X_AXIS BoxLayout, try adding horizontal glues before and after the component:
panel.add(Box.createHorizontalGlue());
panel.add(label);
panel.add(Box.createHorizontalGlue());
Forget all the LayoutManagers in the Java Standard Library and use MigLayout. In my experience it's much easier to work with an usually does exactly what you expect it to do.
Here's how to accomplish what you're after using MigLayout.
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
public class Test
{
public static void main( String[] args )
{
JFrame frame = new JFrame( );
JPanel panel = new JPanel( );
// use MigLayout
panel.setLayout( new MigLayout( ) );
// add the panel to the frame
frame.add( panel );
// create the label
JLabel label = new JLabel( "Text" );
// give the label MigLayout constraints
panel.add( label, "push, align center" );
// show the frame
frame.setSize( 400, 400 );
frame.setVisible( true );
}
}
Most of that is just boilerplate. The key is the layout constraint: "push, align center"
:
align center
tells MigLayout to place the JLabel in the center of its grid cell.
push
tells MigLayout to expand the grid cell to fill available space.
来源:https://stackoverflow.com/questions/9829319/centering-a-jlabel-in-a-jpanel