简体   繁体   English

使用GroupLayout时如何向组件添加边框?

[英]How to add borders to components when using GroupLayout?

For example, I have two radio buttons and I want a border around them so that the UI looks clearer? 例如,我有两个单选按钮,并且希望在它们周围有一个边框,以便UI看起来更清晰? Tried searching but didn't find anything useful. 尝试搜索,但未找到任何有用的信息。 Thanks! 谢谢!

Below would create a border around your radio buttons. 下面将在您的单选按钮周围创建一个边框。 You can create different type of borders, check the BorderFactory API for a description about the different borders 您可以创建不同类型的边框,请检查BorderFactory API以获取有关不同边框的描述

JRadioButton yesButton   = new JRadioButton("Yes", true);
JRadioButton noButton    = new JRadioButton("No", false);

ButtonGroup bgroup = new ButtonGroup();
bgroup.add(yesButton);
bgroup.add(noButton);

JPanel radioPanel = new JPanel();
radioPanel.setLayout(new GridLayout(2, 1));
radioPanel.add(yesButton);
radioPanel.add(noButton);

radioPanel.setBorder(BorderFactory.createTitledBorder(
           BorderFactory.createEtchedBorder(), "BorderTitle"));

Create a panel, put those radio buttons in that panel.. and create a border around the panel 创建一个面板,将那些单选按钮放在该面板中。并在面板周围创建边框

How to use borders 如何使用边框

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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