package com.mes.component; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class MesRadio extends JPanel { private String result = ""; private Integer i = 0; public MesRadio(String[] radioTitles,String[] radioVals){ this.setLayout(new FlowLayout()); ButtonGroup group = new ButtonGroup(); i = 0; for (String title:radioTitles){ JRadioButton radioButton = new JRadioButton(title); radioButton.setFont(new Font("微软雅黑", Font.PLAIN, 28)); String val = radioVals[i]; radioButton.setName(val); this.add(radioButton); group.add(radioButton); radioButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { result = val; System.out.println("选择:"+result); } }); i++; } } public void setResult(String ret){ this.result = ""; Component[] components = this.getComponents(); for (Component jRadioButton:components){ if(jRadioButton instanceof JRadioButton){ JRadioButton jRadioButtonParam = (JRadioButton)jRadioButton; if(ret.equals(jRadioButtonParam.getName())){ jRadioButtonParam.setSelected(true); this.result = jRadioButtonParam.getName(); }else{ jRadioButtonParam.setSelected(false); } } } } public String getResult(){ return this.result; } }