BorderLayout.CENTER doesn't center

 ̄綄美尐妖づ 提交于 2019-12-24 12:19:10

问题


I can't get my JLabel to center in my JPanel after adding a ChartPanel to it:

JPanel panel = new JPanel(new BorderLayout());            
panel.add(visualiser(ternaire), BorderLayout.NORTH);//visualiser(ternaire) is the ChartPanel
panel.add(new JLabel("L'alliage a bien été enregistré."), BorderLayout.CENTER);
JOptionPane jop = new JOptionPane();            
jop.showMessageDialog(null, panel, "Information", JOptionPane.PLAIN_MESSAGE);

Any idea why BorderLayout.CENTER doesn't work ?


回答1:


JLabel by default is horizontally aligned to the left.

You need to set the horizontalAlignment to be JLabel.CENTER

JPanel panel = new JPanel(new BorderLayout());
JLabel label = new JLabel("L'alliage a bien été enregistré.");
label.setHorizontalAlignment(JLabel.CENTER);
label.setVerticalAlignment(JLabel.CENTER);
panel.add(label, BorderLayout.CENTER);



回答2:


This doesn't center your JLabel, it puts it toe the left side (I think). You need to change the alignment of the JLabel.



来源:https://stackoverflow.com/questions/16957329/borderlayout-center-doesnt-center

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!