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();
    }
}

相关内容

热门资讯

辅助透视!拱趴大菠萝挂怎么安装... 辅助透视!拱趴大菠萝挂怎么安装,微乐自建房插件如何安装,技法教程(有挂分享)-哔哩哔哩1、完成拱趴大...
开挂透视!pokernow辅助... 开挂透视!pokernow辅助工具,微信微乐辅助ios,阶段教程(有挂技巧)-哔哩哔哩1、进入到po...
了解透视!wpk透视是真的假的... 了解透视!wpk透视是真的假的,微乐自建房脚本下载,机巧教程(存在有挂)-哔哩哔哩在进入wpk透视是...
普及透视!德州透视hhpoke... 普及透视!德州透视hhpoker,微乐小程序免费黑科技在线下载,指南书教程(有挂存在)-哔哩哔哩1、...
分享透视!hhpoker辅助器... 分享透视!hhpoker辅助器视频,微乐四川麻将辅助器,法门教程(有挂分享)-哔哩哔哩hhpoker...
必备透视!pokemmo辅助脚... 必备透视!pokemmo辅助脚本,微乐江苏小程序游戏破解器下载,模板教程(有挂教学)-哔哩哔哩1、每...
解密透视!wepoker辅助软... 解密透视!wepoker辅助软件视频,微乐宁夏小程序插件,讲义教程(有挂辅助)-哔哩哔哩1、在wep...
揭露透视!hhpkoer辅助器... 您好,hhpkoer辅助器这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很...
解迷透视!wepoker透视脚... 解迷透视!wepoker透视脚本下载,微乐自建房辅助工具别人怎么看,积累教程(真的有挂)-哔哩哔哩1...
分享透视!wepoker私局代... 分享透视!wepoker私局代打,微信小程序怎么开挂,手段教程(有挂教学)-哔哩哔哩一、wepoke...