图片按钮、单选框、多选框

一、图片按钮

package com.massimo.component;

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

public class JButtonTest extends JFrame {

    public JButtonTest(){
        Container container = this.getContentPane();
        //将一个图片变为图标
        URL url = JButtonTest.class.getResource("qq.png");
        Icon icon = new ImageIcon(url);

        //把这个图标放在按钮上
        JButton button = new JButton();
        button.setIcon(icon);
        button.setToolTipText("图片按钮");

        container.add(button);

        this.setVisible(true);
        this.setSize(500 , 300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

}

效果:
在这里插入图片描述

二、单选按钮

package com.massimo.component;

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

public class SingleBox extends JFrame {

    public SingleBox(){
        Container container = this.getContentPane();

        //单选框
        JRadioButton jRadioButton1 = new JRadioButton("JRadioButton1");
        JRadioButton jRadioButton2 = new JRadioButton("JRadioButton2");
        JRadioButton jRadioButton3 = new JRadioButton("JRadioButton3");

        //由于单选框只能选择一个,所以我们要进行分组,一个组中只能选择一个
        ButtonGroup group = new ButtonGroup();
        group.add(jRadioButton1);
        group.add(jRadioButton2);
        group.add(jRadioButton3);

        container.add(jRadioButton1 , BorderLayout.CENTER);
        container.add(jRadioButton2 , BorderLayout.NORTH);
        container.add(jRadioButton3 , BorderLayout.SOUTH);

        this.setVisible(true);
        this.setSize(500 , 300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

效果:
在这里插入图片描述

四、复选框

package com.massimo.component;

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

public class CheckBoxTest extends JFrame {

    public CheckBoxTest(){
        Container container = this.getContentPane();

        //多选框
        JCheckBox box1 = new JCheckBox("checkBox1");
        JCheckBox box2 = new JCheckBox("checkBox2");

        container.add(box1 , BorderLayout.NORTH);
        container.add(box2 , BorderLayout.SOUTH);

        this.setVisible(true);
        this.setSize(500 , 300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

效果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值