MesRadio.java 1.7 KB

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