本文主要是介绍Java SWing——单选按钮控制背景颜色,哈哈哈,答案牛牪犇逼,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
package 按钮组件;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class 按钮组件单选 {public static void main(String[] args) {new MyRadio("check单选窗口"); }
}
class MyRadio extends JFrame{JRadioButton rad1,rad2;JPanel jp,jp2;ButtonGroup bg;public MyRadio(String title){super(title);rad1=new JRadioButton("红色");rad2=new JRadioButton("绿色");bg=new ButtonGroup();jp=new JPanel();jp2=new JPanel();//匿名内部类rad1.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){jp2.setBackground(Color.red);}});rad2.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){jp2.setBackground(Color.GREEN);}});this.add(jp2,"Center");bg.add(rad1);bg.add(rad2);this.add(jp,"South");jp.add(rad1);jp.add(rad2);jp2.setBackground(Color.WHITE);//JFrame是透明的this.setLocation(300, 200);//设置位置this.setSize(300,200);//设置大小this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//this.setVisible(true);//}
}
这篇关于Java SWing——单选按钮控制背景颜色,哈哈哈,答案牛牪犇逼的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!