ButtonGroup的间距不统一。
创始人
2025-01-10 03:30:43
0

使用BoxLayout布局管理器代替默认的FlowLayout布局管理器,并在添加按钮之前设置组件垂直对齐方式和间距。示例代码如下:

import javax.swing.*;
import java.awt.*;

public class ButtonGroupTest extends JFrame {
    public ButtonGroupTest() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JRadioButton btn1 = new JRadioButton("Button 1");
        JRadioButton btn2 = new JRadioButton("Button 2");
        JRadioButton btn3 = new JRadioButton("Button 3");
        JRadioButton btn4 = new JRadioButton("Button 4");
        JRadioButton btn5 = new JRadioButton("Button 5");

        // 创建一个垂直方向的BoxLayout布局管理器
        BoxLayout layout = new BoxLayout(getContentPane(), BoxLayout.Y_AXIS);
        getContentPane().setLayout(layout);

        // 按钮之间的垂直间距为20px
        getContentPane().add(Box.createVerticalStrut(20));

        // 设置组件对齐方式为左对齐
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        panel.add(btn1);
        panel.add(btn2);
        getContentPane().add(panel);

        getContentPane().add(Box.createVerticalStrut(20));

        // 设置组件对齐方式为右对齐
        panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        panel.add(btn3);
        panel.add(btn4);
        getContentPane().add(panel);

        getContentPane().add(Box.createVerticalStrut(20));

        // 设置组件对齐方式为居中对齐
        panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        panel.add(btn5);
        getContentPane().add(panel);

        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        new ButtonGroupTest();
    }
}

相关内容

热门资讯

5次性理解!德扑圈透视“详细开... 5次性理解!德扑圈透视“详细开挂辅助攻略”德扑圈透视是一种具有地方特色的麻将游戏,要想赢得游戏,需要...
9次性知晓!花花生活圈可以开挂... 9次性知晓!花花生活圈可以开挂“揭露开挂辅助教程”1、不需要AI权限,帮助你快速的进行花花生活圈可以...
第1次性神器!福建兄弟十三水辅... 第1次性神器!福建兄弟十三水辅助工具“科普开挂辅助技巧”福建兄弟十三水辅助工具辅助器中分为三种模型:...
三次性知晓!新荣耀房卡辅助“揭... 三次性知晓!新荣耀房卡辅助“揭幕开挂辅助攻略”1、玩家可以在新荣耀房卡辅助软件透明挂俱乐部对游戏的模...
三次性了解!阿拉斗牌辅助视频“... 三次性了解!阿拉斗牌辅助视频“分享开挂辅助教程”1、不需要AI权限,帮助你快速的进行阿拉斗牌辅助视频...
第三次性辅助!逸趣鄱阳翻精辅助... 第三次性辅助!逸趣鄱阳翻精辅助“关于开挂辅助插件”1、许多玩家不知道逸趣鄱阳翻精辅助辅助软件怎么退出...
第3次性俱乐部!爱玩联盟辅助“... 第3次性俱乐部!爱玩联盟辅助“普及开挂辅助攻略”1、爱玩联盟辅助系统规律教程、爱玩联盟辅助辅助透视等...
七次性透明挂!兴动互娱脚本“分... 七次性透明挂!兴动互娱脚本“分享开挂辅助工具”1、该软件可以轻松地帮助玩家将兴动互娱脚本透视辅助提升...
第7次性私人局!上品游戏辅助器... 第7次性私人局!上品游戏辅助器“教你开挂辅助器”上品游戏辅助器软件透明挂微扑克wpk插件教程:1、微...
5次性熟悉!心悦提坑辅助器“详... 5次性熟悉!心悦提坑辅助器“详情开挂辅助方法”1、起透看视 心悦提坑辅助器透明视辅助2、随意选牌 心...