2019-10-21|811阅|作者:我真的很帅|举报 摘要:全部代码
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class JRadioButton1 extends JFrame{
private ButtonGroup group;
private JPanel penal;
private JPanel pallet;
public JRadioButton1() throws HeadlessException{
pallet=new JPanel();
this.add(pallet,BorderLayout.CENTER);
penal=new JPanel();
group=new ButtonGroup();
addJRadioBtton("灰");
addJRadioBtton("粉");
addJRadioBtton("黄");
this.add(penal,BorderLayout.SOUTH);
this.setSize(300,300);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void addJRadioBtton(final String string) {
// TODO 自动生成的方法存根
JRadioButton radioButton=new JRadioButton(string);
group.add(radioButton);
penal.add(radioButton);
radioButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成的方法存根
Color color=null;
if ("灰".equals(string)){
color=Color.GRAY;
}else if("粉".equals(string)){
color=Color.PINK;
}else if("黄".equals(string)){
color=Color.YELLOW;
}else{
color=Color.WHITE;
}
pallet.setBackground(color);
}
});
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
new JRadioButton1();
}
}