Java中图标按钮-单选按钮-多选按钮的实现方式

图片按钮

package lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JButtonDemo01 extends JFrame {
    public JButtonDemo01() {
        Container container=this.getContentPane();
        URL resource = JButtonDemo01.class.getResource("12.jpg");
        Icon icon =new ImageIcon(resource);
        JButton button = new JButton();
        button.setIcon(icon);
        button.setToolTipText("图片按钮");
        container.add(button);
        setVisible(true);
        setBounds(100,100,500,300);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

单选按钮

package lesson05;

import javafx.scene.control.RadioButton;

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

public class JButtonDemo02 extends JFrame {
    public  JButtonDemo02(){
        Container container=this.getContentPane();
        JRadioButton r1=new JRadioButton("01");
        JRadioButton r2=new JRadioButton("02");
        JRadioButton r3=new JRadioButton("03");
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(r1);
        buttonGroup.add(r2);
        buttonGroup.add(r3);
        container.add(r1,BorderLayout.NORTH);
        container.add(r2,BorderLayout.CENTER);
        container.add(r3,BorderLayout.SOUTH);

        setVisible(true);
        setBounds(100,100,500,300);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JButtonDemo02();
    }
}

多选按钮

package lesson05;

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

public class JButtonDemo03 extends JFrame {
    public JButtonDemo03() {
        Container container=this.getContentPane();
        JCheckBox box1 = new JCheckBox("01");
        JCheckBox box2 = new JCheckBox("02");//设置多选框
        container.setLayout(new FlowLayout());
        container.add(box1);
        container.add(box2);
        setVisible(true);
        setBounds(100,100,500,300);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值