简体   繁体   中英

Center a JCheckBox in the JPanel

I need to add a Label in the NORTH and 3 CheckBoxes in the CENTER and 3 Buttons at the SOUTH.

So I've created 2 JPanels (Principal, and Inside_P)

private JPanel Principal, Inside_P;
private JLabel Title;
private JCheckBox in, dou, flo;
private JButton End;

Title= new JLabel("Conversion", JLabel.CENTER);     // Works (Center the Label in the middle of North)
in = new JCheckBox("Integer", JCheckBox.CENTER);     // Can't work (I don't know)
dou = new JCheckBox("Double");
flo = new JCheckBox("Float");


Principal= new JPanel();
Inside_P = new JPanel();

Principal.setLayout(new BorderLayout());
Inside_P.setLayout(new BorderLayout());

Principal.add(Titre, BorderLayout.NORTH);
Principal.add(Inside, BorderLayout.CENTER);

Inside_P.add(in, BorderLayout.WEST);
Inside_P.add(dou, BorderLayout.CENTER);
Inside_P.add(flo, BorderLayout.EAST);

    Principal.add(End, BorderLayout.SOUTH);

What I need her is how can I center the 3 CheckBox in the Middle of the CENTER of the Pricipal JPanel ?

If you want them grouped together, let the Inside_P JPanel keeps it's default FlowLayout manager. If you add them, they will align in the center. They will be centered, however they are at the top of the Inside_P JPanel.

Before you add your JCheckboxe's to the Inside_P container,

Try adding a Box spacer:

Inside_P.add( Box.createVerticalGlue() );
Inside_P.add( Box.createVerticalStrut( 160 ) );
Inside_P.add(in);
Inside_P.add(dou);
Inside_P.add(flo);

If the user decides to resize the window, the check boxes will not stay in the center vertically.

做同样的事情-使您的Principal JPanel使用BorderLayout,然后将第三个复选框置于中间。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM