1 public class RadioButton extends Canvas { 2 3 protected final Image RADIO_CHECKED = createImage("radiobutton_checked.png"); 4 protected final Image RADIO_UNCHECKED = createImage("radiobutton_unchecked.png"); 5 6 protected Image decrotorImage;//左侧图标 7 protected String text = "RadioButton";//默认文字 8 protected boolean selection;//选中状态 9 protected int imageSize = 16;//默认图标大小 10 11 private int mouse = 0;//鼠标状态 12 private boolean hit = false;//碰撞 13 14 /** 15 * 16 * @param parent 17 * @param style SWT.NONE 18 */ 19 public RadioButton(Composite parent, int style) { 20 21 super(parent, style); 22 //默认未选中 23 setDecrotorImage(RADIO_UNCHECKED); 24 25 this.addPaintListener(new PaintListener() { 26 27 //绘制按钮 28 @Override 29 public void paintControl(PaintEvent e) { 30 31 Rectangle rect = getBounds(); 32 Color foreground = getForeground(); 33 Color background = getBackground(); 34 Font font = getFont(); 35 36 e.gc.setBackground(background); 37 e.gc.setForeground(foreground); 38 e.gc.setFont(font); 39 40 //图片垂直居中 41 int imgX = 2; 42 int imgY = (rect.height - imageSize) / 2; 43 e.gc.drawImage(getDecrotorImage(), imgX, imgY); 44 45 //文字垂直居中 46 FontMetrics fontMetrics = e.gc.getFontMetrics(); 47 int padding = 2; 48 int txtX = imgX + getDecrotorImage().getBounds().width + padding; 49 int txtY = (rect.height - fontMetrics.getHeight()) / 2; 50 e.gc.drawText(getText(), txtX, txtY - 1); 51 52 e.gc.dispose(); 53 54 } 55 }); 56 this.addMouseListener(new MouseAdapter() { 57 @Override 58 public void mouseDown(MouseEvent e) { 59 hit = true; 60 mouse = 2; 61 // redraw(); 62 } 63 64 @Override 65 public void mouseUp(MouseEvent e) { 66 hit = false; 67 mouse = 1; 68 if (e.x < 0 || e.y < 0 || e.x > getBounds().width || e.y > getBounds().height) { 69 mouse = 0; 70 } 71 if (mouse == 1) { 72 setSelection(true); 73 //触发重绘 74 redraw(); 75 notifyListeners(SWT.Selection, new Event()); 76 } 77 } 78 }); 79 this.setCursor(new Cursor(this.getDisplay(), SWT.CURSOR_HAND)); 80 81 } 82 83 private static Image createImage(String name) { 84 InputStream stream = RadioButton.class.getResourceAsStream(name); 85 Image image = new Image(null, stream); 86 try { 87 stream.close(); 88 } catch (IOException ioe) { 89 } 90 return image; 91 } 92 93 public void setSelection(boolean selection) { 94 if (this.selection == selection) { 95 return; 96 } 97 this.selection = selection; 98 //设置互斥 99 if (selection) { 100 if (getParent() != null) { 101 Control[] controls = getParent().getChildren(); 102 for (Control control : controls) { 103 if (control instanceof RadioButton) { 104 if (!(this == control)) 105 ((RadioButton) control).setSelection(false); 106 } 107 } 108 } 109 } 110 setDecrotorImage(selection ? RADIO_CHECKED : RADIO_UNCHECKED); 111 redraw(); 112 } 113 114 /** 115 * @param decrotorImage the decrotorImage to set 116 */ 117 protected void setDecrotorImage(Image decrotorImage) { 118 //对图片进行缩放 119 ImageData imageData = decrotorImage.getImageData(); 120 ImageData newImageData = imageData.scaledTo(imageSize, imageSize); 121 Image scaledImage = new Image(getDisplay(), newImageData); 122 this.decrotorImage = scaledImage; 123 } 124 125 /** 126 * @return the decrotorImage 127 */ 128 protected Image getDecrotorImage() { 129 return decrotorImage; 130 } 131 132 133 /** 134 * @param text the text to set 135 */ 136 public void setText(String text) { 137 this.text = text; 138 } 139 140 141 /** 142 * @return the text 143 */ 144 public String getText() { 145 return text; 146 } 147 148 /** 149 * @return the selection 150 */ 151 public boolean isSelection() { 152 return selection; 153 } 154 155 public int getImageSize() { 156 return imageSize; 157 } 158 159 public void setImageSize(int imageSize) { 160 this.imageSize = imageSize; 161 } 162 163 }
标签:selection,return,int,text,Image,public,radiobutton,swt,模拟 From: https://www.cnblogs.com/gzhq/p/16984384.html