JFrame:图片按钮,单选框,多选框

package demo02;

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

public class JButtonDemo01 extends JFrame {

    public JButtonDemo01() {
        Container container = this.getContentPane();
        //将图片变成图标
        URL url = JButtonDemo01.class.getResource("1.jpg");
        ImageIcon imageIcon = new ImageIcon(url);

        //把这个图片放在按钮上
        JButton jButton = new JButton();
        jButton.setIcon(imageIcon);
        jButton.setToolTipText("这是一个图片按钮");

        container.add(jButton);

        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBounds(100,100,600,400);


    }

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

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

public class JButtonDemo02 extends JFrame{
    public JButtonDemo02() {
        Container container = this.getContentPane();

        container.setLayout(new GridLayout(2,2));
        //单选框
        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);
        container.add(jRadioButton2);
        container.add(jRadioButton3);


        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBounds(100,100,600,400);


    }

    public static void main(String[] args) {
        new JButtonDemo02();
    }
}
package demo02;

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

public class JButtonDemo03 extends JFrame{
    public JButtonDemo03() {
        Container container = this.getContentPane();

        container.setLayout(new GridLayout(2,2));
        //多选框
        JCheckBox jCheckBox1 = new JCheckBox("jCheckBox1");
        JCheckBox jCheckBox2 = new JCheckBox("jCheckBox2");
        JCheckBox jCheckBox3 = new JCheckBox("jCheckBox3");

        container.add(jCheckBox1);
        container.add(jCheckBox2);
        container.add(jCheckBox3);

        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBounds(100,100,600,400);


    }

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

你可能感兴趣的:(GUI)