JRadioButton 实现图片切换

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";
    JRadioButton birdButton,catButton,dogButton,rabbitButton,pigButton;
    JLabel picture;
    ButtonGroup  bGroup;
    public RadioButtonDemo() {
        super(new BorderLayout());

        //Create the radio buttons.
        birdButton = new JRadioButton(birdString);
        birdButton.setMnemonic(KeyEvent.VK_B);
        birdButton.setActionCommand(birdString);
        
        birdButton.doClick();//to do: 将birdButton置为选中状态
        
        catButton = new JRadioButton(catString);
        catButton.setMnemonic(KeyEvent.VK_C);
        catButton.setActionCommand(catString);

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

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

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

        //to do:用ButtonGroup将各个按钮组合起来,这样选中一个按钮,另一个才会取消选中状态
       bGroup  = new ButtonGroup();
       bGroup.add(birdButton);//将单选按钮添加到ButtonGroup中
       bGroup.add(catButton);
       bGroup.add(dogButton);
       bGroup.add(rabbitButton);
       bGroup.add(pigButton);
        
        //to do:将当前RadioButtonDemo对象注册到各个radio button中
     
       //dogButton.addActionListener(new RadioButtonDemo());
        birdButton.addActionListener(this) ;  
        catButton.addActionListener(this) ;  
        dogButton.addActionListener(this) ;  
        rabbitButton.addActionListener(this) ;  
        pigButton.addActionListener(this) ;  
       
        //Set up the picture label.
        picture = new JLabel(createImageIcon("images/"+ birdString + ".gif"));

        //The preferred size is hard-coded to be the width of the
        //widest image and the height of the tallest image.
        //A real program would compute this.
        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);
        BorderLayout blayout = new BorderLayout();
        
        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) {
		// TODO Auto-generated method stub
    	if (e.getSource().equals(birdButton)) {
    		Icon icon = createImageIcon("images/"+ birdString + ".gif");
    		picture.setIcon(icon);
		}
    	if (e.getSource().equals(catButton)) {
    		Icon icon = createImageIcon("images/"+ catString + ".gif");
    		picture.setIcon(icon);
		}
    	if (e.getSource().equals(dogButton)) {
    		Icon icon = createImageIcon("images/"+ dogString + ".gif");
    		picture.setIcon(icon);
		}
    	if (e.getSource().equals(rabbitButton)) {
    		Icon icon = createImageIcon("images/"+ rabbitString + ".gif");
    		picture.setIcon(icon);
		}
    	if (e.getSource().equals(pigButton)) {
    		Icon icon = createImageIcon("images/"+ pigString + ".gif");
    		picture.setIcon(icon);
		}
    	
    	
    	
	}
    
    
   

	/** 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;
        }
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    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();
            }
        });
    }

	
}

/*创建imges文件夹放入图片,将整个images文件夹移动到eclipse项目工程bin目录下,即可调试*/




此为老师布置的作业         点击 老师博客

posted @ 2018-04-29 18:22  hunterxing  阅读(484)  评论(0编辑  收藏  举报