Centering a JLabel on a JPanel

后端 未结 5 872
花落未央
花落未央 2020-11-22 14:32

I\'m using the NetBeans GUI builder to handle my layout (I\'m terrible with LayoutManagers) and am trying to place a simple JLabel so that it is always centered (horizontall

5条回答
  •  没有蜡笔的小新
    2020-11-22 15:03

    Even with BoxLayout you can achieve that:

    JPanel listPane = new JPanel();
    listPane.setLayout(new BoxLayout(listPane, BoxLayout.X_AXIS ));
    
    JLabel label = new JLabel();
    listPane.add(Box.createHorizontalGlue());
    listPane.add(label);
    listPane.add(Box.createHorizontalGlue());
    

    mKorbel's solution is perfect for your goal. Anyway I always like to suggest BoxLayout because it's very flexible.

提交回复
热议问题