MesRadio.java 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package com.mes.component;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import javax.swing.*;
  5. import java.awt.*;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. public class MesRadio extends JPanel {
  9. public static final Logger log = LoggerFactory.getLogger(MesRadio.class);
  10. private String result = "";
  11. private Integer i = 0;
  12. public MesRadio(String[] radioTitles,String[] radioVals){
  13. this.setLayout(new FlowLayout());
  14. ButtonGroup group = new ButtonGroup();
  15. i = 0;
  16. for (String title:radioTitles){
  17. JRadioButton radioButton = new JRadioButton(title);
  18. radioButton.setFont(new Font("微软雅黑", Font.PLAIN, 28));
  19. String val = radioVals[i];
  20. radioButton.setName(val);
  21. this.add(radioButton);
  22. group.add(radioButton);
  23. radioButton.addActionListener(new ActionListener() {
  24. @Override
  25. public void actionPerformed(ActionEvent e) {
  26. result = val;
  27. log.info("选择:"+result);
  28. }
  29. });
  30. i++;
  31. }
  32. }
  33. public void setResult(String ret){
  34. this.result = "";
  35. Component[] components = this.getComponents();
  36. for (Component jRadioButton:components){
  37. if(jRadioButton instanceof JRadioButton){
  38. JRadioButton jRadioButtonParam = (JRadioButton)jRadioButton;
  39. if(ret.equals(jRadioButtonParam.getName())){
  40. jRadioButtonParam.setSelected(true);
  41. this.result = jRadioButtonParam.getName();
  42. }else{
  43. jRadioButtonParam.setSelected(false);
  44. }
  45. }
  46. }
  47. }
  48. public String getResult(){
  49. return this.result;
  50. }
  51. }