0
点赞
收藏
分享

微信扫一扫

GUI-study-lesson11(Swing之按钮:图片按钮·单选复选按钮)

c一段旅程c 2022-05-01 阅读 44

一.图片按钮

package lesson05;

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

public class JButtonDemo extends JFrame {
    public JButtonDemo() {
        Container contentPane = this.getContentPane();

        //将图片变成一个图标
        URL url = JButtonDemo.class.getResource("资金1.png");
        ImageIcon imageIcon = new ImageIcon(url);

        JButton jButton = new JButton();
        jButton.setIcon(imageIcon);
        jButton.setText("请点击它");
        jButton.setToolTipText("This is a ImageIcon");

        contentPane.add(jButton);

        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);
        //this.setBounds(100,100,400,300);
        this.pack();
    }

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

    二.单选按钮

package lesson05;

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

public class JBluttonDemo2 extends JFrame {

    public JBluttonDemo2() {
        Container contentPane1 = getContentPane();
        URL url = JBluttonDemo2.class.getResource("资金1.png");
        ImageIcon imageIcon = new ImageIcon();

        //单选框
        JRadioButton jRadioButton1 = new JRadioButton("jRadioButton1");
        JRadioButton jRadioButton2 = new JRadioButton("jRadioButton2");
        JRadioButton jRadioButton3 = new JRadioButton("jRadioButton3");
        //单选框只能选一个,所以分组,一组选一个
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(jRadioButton1);
        buttonGroup.add(jRadioButton2);
        buttonGroup.add(jRadioButton3);

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




        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.pack();
      }

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

 三.复选按钮  

package lesson05;

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

public class JButtonDemo3 extends JFrame {

    public JButtonDemo3(){
        Container contentPane = getContentPane();
        URL url = JButtonDemo3.class.getResource("资金1.png");
        ImageIcon imageIcon = new ImageIcon(url);

        JCheckBox jCheckBox1 = new JCheckBox("jCheckBox1");
        JCheckBox jCheckBox2 = new JCheckBox("jCheckBox2");
        JCheckBox jCheckBox3 = new JCheckBox("jCheckBox3");

        contentPane.add(jCheckBox1,BorderLayout.NORTH);
        contentPane.add(jCheckBox2,BorderLayout.CENTER);
        contentPane.add(jCheckBox3,BorderLayout.SOUTH);


        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.pack();
    }

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

 

举报

相关推荐

0 条评论