API for the setAlignOnBaseline(...) method:
Components that do not have a baseline will be centered.
JPanel does not have a reasonable baseline for use, as components may be on multiple lines depending on the layout manager used. Therefore he is focused.
I canβt say for your question if you are actually trying to center all the text on the basis, regardless of the font size, or just trying to make all the components colorize in the panel pane.
If you are trying to center the text on a baseline, perhaps you can redefine the baseline of the panel with the code:
JPanel subPanel = new JPanel() { @Override public int getBaseline(int width, int height) { Component c = getComponent(0); return c.getBaseline(width, height); } };
Of course, this will only work if all the components on the panel have the same baseline.
Or, if you are just trying to arrange all the components at the bottom of the panel, you need to use a different layout manager.
You can use the Relative layout to align all components at the bottom.
It can be used as a direct replacement for existing code:
RelativeLayout rl = new RelativeLayout(RelativeLayout.X_AXIS, 0); rl.setAlignment( RelativeLayout.TRAILING ); JPanel topPanel = new JPanel(rl);
Or, if you do not want to use a class without JDK, then the BoxLayout or GridBagLayout will be the way to go.
If you use BoxLayout , you will need to play with the setAlignmentY(...) property for each component.
If you use GridBagLayout , you will need to play with restrictions for each component.