一个 JFrame 里面有四个 单选按钮  + 一个 确定按钮 + 一个标签我想利用 选择单选按钮+单击确定按钮  切换 标签的内容  ; 能实现吗?谢谢

解决方案 »

  1.   

    这是部分程序:我想在切换背景色的同时来换标签lab里面的图片
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class test extends JFrame{
    private JLabel label,lab,lab1,lab2,lab3,lab4;
    private Container container;
    private ButtonGroup radiogroup;
    private JRadioButton bta,btb,btc,btd;
    private ImageIcon icon,icon1,icon2,icon3,icon4;    public test() {
         super("单选按钮");
         container = getContentPane();
         container.setLayout(new FlowLayout());     label = new JLabel("选择你最喜欢的老师:");
         container.add(label);     bta = new JRadioButton("张",true);
         bta.setOpaque(false);
         container.add(bta);     btb = new JRadioButton("陈",false);
         btb.setOpaque(false);
         container.add(btb);     btc = new JRadioButton("李",false);
         btc.setOpaque(false);
         container.add(btc);     btd = new JRadioButton("吴",false);
         btd.setOpaque(false);
         container.add(btd);     radiogroup = new ButtonGroup();
         radiogroup.add(bta);
         radiogroup.add(btb);
         radiogroup.add(btc);
         radiogroup.add(btd);     JButton button = new JButton("确定");
         container.add(button); icon = new ImageIcon("1.jpg");
       lab = new JLabel(icon);
       lab.setBounds(0, 0,icon.getIconWidth(), icon.getIconHeight());
    container.add(lab);  button.addActionListener(
         new ActionListener(){
         public void actionPerformed(ActionEvent e){
    if(bta.isSelected()) container.setBackground(Color.RED);
    if(btb.isSelected()) container.setBackground(Color.GREEN);
    if(btc.isSelected()) container.setBackground(Color.BLUE);
    if(btd.isSelected()) container.setBackground(Color.YELLOW);
    }
         }
         );
         setBounds(500,200,450,400);
         setVisible(true);
         setResizable(false);
        }
        public static void main(String[] args){
         test application = new test();
         application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }
      

  2.   

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;public class test extends JFrame {
    private JLabel label, lab, lab1, lab2, lab3, lab4;
    private Container container;
    private ButtonGroup radiogroup;
    private JRadioButton bta, btb, btc, btd;
    private ImageIcon icon, icon1, icon2, icon3, icon4; public test() {
    super("单选按钮");
    container = getContentPane();
    container.setLayout(new FlowLayout()); label = new JLabel("选择你最喜欢的老师:");
    container.add(label); bta = new JRadioButton("张", true);
    bta.setOpaque(false);
    container.add(bta); btb = new JRadioButton("陈", false);
    btb.setOpaque(false);
    container.add(btb); btc = new JRadioButton("李", false);
    btc.setOpaque(false);
    container.add(btc); btd = new JRadioButton("吴", false);
    btd.setOpaque(false);
    container.add(btd); radiogroup = new ButtonGroup();
    radiogroup.add(bta);
    radiogroup.add(btb);
    radiogroup.add(btc);
    radiogroup.add(btd); JButton button = new JButton("确定");
    container.add(button); icon1 = new ImageIcon("1.jpg");
    icon2 = new ImageIcon("2.jpg");
    icon3 = new ImageIcon("3.jpg");
    icon4 = new ImageIcon("4.jpg");
    lab = new JLabel();
    container.add(lab); button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    if (bta.isSelected()) {
    container.setBackground(Color.RED);
    lab.setIcon(icon1);
    }
    if (btb.isSelected()) {
    container.setBackground(Color.GREEN);
    lab.setIcon(icon2);
    }
    if (btc.isSelected()) {
    container.setBackground(Color.BLUE);
    lab.setIcon(icon3);
    }
    if (btd.isSelected()) {
    container.setBackground(Color.YELLOW);
    lab.setIcon(icon4);
    }

    if (lab.getIcon() != null) {
    lab.setBounds(0, 0, lab.getIcon().getIconWidth(), lab.getIcon().getIconHeight());
    }
    }
    }); setBounds(500, 200, 450, 400);
    setVisible(true);
    setResizable(false);
    } public static void main(String[] args) {
    test application = new test();
    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }}
      

  3.   

    图片位置不对
    我修改了一下
    if (lab.getIcon() != null) {
                       lab.setSize(lab.getIcon().getIconWidth(), lab.getIcon().getIconHeight());
    这样就好了