radio button简单选择切换照片功能

package Cj;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


   public class RadioButtonDemo extends JPanel implements ActionListener {
    static String birdString = "Bird";
    static String catString = "Cat";
    static String dogString = "Dog";
    static String rabbitString = "Rabbit";
    static String pigString = "Pig";

    JLabel picture;
    JRadioButton birdButton,catButton,dogButton,rabbitButton,pigButton;//声明一个对象
    public RadioButtonDemo() {
        super(new BorderLayout());

        //Create the radio buttons.
        birdButton = new JRadioButton(birdString,true);
        birdButton.setMnemonic(KeyEvent.VK_B);
        birdButton.setActionCommand(birdString);
        birdButton.addActionListener(this);//注册监听事件
        //to do: 将birdButton置为选中状态

        catButton = new JRadioButton(catString);
        catButton.setMnemonic(KeyEvent.VK_C);
        catButton.setActionCommand(catString);
        catButton.addActionListener(this);

        dogButton = new JRadioButton(dogString);
        dogButton.setMnemonic(KeyEvent.VK_D);
        dogButton.setActionCommand(dogString);
        dogButton.addActionListener(this);

        rabbitButton = new JRadioButton(rabbitString);
        rabbitButton.setMnemonic(KeyEvent.VK_R);
        rabbitButton.setActionCommand(rabbitString);
        rabbitButton.addActionListener(this);

        pigButton = new JRadioButton(pigString);
        pigButton.setMnemonic(KeyEvent.VK_P);
        pigButton.setActionCommand(pigString);
        pigButton.addActionListener(this);

        //to do:用ButtonGroup将各个按钮组合起来,这样选中一个按钮,另一个才会取消选中状态
        ButtonGroup bg = new ButtonGroup();
        bg.add(birdButton);
        bg.add(catButton);
        bg.add(dogButton);
        bg.add(rabbitButton);
        bg.add(pigButton);

        //Set up the picture label.
        picture = new JLabel(createImageIcon("images/" + birdString + ".gif"));
        picture.setPreferredSize(new Dimension(177, 122));

        //Put the radio buttons in a column in a panel.
        JPanel radioPanel = new JPanel(new GridLayout(0, 1));
        radioPanel.add(birdButton);
        radioPanel.add(catButton);
        radioPanel.add(dogButton);
        radioPanel.add(rabbitButton);
        radioPanel.add(pigButton);

        add(radioPanel, BorderLayout.LINE_START);
        add(picture, BorderLayout.CENTER);
        setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
    }

    // to do:实现Action Listener接口的方法
    public void actionPerformed(ActionEvent e)
    {
        JRadioButton temp = (JRadioButton) e.getSource();//获取到里面的数据
        if (temp.equals(birdButton)) {
            Icon icon1 = createImageIcon("images/" + birdString + ".gif");
            picture.setIcon(icon1);//改变图片
        }else if(temp.equals(catButton)){
            Icon icon2 = createImageIcon("images/" + catString + ".gif");
            picture.setIcon(icon2);
        }else if(temp.equals(dogButton)){
            Icon icon3 = createImageIcon("images/" + dogString + ".gif");
            picture.setIcon(icon3);
        }else if(temp.equals(rabbitButton)){
            Icon icon4 = createImageIcon("images/" + rabbitString + ".gif");
            picture.setIcon(icon4);
        }else if(temp.equals(pigButton)){
            Icon icon5 = createImageIcon("images/" + pigString + ".gif");
            picture.setIcon(icon5);
        }

    }
       /** Returns an ImageIcon, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = RadioButtonDemo.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("RadioButtonDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        JComponent newContentPane = new RadioButtonDemo();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值