Set component at center of the page

前端 未结 3 1771
天涯浪人
天涯浪人 2020-12-12 07:01

How can I set a component (say button) at center of the panel?

I used Flowlayout with layout constraint as center, but I am getting button at

3条回答
  •  遥遥无期
    2020-12-12 07:49

    Use GridBagLayout instead of FlowLayout.

    JPanel panel=new JPanel();
    panel.setLayout(new GridBagLayout());
    panel.add(new JButton("Sample")); // will use default value of GridBagConstraints
    

提交回复
热议问题