将 JCheckBox 置于 JPanel 的中心
我需要在北边添加一个标签,在中边添加 3 个复选框,在南边添加 3 个按钮。
所以我创建了 2 个 JPanel(Principal 和 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);
我需要的是如何将 3 个复选框居中于 Prcipal 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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您希望将它们组合在一起,请让 Inside_P JPanel 保留其默认的 FlowLayout 管理器。如果添加它们,它们将在中心对齐。它们将居中,但位于 Inside_P JPanel 的顶部。
在将 JCheckboxe 添加到 Inside_P 容器之前,
请尝试添加 Box 间隔符:
如果用户决定调整窗口大小,复选框将不会垂直保持在中心。
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:
If the user decides to resize the window, the check boxes will not stay in the center vertically.
做同样的事情 - 让您的 Primary JPanel 使用 BorderLayout,并将第三个复选框放在中间。
Do the same thing - make your Principal JPanel use the BorderLayout, and place the third checkbox in the center.